MongoDBIntroduction
MongoDB is 一款open-source NoSQLdocumentationdatalibrary, 它usingJSON-like格式storedata, providing了flexible datamodel and 强 big querycapacity. MongoDB适用于各种application场景, from simple moveapplication to complex 企业级system.
MongoDB versionclass型
- Community Edition: community版, 免费open-source, 适合 big many 数application场景
- Enterprise Edition: 企业版, package含advanced features, such asLDAP集成, audit and encryptionetc.
提示: for 于 big many 数Development者 and small 型project, Community Edition已经足够using. Enterprise Edition主要面向需要advancedsecurityfeatures and 企业级support big 型组织.
Windowssysteminstallation
步骤1: under 载MongoDB
- 访问MongoDB官方 under 载页面: https://www.mongodb.com/try/download/community
- 选择Windowsversion, 点击 under 载
步骤2: runinstallation程序
- 双击 under 载 .msifile启动installation向导
- 选择"complete"installationclass型
- in "Service Configuration"页面, 保持默认设置, 勾选"Install MongoDB as a Service"
- 点击"Install"completioninstallation
步骤3: verificationinstallation
-- 打开commands提示符, 输入以 under commands mongo --version -- 启动MongoDB Shell mongo -- 查看datalibrarylist show dbs
步骤4: configurationenvironmentvariable (可选)
- right 键点击"此电脑" → "property" → "advancedsystem设置" → "environmentvariable"
- in "systemvariable"in找 to "Path", 点击"编辑"
- 添加MongoDB binTable of Contentspath, 默认 for :
C:\Program Files\MongoDB\Server\4.4\bin(根据practicalversion调整) - 点击"确定"保存设置
macOSsysteminstallation
method1: usingHomebrewinstallation (推荐)
-- installationHomebrew (such as果尚未installation) /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" -- installationMongoDB brew tap mongodb/brew brew install mongodb-community -- 启动MongoDBservice brew services start mongodb-community -- verificationinstallation mongo --version -- 启动MongoDB Shell mongo
method2: using官方installationpackage
- 访问MongoDB官方 under 载页面, under 载macOSversion
- 解压 under 载 .tgzfile
- 将解压 after file夹move to 合适位置
- configurationenvironmentvariable, 将MongoDB binTable of Contents添加 to PATH
Linuxsysteminstallation
Ubuntu/Debiansystem
-- importMongoDB GPGkey wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add - -- creationMongoDBsourceslistfile echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list -- updatepackagelist sudo apt-get update -- installationMongoDB sudo apt-get install -y mongodb-org -- 启动MongoDBservice sudo systemctl start mongod -- 设置开机自启 sudo systemctl enable mongod -- verificationinstallation sudo systemctl status mongod mongo --version
CentOS/RHELsystem
-- creationMongoDBsourcesfile sudo vi /etc/yum.repos.d/mongodb-org-4.4.repo -- 添加以 under in 容 [mongodb-org-4.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc -- installationMongoDB sudo yum install -y mongodb-org -- 启动MongoDBservice sudo systemctl start mongod -- 设置开机自启 sudo systemctl enable mongod -- verificationinstallation sudo systemctl status mongod mongo --version
MongoDBconfiguration
configurationfile位置
| system | configurationfile位置 |
|---|---|
| Windows | C:\Program Files\MongoDB\Server\4.4\bin\mongod.cfg |
| macOS (Homebrew) | /usr/local/etc/mongod.conf |
| Linux | /etc/mongod.conf |
常用configuration选项
# storeconfiguration
store:
dbPath: /var/lib/mongodb # datastorepath
journal:
enabled: true # 启用 journal log
# networkconfiguration
net:
port: 27017 # 端口号
bindIp: 127.0.0.1 # 绑定IP, 0.0.0.0表示允许所 has IP访问
# logconfiguration
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# processmanagement
processmanagementment:
fork: true # 以守护process方式run
pidFilePath: /var/run/mongodb/mongod.pid
modifyconfiguration并重启service
-- modifyconfigurationfile after 重启service # Linuxsystem sudo systemctl restart mongod # macOS (Homebrew) brew services restart mongodb-community # Windowssystem net stop MongoDB net start MongoDB
commonissues解决
1. 端口占用issues
症状: 启动MongoDB时出现端口27017被占用 error.
solution:
-- find占用端口 process # Windows netstat -ano | findstr :27017 # macOS/Linux lsof -i :27017 -- 终止占用端口 process # Windows taskkill /PID <processID> /F # macOS/Linux sudo kill <processID>
2. permissionissues
症状: 启动MongoDB时出现permissionerror.
solution:
-- 修复dataTable of Contentspermission # Linux/macOS sudo chown -R mongodb:mongodb /var/lib/mongodb sudo chown -R mongodb:mongodb /var/log/mongodb -- 重启service sudo systemctl restart mongod
3. 连接issues
症状: 无法连接 to MongoDBserver.
solution:
- checkMongoDBservice is 否正 in run
- check防火墙 is 否阻止了端口27017
- checkconfigurationfilein bindIp设置
- 尝试using
mongo --host 127.0.0.1:27017连接
4. 启动失败
症状: MongoDBservice启动失败.
solution:
- 查看logfileUnderstand具体errorinformation
- checkdataTable of Contents is 否存 in 且 has 正确permission
- checkconfigurationfile语法 is 否正确
- 尝试deletelockfile after 重启:
sudo rm /var/lib/mongodb/mongod.lock
MongoDB Compass
MongoDB Compass is 官方providing graph形化managementtool, 它providing了直观 界面来managementMongoDBdatalibrary.
under 载 and installation
- 访问MongoDB Compass under 载页面: https://www.mongodb.com/try/download/compass
- 选择适合您system version under 载
- runinstallation程序completioninstallation
MongoDB Compass 主要functions
- visualization查看 and 编辑documentation
- 构建 and 执行query
- creation and managementindex
- monitordatalibraryperformance
- import and exportdata
- visualizationaggregate管道构建
实践case
case: configurationMongoDBcopy集
in Developmentenvironmentinconfiguration一个 simple MongoDBcopy集, improvingdatareliability.
-- 1. creationdataTable of Contents
mkdir -p /data/db1 /data/db2 /data/db3
-- 2. 启动三个MongoDBinstance
mongod --port 27017 --dbpath /data/db1 --replSet rs0
mongod --port 27018 --dbpath /data/db2 --replSet rs0
mongod --port 27019 --dbpath /data/db3 --replSet rs0
-- 3. 初始化copy集
mongo --port 27017
> rs.initiate()
> rs.add("localhost:27018")
> rs.add("localhost:27019")
> rs.status()
-- 4. verificationcopy集status
> rs.isMaster()
> use test
> db.test.insert({name: "test"})
> mongo --port 27018
> use test
> db.test.find()
互动练习
练习1: installationMongoDB
步骤:
- under 载适合您system MongoDB Community Edition
- 按照本tutorialin 步骤completioninstallation
- 启动MongoDBservice
- using
mongo --versionverificationinstallation - using
mongocommands启动Shell并runshow dbs查看datalibrarylist
练习2: modifyMongoDBconfiguration
步骤:
- 找 to MongoDBconfigurationfile
- modifyconfigurationfilein port设置 for 27018
- 保存configurationfile
- 重启MongoDBservice
- using
mongo --port 27018连接verification