Mongdb社区版下载地址https://www.mongodb.com/try/download/community
选择tgz下载,上传到服务器,解压:
tar -zxvf mongodb-linux-x86_64-rhel70-4.2.14.tgz
[root@localhost mongodb]# ll total 216824 drwxr-xr-x. 3 root root 135 May 27 04:34 mongodb-linux-x86_64-rhel70-4.2.14 -rw-r--r--. 1 root root 133214967 May 26 06:47 mongodb-linux-x86_64-rhel70-4.2.14.tgz
export PATH=/root/mongodb/mongodb-linux-x86_64-rhel70-4.2.14/bin:$PATH
添加到/etc/下的profile中。执行source /etc/profile
[root@localhost mongodb]# source /etc/profile [root@localhost mongodb]#
mkdir -p /mongodb/28017/conf mkdir -p /mongodb/28017/data mkdir -p /mongodb/28017/logs mkdir -p /mongodb/28018/conf mkdir -p /mongodb/28018/data mkdir -p /mongodb/28018/logs mkdir -p /mongodb/28019/conf mkdir -p /mongodb/28019/data mkdir -p /mongodb/28019/logs
mongod.conf,分别需要修改日志,数据文件和配置文件的位置,对应上面建立的目录
systemLog: #MongoDB发送所有日志输出的目标指定为文件 destination: file #mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径 path: /root/mongdb/db28017/logs/mongod.log #当mongos或mongod实例重新启动时,mongos或mongod会将新条目附加到现有日志文件的末尾。 logAppend: true storage: dbPath: /root/mongdb/db28017/data journal: #启用或禁用持久性日志以确保数据文件保持有效和可恢复。 enabled: true processManagement: #启用在后台运行mongos或mongod进程的守护进程模式。 fork: true pidFilePath: /root/mongdb/db28017/logs/mongod.pid net: #服务实例绑定的IP,0.0.0.0,任何IP都可以访问 bindIp: 0.0.0.0 #绑定的端口 port: 28017 replication: replSetName: rs0
mongod -f ./mongodb/db28017/conf/mongod.conf mongod -f ./mongodb/db28018/conf/mongod.conf mongod -f ./mongodb/db28019/conf/mongod.conf
如下输出说明启动完毕(三次每次都有):
[root@localhost ~]# mongod -f ./mongdb/db28017/conf/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 1731 child process started successfully, parent exiting [root@localhost ~]#
使用 ps -ef|grep mongod 查看一下是否启动成功,后台有三个mongodb进程说明成功
[root@localhost ~]# ps -ef|grep mongod root 1731 1 3 09:26 ? 00:00:03 mongod -f ./mongdb/db28017/conf/mongod.conf root 1805 1 14 09:28 ? 00:00:01 mongod -f ./mongdb/db28018/conf/mongod.conf root 1880 1 38 09:28 ? 00:00:01 mongod -f ./mongdb/db28019/conf/mongod.conf root 1956 1671 0 09:28 pts/0 00:00:00 grep --color=auto mongod [root@localhost ~]#
执行mongo --port 28017
默认第一个启动的是主库可以调优先级,主库写从库读,有rs0:PRIMARY说明是主库rs0:SECONDARY是从库,可以使用rs.status()查看详细信息。
[root@localhost ~]# mongo --port 28017 MongoDB shell version v4.2.14 connecting to: mongodb://127.0.0.1:28017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("b834c255-38ed-4632-949f-7476461a2e8f") } MongoDB server version: 4.2.14 Server has startup warnings: 2021-06-03T09:28:17.394-0400 I CONTROL [initandlisten] 2021-06-03T09:28:17.394-0400 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2021-06-03T09:28:17.394-0400 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2021-06-03T09:28:17.394-0400 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2021-06-03T09:28:17.394-0400 I CONTROL [initandlisten] 2021-06-03T09:28:17.394-0400 I CONTROL [initandlisten] 2021-06-03T09:28:17.394-0400 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2021-06-03T09:28:17.394-0400 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2021-06-03T09:28:17.394-0400 I CONTROL [initandlisten] 2021-06-03T09:28:17.394-0400 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2021-06-03T09:28:17.394-0400 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2021-06-03T09:28:17.394-0400 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- rs0:PRIMARY>
执行rs.initiate()
rs0:PRIMARY> rs.initiate() { "operationTime" : Timestamp(1622727158, 1), "ok" : 0, "errmsg" : "already initialized", "code" : 23, "codeName" : "AlreadyInitialized", "$clusterTime" : { "clusterTime" : Timestamp(1622727158, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } rs0:PRIMARY>
分别将28018和28019加入副本集
rs.add("192.168.0.10:28018") rs.add("192.168.0.10:28019")
rs0:PRIMARY> rs.add("192.168.0.10:28018") { "operationTime" : Timestamp(1622727197, 1), "ok" : 0, "errmsg" : "Found two member configurations with same host field, members.1.host == members.3.host == 192.168.0.10:28018", "code" : 103, "codeName" : "NewReplicaSetConfigurationIncompatible", "$clusterTime" : { "clusterTime" : Timestamp(1622727197, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } rs0:PRIMARY>
show dbs 可以看到当前所有的库
rs0:PRIMARY> show dbs admin 0.000GB config 0.000GB local 0.052GB mashibing 0.057GB test 0.000GB rs0:PRIMARY>
在主插入一条记录:
rs0:PRIMARY> db.test.insert({name:'liand'}) WriteResult({ "nInserted" : 1 }) rs0:PRIMARY>
在从库查询会出现:
需要再开个shell,连接到28018或28019上
mongo --port 28018
mongo --port 28019
rs0:SECONDARY> db.test.find() Error: error: { "operationTime" : Timestamp(1622727428, 1), "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotPrimaryNoSecondaryOk", "$clusterTime" : { "clusterTime" : Timestamp(1622727428, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }
需要在28018和28019上执行rs.secondaryOk(),再查询就不会有问题了:
上一步在主库插入的数据已经同步到从库中
rs0:SECONDARY> rs.secondaryOk() rs0:SECONDARY> db.test.find() { "_id" : ObjectId("60b8daa66e8c81042b24cbed"), "name" : "liand" } rs0:SECONDARY>