MongoDB集群搭建之主从模式

单机搭建

#创建docker持久化数据目录
[root@docker ~]# mkdir -p /root/application/program/mongodb/data/master-slaveMode
[root@docker ~]# cd /root/application/program/mongodb
#启动容器
[root@docker mongodb]# docker run -d -v `pwd`/data/master-slaveMode:/mongodb -p 30017:27017 docker.io/mongodb:3.6.3 /usr/sbin/init
ba3b27d855f6486151cc2248e6524d0c737ac3266f6b340c5ac5a29e43a42431
[root@docker mongodb]# docker ps
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS              PORTS                                                NAMES
ba3b27d855f6        docker.io/mongodb:3.6.3   "/usr/sbin/init"    30 seconds ago      Up 29 seconds       28017/tcp, 0.0.0.0:30017->27017/tcp                  laughing_thompson
f75c4ce68803        docker.io/mongodb:3.6.3   "/usr/sbin/init"    22 hours ago        Up 22 hours         0.0.0.0:37017->27017/tcp, 0.0.0.0:38017->28017/tcp   dazzling_kowalevski
b23aeb293514        docker.io/mongodb:3.6.3   "/usr/sbin/init"    2 days ago          Up 2 days           0.0.0.0:27017->27017/tcp, 0.0.0.0:28017->28017/tcp   distracted_bhaskara
[root@docker mongodb]# docker exec -it ba3b27d855f6 bash
[root@ba3b27d855f6 /]# 
#查看容器私有IP
[root@ba3b27d855f6 /]# ifconfig
\eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.4  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:4  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:04  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 648 (648.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@ba3b27d855f6 /]# cd /mongodb/mongodb/bin/
[root@ba3b27d855f6 bin]# ll
total 244520
-rwxr-xr-x. 1 root root  5487061 Aug 10 06:33 bsondump
drwxr-xr-x. 4 root root     4096 Aug 10 06:33 datas
-rwxr-xr-x. 1 root root     5792 Aug 10 06:33 install_compass
drwxr-xr-x. 2 root root      103 Aug 10 06:33 logs
-rwxr-xr-x. 1 root root 33065816 Aug 10 06:33 mongo
-rwxr-xr-x. 1 root root 58575600 Aug 10 06:33 mongod
-rw-r--r--. 1 root root      203 Aug 10 06:37 mongodb.conf
-rwxr-xr-x. 1 root root  8667585 Aug 10 06:33 mongodump
-rwxr-xr-x. 1 root root  6620731 Aug 10 06:33 mongoexport
-rwxr-xr-x. 1 root root  6471255 Aug 10 06:33 mongofiles
-rwxr-xr-x. 1 root root  6771267 Aug 10 06:33 mongoimport
-rwxr-xr-x. 1 root root 58105192 Aug 10 06:33 mongoperf
-rwxr-xr-x. 1 root root  9780339 Aug 10 06:33 mongoreplay
-rwxr-xr-x. 1 root root 10082759 Aug 10 06:33 mongorestore
-rwxr-xr-x. 1 root root 33447696 Aug 10 06:33 mongos
-rwxr-xr-x. 1 root root  6833184 Aug 10 06:33 mongostat
-rwxr-xr-x. 1 root root  6441493 Aug 10 06:33 mongotop
[root@ba3b27d855f6 bin]#
#创建主从节点的data目录
[root@ba3b27d855f6 bin]# mkdir -p datas/master datas/slave
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]#
#启动主节点进程
[root@ba3b27d855f6 bin]# ./mongod --master --dbpath /mongodb/mongodb/bin/datas/master --logpath /mongodb/mongodb/bin/logs/mongodb-master.log --bind_ip 172.17.0.4 --port 27017 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 74
child process started successfully, parent exiting
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# ps -ef | grep mongodb
root         74      0  6 06:42 ?        00:00:00 ./mongod --master --dbpath /mongodb/mongodb/bin/datas/master --logpath /mongodb/mongodb/bin/logs/mongodb-master.log --bind_ip 172.17.0.4 --port 27017 --fork
root        101     26  0 06:42 ?        00:00:00 grep --color=auto mongodb
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# 
#启动从节点进程
[root@ba3b27d855f6 bin]# ./mongod --slave --source 172.17.0.4:27017 --dbpath /mongodb/mongodb/bin/datas/slave --logpath /mongodb/mongodb/bin/logs/mongodb-slave.log --bind_ip 172.17.0.4 --port 27018 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 104
child process started successfully, parent exiting
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# ps -ef | grep mongodb
root         74      0  1 06:42 ?        00:00:01 ./mongod --master --dbpath /mongodb/mongodb/bin/datas/master --logpath /mongodb/mongodb/bin/logs/mongodb-master.log --bind_ip 172.17.0.4 --port 27017 --fork
root        104      0 21 06:43 ?        00:00:00 ./mongod --slave --source 172.17.0.4:27017 --dbpath /mongodb/mongodb/bin/datas/slave --logpath /mongodb/mongodb/bin/logs/mongodb-slave.log --bind_ip 172.17.0.4 --port 27018 --fork
root        133     26  0 06:43 ?        00:00:00 grep --color=auto mongodb
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]#
#验证
#主节点插入数据
[root@ba3b27d855f6 bin]# ./mongo 172.17.0.4:27017
MongoDB shell version v3.6.3
connecting to: mongodb://172.17.0.4:27017/test
MongoDB server version: 3.6.3
Server has startup warnings: 
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] 
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] 
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] 
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] 
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] 
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] ** WARNING: This node was started in master-slave replication mode.
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] **          Master-slave replication is deprecated and subject to be removed 
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] **          in a future version.
2018-08-10T06:42:13.217+0000 I CONTROL  [initandlisten] 
> show dbs
admin  0.000GB
local  0.000GB
> db.school.insert({"name" : "xuaiqi", "date" : new Date()})
WriteResult({ "nInserted" : 1 })
> db.school.find()
{ "_id" : ObjectId("5b6d3436dc9a5ab161c121e1"), "name" : "xuaiqi", "date" : ISODate("2018-08-10T06:44:06.284Z") }
> exit
bye
#登录从节点查询主从点插入的记录
[root@ba3b27d855f6 bin]# ./mongo 172.17.0.4:27018
MongoDB shell version v3.6.3
connecting to: mongodb://172.17.0.4:27018/test
MongoDB server version: 3.6.3
Server has startup warnings: 
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] 
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] 
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] 
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] 
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] 
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] ** WARNING: This node was started in master-slave replication mode.
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] **          Master-slave replication is deprecated and subject to be removed 
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] **          in a future version.
2018-08-10T06:43:14.911+0000 I CONTROL  [initandlisten] 
> show dbs
2018-08-10T06:44:23.880+0000 E QUERY    [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:816:19
shellHelper@src/mongo/shell/utils.js:706:15
@(shellhelp2):1:1
#从节点默认不支持读写操作
> rs.slaveOk()
> show dbs
admin  0.000GB
local  0.000GB
test   0.000GB
> db.school.find()
{ "_id" : ObjectId("5b6d3436dc9a5ab161c121e1"), "name" : "xuaiqi", "date" : ISODate("2018-08-10T06:44:06.284Z") }
> exit
bye
[root@ba3b27d855f6 bin]# 

###########################################################################################################################################

#复制单个数据库
[root@ba3b27d855f6 bin]# ./mongod --slave --source 172.17.0.4:27017 --only test --dbpath /mongodb/mongodb/bin/datas/replace --logpath /mongodb/mongodb/bin/logs/mongodb-replace.log --bind_ip 172.17.0.4 --port 27019 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 153
child process started successfully, parent exiting
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# ps -ef | grep mongodb
root         74      0  0 06:42 ?        00:00:06 ./mongod --master --dbpath /mongodb/mongodb/bin/datas/master --logpath /mongodb/mongodb/bin/logs/mongodb-master.log --bind_ip 172.17.0.4 --port 27017 --fork
root        104      0  0 06:43 ?        00:00:06 ./mongod --slave --source 172.17.0.4:27017 --dbpath /mongodb/mongodb/bin/datas/slave --logpath /mongodb/mongodb/bin/logs/mongodb-slave.log --bind_ip 172.17.0.4 --port 27018 --fork
root        153      0  7 06:55 ?        00:00:00 ./mongod --slave --source 172.17.0.4:27017 --only test --dbpath /mongodb/mongodb/bin/datas/replace --logpath /mongodb/mongodb/bin/logs/mongodb-replace.log --bind_ip 172.17.0.4 --port 27019 --fork
root        182     26  0 06:55 ?        00:00:00 grep --color=auto mongodb
#验证数据
[root@ba3b27d855f6 bin]# ./mongo 172.17.0.4:27019
MongoDB shell version v3.6.3
connecting to: mongodb://172.17.0.4:27019/test
MongoDB server version: 3.6.3
Server has startup warnings: 
2018-08-10T06:55:47.033+0000 I CONTROL  [initandlisten] 
2018-08-10T06:55:47.033+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-10T06:55:47.033+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-08-10T06:55:47.033+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-08-10T06:55:47.033+0000 I CONTROL  [initandlisten] 
2018-08-10T06:55:47.034+0000 I CONTROL  [initandlisten] 
2018-08-10T06:55:47.034+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-08-10T06:55:47.034+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-08-10T06:55:47.034+0000 I CONTROL  [initandlisten] 
2018-08-10T06:55:47.034+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-08-10T06:55:47.034+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-08-10T06:55:47.034+0000 I CONTROL  [initandlisten] 
2018-08-10T06:55:47.034+0000 I CONTROL  [initandlisten] ** WARNING: This node was started in master-slave replication mode.
2018-08-10T06:55:47.034+0000 I CONTROL  [initandlisten] **          Master-slave replication is deprecated and subject to be removed 
2018-08-10T06:55:47.034+0000 I CONTROL  [initandlisten] **          in a future version.
2018-08-10T06:55:47.034+0000 I CONTROL  [initandlisten] 
> show dbs
2018-08-10T06:57:04.298+0000 E QUERY    [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:816:19
shellHelper@src/mongo/shell/utils.js:706:15
@(shellhelp2):1:1
> rs.slaveOk()
> show dbs
local  0.000GB
test   0.000GB
> db.school.find()
{ "_id" : ObjectId("5b6d3436dc9a5ab161c121e1"), "name" : "xuaiqi", "date" : ISODate("2018-08-10T06:44:06.284Z") }
> rs.status()
{
"ok" : 0,
"errmsg" : "not running with --replSet",
"code" : 76,
"codeName" : "NoReplicationEnabled"
}
> db.school.insert({"name" : "dddd"})
WriteResult({ "writeError" : { "code" : 10107, "errmsg" : "not master" } })
> exit
bye

###########################################################################################################################################

#从主从模式切换到副本集模式(停机操作)

#查看mongo进程并杀掉
[root@ba3b27d855f6 bin]# ps -ef | grep mongo
root         74      0  0 06:42 ?        00:00:10 ./mongod --master --dbpath /mongodb/mongodb/bin/datas/master --logpath /mongodb/mongodb/bin/logs/mongodb-master.log --bind_ip 172.17.0.4 --port 27017 --fork
root        104      0  0 06:43 ?        00:00:10 ./mongod --slave --source 172.17.0.4:27017 --dbpath /mongodb/mongodb/bin/datas/slave --logpath /mongodb/mongodb/bin/logs/mongodb-slave.log --bind_ip 172.17.0.4 --port 27018 --fork
root        153      0  0 06:55 ?        00:00:04 ./mongod --slave --source 172.17.0.4:27017 --only test --dbpath /mongodb/mongodb/bin/datas/replace --logpath /mongodb/mongodb/bin/logs/mongodb-replace.log --bind_ip 172.17.0.4 --port 27019 --fork
root        196     26  0 07:03 ?        00:00:00 grep --color=auto mongo
 
[root@ba3b27d855f6 bin]# kill -9 74
[root@ba3b27d855f6 bin]# kill -9 104
[root@ba3b27d855f6 bin]# kill -9 153
#以replSet启动mongo进程
[root@ba3b27d855f6 bin]# ./mongod --replSet test  --dbpath /mongodb/mongodb/bin/datas/master --logpath /mongodb/mongodb/bin/logs/mongodb-master.log --bind_ip 172.17.0.4 --port 27017 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 199
child process started successfully, parent exiting
[root@ba3b27d855f6 bin]# ps -ef | grep mongo
root        199      0 17 07:04 ?        00:00:01 ./mongod --replSet test --dbpath /mongodb/mongodb/bin/datas/master --logpath /mongodb/mongodb/bin/logs/mongodb-master.log --bind_ip 172.17.0.4 --port 27017 --fork
root        232     26  0 07:04 ?        00:00:00 grep --color=auto mongo
#replSet无法与fastsync共用
[root@ba3b27d855f6 bin]# ./mongod --replSet test --fastsync --dbpath /mongodb/mongodb/bin/datas/master --logpath /mongodb/mongodb/bin/logs/mongodb-master.log --bind_ip 172.17.0.4 --port 27018 --fork
BadValue: --fastsync must only be used with --slave
[root@ba3b27d855f6 bin]# ./mongod --replSet test --dbpath /mongodb/mongodb/bin/datas/slave --logpath /mongodb/mongodb/bin/logs/mongodb-slave.log --bind_ip 172.17.0.4 --port 27018 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 240
child process started successfully, parent exiting
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# ./mongod --replSet test --dbpath /mongodb/mongodb/bin/datas/replace --logpath /mongodb/mongodb/bin/logs/mongodb-replace.log --bind_ip 172.17.0.4 --port 27019 --fork 
about to fork child process, waiting until server is ready for connections.
forked process: 272
ERROR: child process failed, exited with error number 62
To see additional information in this output, start without the "--fork" option.
[root@ba3b27d855f6 bin]# ./mongod --replSet test --dbpath /mongodb/mongodb/bin/datas/replace --logpath /mongodb/mongodb/bin/logs/mongodb-replace.log --bind_ip 172.17.0.4 --port 27019      
2018-08-10T07:13:13.877+0000 I CONTROL  [main] log file "/mongodb/mongodb/bin/logs/mongodb-replace.log" exists; moved to "/mongodb/mongodb/bin/logs/mongodb-replace.log.2018-08-10T07-13-13".
[root@ba3b27d855f6 bin]# vim /mongodb/mongodb/bin/logs/mongodb-replace.log
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# ./mongod --replSet test --dbpath /mongodb/mongodb/bin/datas/replace --logpath /mongodb/mongodb/bin/logs/mongodb-replace.log --bind_ip 172.17.0.4 --port 27019 --fork^C
[root@ba3b27d855f6 bin]# rm -rf /mongodb/mongodb/bin/datas/replace/*
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# ./mongod --replSet test --dbpath /mongodb/mongodb/bin/datas/replace --logpath /mongodb/mongodb/bin/logs/mongodb-replace.log --bind_ip 172.17.0.4 --port 27019  --fork
about to fork child process, waiting until server is ready for connections.
forked process: 336
child process started successfully, parent exiting
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]# ps -ef | grep mongodb 
root        199      0  1 07:04 ?        00:00:06 ./mongod --replSet test --dbpath /mongodb/mongodb/bin/datas/master --logpath /mongodb/mongodb/bin/logs/mongodb-master.log --bind_ip 172.17.0.4 --port 27017 --fork
root        240      0  1 07:12 ?        00:00:02 ./mongod --replSet test --dbpath /mongodb/mongodb/bin/datas/slave --logpath /mongodb/mongodb/bin/logs/mongodb-slave.log --bind_ip 172.17.0.4 --port 27018 --fork
root        336      0  5 07:14 ?        00:00:00 ./mongod --replSet test --dbpath /mongodb/mongodb/bin/datas/replace --logpath /mongodb/mongodb/bin/logs/mongodb-replace.log --bind_ip 172.17.0.4 --port 27019 --fork
root        363     26  0 07:15 ?        00:00:00 grep --color=auto mongodb
[root@ba3b27d855f6 bin]# 
[root@ba3b27d855f6 bin]#
#登录其中一个节点,创建config并加载
[root@ba3b27d855f6 bin]# ./mongo 172.17.0.4:27017
MongoDB shell version v3.6.3
connecting to: mongodb://172.17.0.4:27017/test
MongoDB server version: 3.6.3
Server has startup warnings: 
2018-08-10T07:04:44.879+0000 I CONTROL  [initandlisten] 
2018-08-10T07:04:44.879+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-10T07:04:44.879+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-08-10T07:04:44.879+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-08-10T07:04:44.879+0000 I CONTROL  [initandlisten] 
2018-08-10T07:04:44.880+0000 I CONTROL  [initandlisten] 
2018-08-10T07:04:44.880+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-08-10T07:04:44.880+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-08-10T07:04:44.880+0000 I CONTROL  [initandlisten] 
2018-08-10T07:04:44.880+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-08-10T07:04:44.880+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-08-10T07:04:44.880+0000 I CONTROL  [initandlisten] 
> rs.config()
2018-08-10T07:15:24.809+0000 E QUERY    [thread1] Error: Could not retrieve replica set config: {
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized",
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
rs.conf@src/mongo/shell/utils.js:1323:11
@(shell):1:1
> rs.add("172.17.0.4:27018") #集群没有生效之前,无法使用add增加备份节点
assert failed : no config object retrievable from local.system.replset
doassert@src/mongo/shell/assert.js:18:14
assert@src/mongo/shell/assert.js:35:5
rs.add@src/mongo/shell/utils.js:1277:5
@(shell):1:1
2018-08-10T07:16:08.749+0000 E QUERY    [thread1] Error: assert failed : no config object retrievable from local.system.replset :
doassert@src/mongo/shell/assert.js:18:14
assert@src/mongo/shell/assert.js:35:5
rs.add@src/mongo/shell/utils.js:1277:5
@(shell):1:1
> config = {
... "_id" : "test",
... "members" : [
... {"_id" : 0, "host" : "172.17.0.4:27017"},
... {"_id" : 1, "host" : "172.17.0.4:27018"},
... {"_id" : 2, "host" : "172.17.0.4:27019"}
... ]
... }
{
"_id" : "test",
"members" : [
{
"_id" : 0,
"host" : "172.17.0.4:27017"
},
{
"_id" : 1,
"host" : "172.17.0.4:27018"
},
{
"_id" : 2,
"host" : "172.17.0.4:27019"
}
]
}
> rs.initiate(config)
{
"ok" : 1,
"operationTime" : Timestamp(1533885822, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1533885822, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
test:SECONDARY> 
test:SECONDARY> 
test:SECONDARY> rs.status()
{
"set" : "test",
"date" : ISODate("2018-08-10T07:23:51.350Z"),
"myState" : 2,
"term" : NumberLong(0),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"appliedOpTime" : {
"ts" : Timestamp(1533885822, 1),
"t" : NumberLong(-1)
},
"durableOpTime" : {
"ts" : Timestamp(1533885822, 1),
"t" : NumberLong(-1)
}
},
"members" : [
{
"_id" : 0,
"name" : "172.17.0.4:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1148,
"optime" : {
"ts" : Timestamp(1533885822, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2018-08-10T07:23:42Z"),
"infoMessage" : "could not find member to sync from",
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "172.17.0.4:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 8,
"optime" : {
"ts" : Timestamp(1533885822, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1533885822, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2018-08-10T07:23:42Z"),
"optimeDurableDate" : ISODate("2018-08-10T07:23:42Z"),
"lastHeartbeat" : ISODate("2018-08-10T07:23:47.559Z"),
"lastHeartbeatRecv" : ISODate("2018-08-10T07:23:49.776Z"),
"pingMs" : NumberLong(1),
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.17.0.4:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 8,
"optime" : {
"ts" : Timestamp(1533885822, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1533885822, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2018-08-10T07:23:42Z"),
"optimeDurableDate" : ISODate("2018-08-10T07:23:42Z"),
"lastHeartbeat" : ISODate("2018-08-10T07:23:47.559Z"),
"lastHeartbeatRecv" : ISODate("2018-08-10T07:23:49.782Z"),
"pingMs" : NumberLong(0),
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1533885822, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1533885822, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
test:SECONDARY> 
#此时选举还没有结束,还没有确定哪个节点是主节点
test:PRIMARY> 
test:PRIMARY> 
test:PRIMARY> 
test:PRIMARY> rs.status()
{
"set" : "test",
"date" : ISODate("2018-08-10T07:24:08.031Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1533885834, 5),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1533885834, 5),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1533885834, 5),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1533885834, 5),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "172.17.0.4:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1165,
"optime" : {
"ts" : Timestamp(1533885834, 5),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-08-10T07:23:54Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1533885832, 1),
"electionDate" : ISODate("2018-08-10T07:23:52Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "172.17.0.4:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 25,
"optime" : {
"ts" : Timestamp(1533885834, 5),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1533885834, 5),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-08-10T07:23:54Z"),
"optimeDurableDate" : ISODate("2018-08-10T07:23:54Z"),
"lastHeartbeat" : ISODate("2018-08-10T07:24:06.917Z"),
"lastHeartbeatRecv" : ISODate("2018-08-10T07:24:07.788Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "172.17.0.4:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.17.0.4:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 25,
"optime" : {
"ts" : Timestamp(1533885834, 5),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1533885834, 5),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-08-10T07:23:54Z"),
"optimeDurableDate" : ISODate("2018-08-10T07:23:54Z"),
"lastHeartbeat" : ISODate("2018-08-10T07:24:06.916Z"),
"lastHeartbeatRecv" : ISODate("2018-08-10T07:24:07.795Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "172.17.0.4:27017",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1533885834, 5),
"$clusterTime" : {
"clusterTime" : Timestamp(1533885834, 5),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
test:PRIMARY> 
test:PRIMARY> //验证数据
test:PRIMARY> db.school.find()
{ "_id" : ObjectId("5b6d3436dc9a5ab161c121e1"), "name" : "xuaiqi", "date" : ISODate("2018-08-10T06:44:06.284Z") }
{ "_id" : ObjectId("5b6d37ce1a42350d380cb142"), "nme" : "ddddd" }
test:PRIMARY> 
test:PRIMARY> 
test:PRIMARY> 
test:PRIMARY> exit
bye
[root@ba3b27d855f6 bin]# 
上一篇:Beautiful Soup 中文文档


下一篇:Candies POJ - 3159 (最短路+差分约束)