AI智能
改变未来

mogodb replication set复制集

replication set复制集

简要命令

replication set复制集replicattion set 多台服务器维护相同的数据副本,提高服务器的可用性.Replication set 设置全过程0:创建目录mkdir -p /data/r0 /data/r1 /data/r21:启动3个实例,且声明实例属于某复制集./bin/mongod --port 27017 --dbpath /data/r0 --smallfiles --replSet rsa --fork --logpath /var/log/mongo17.log./bin/mongod --port 27018 --dbpath /data/r1 --smallfiles --replSet rsa --fork --logpath /var/log/mongo18.log./bin/mongod --port 27019 --dbpath /data/r2 --smallfiles --replSet rsa --fork --logpath /var/log/mongo19.log2:配置rsconf = {_id:\'rsa\',members:[{_id:0,host:\'192.168.1.201:27017\'}]}3: 根据配置做初始化rs.initiate(rsconf);4: 添加节点rs.add(\'192.168.1.201:27018\');rs.add(\'192.168.1.201:27019\');5:查看状态rs.status();6:删除节点rs.remove(\'192.168.1.201:27019\');7:主节点插入数据>use test>db.user.insert({uid:1,name:\'lily\'});8:连接secondary查询同步情况./bin/mongo --port 27019>use test>show tablesrsa:SECONDARY> show tables;Sat Aug 17 16:03:55.786 JavaScript execution failed: error: { \"$err\" : \"not master and slaveOk=false\", \"code\" : 13435 }8.1 出现上述错误,是因为slave默认不许读写>rs.slaveOk();>show tables#看到与primary 一致的数据

详细过程

0:创建目录

mkdir -p /data/r0 /data/r1 /data/r2[mongod@mcw01 ~]$ ps -ef|grep mongoroot      16595  16566  0 10:57 pts/0    00:00:00 su - mongodmongod    16596  16595  0 10:57 pts/0    00:00:02 -bashmongod    16765      1  1 13:28 ?        00:02:13 mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --forkmongod    17150  16596  0 16:05 pts/0    00:00:00 ps -efmongod    17151  16596  0 16:05 pts/0    00:00:00 grep --color=auto mongo[mongod@mcw01 ~]$ kill -2 16765[mongod@mcw01 ~]$ ps -ef|grep mongoroot      16595  16566  0 10:57 pts/0    00:00:00 su - mongodmongod    16596  16595  0 10:57 pts/0    00:00:02 -bashmongod    17153  16596  0 16:05 pts/0    00:00:00 ps -efmongod    17154  16596  0 16:05 pts/0    00:00:00 grep --color=auto mongo[mongod@mcw01 ~]$ ls /mongodb/bin  conf  data  log[mongod@mcw01 ~]$ mv /mongodb/* /tmp/[mongod@mcw01 ~]$ mkdir /mongodb/m1{7..9}[mongod@mcw01 ~]$ ls /mongodb/m17  m18  m19[mongod@mcw01 ~]$ mkdir /mongodb/mlog[mongod@mcw01 ~]$ ls /mongodb/m17  m18  m19  mlog[mongod@mcw01 ~]$[mongod@mcw01 ~]$ mongod #刚刚把命令都移走了-bash: /mongodb/bin/mongod: No such file or directory[mongod@mcw01 ~]$ cp -a /tmp/bin/  /mongodb/  #把命令再移动回来[mongod@mcw01 ~]$ ls /mongodb/bin  m17  m18  m19  mlog

1:启动3个实例,且声明实例属于某复制集

./bin/mongod --port 27017 --dbpath /data/r0 --smallfiles --replSet rsa --fork --logpath /var/log/mongo17.log./bin/mongod --port 27018 --dbpath /data/r1 --smallfiles --replSet rsa --fork --logpath /var/log/mongo18.log./bin/mongod --port 27019 --dbpath /data/r2 --smallfiles --replSet rsa --fork --logpath /var/log/mongo19.log这里是测试,为了启动快才添加--smallfiles 的吧。如果实际使用的时候,应该不需要加这个参数。三个都指定同一个复制集,起名为rs2.[mongod@mcw01 ~]$ mongod --dbpath /mongodb/m17 --logpath /mongodb/mlog/m17.log --fork --port 27017 --replSet rs2 --smallfilesabout to fork child process, waiting until server is ready for connections.forked process: 17206child process started successfully, parent exiting[mongod@mcw01 ~]$ mongod --dbpath /mongodb/m18 --logpath /mongodb/mlog/m18.log --fork --port 27018 --replSet rs2 --smallfilesabout to fork child process, waiting until server is ready for connections.forked process: 17234child process started successfully, parent exiting[mongod@mcw01 ~]$ mongod --dbpath /mongodb/m19 --logpath /mongodb/mlog/m19.log --fork --port 27019 --replSet rs2 --smallfilesabout to fork child process, waiting until server is ready for connections.forked process: 17257child process started successfully, parent exiting[mongod@mcw01 ~]$[mongod@mcw01 ~]$ ps -ef|grep -v grep |grep  dbpath  #查看,已经启动三个进程,都是同一个复制集mongod    17206      1  1 16:15 ?        00:00:04 mongod --dbpath /mongodb/m17 --logpath /mongodb/mlog/m17.log --fork --port 27017 --replSet rs2 --smallfilesmongod    17234      1  1 16:18 ?        00:00:01 mongod --dbpath /mongodb/m18 --logpath /mongodb/mlog/m18.log --fork --port 27018 --replSet rs2 --smallfilesmongod    17257      1  2 16:18 ?        00:00:01 mongod --dbpath /mongodb/m19 --logpath /mongodb/mlog/m19.log --fork --port 27019 --replSet rs2 --smallfiles[mongod@mcw01 ~]$

2:配置

rsconf = {_id:\'rsa\', #复制集名称members: #有哪些成员[{_id:0,host:\'192.168.1.201:27017\'}]}var rsconf = {_id:\'rs2\',members:[{_id:0,host:\'10.0.0.11:27017\'},{_id:1,host:\'10.0.0.11:27018\'},{_id:2,host:\'10.0.0.11:27019\'}]}随便找一个节点进入,[mongod@mcw01 ~]$ mongo --port 27017.......> #生成复制集配置变量> var rsconf = {...     _id:\'rs2\',...     members:...       [...         {_id:0,...         host:\'10.0.0.11:27017\'...         },... {_id:1,...         host:\'10.0.0.11:27018\'...         },... {_id:2,...         host:\'10.0.0.11:27019\'...         }...   ]... }> printjson(rsconf)  #打印一下变量{\"_id\" : \"rs2\",\"members\" : [{\"_id\" : 0,\"host\" : \"10.0.0.11:27017\"},{\"_id\" : 1,\"host\" : \"10.0.0.11:27018\"},{\"_id\" : 2,\"host\" : \"10.0.0.11:27019\"}]}>

3: 根据配置做初始化

rs.initiate(rsconf);> #根据生成的复制集配置变量,做初始化。初始化完后直接进入不同的命令行前面的信息中rs2副本名称:other>> rs.initiate(rsconf);{ \"ok\" : 1 }rs2:OTHER>rs2:OTHER> rs.status();  #执行命令查看复制集状态,{\"set\" : \"rs2\",  #复制集名称\"date\" : ISODate(\"2022-03-04T08:31:33.312Z\"),\"myState\" : 1, #状态\"term\" : NumberLong(1),\"heartbeatIntervalMillis\" : NumberLong(2000),\"members\" : [  #有哪些成员{\"_id\" : 0,\"name\" : \"10.0.0.11:27017\",\"health\" : 1,\"state\" : 1,\"stateStr\" : \"PRIMARY\",  #这个成员是主,其它两个不是,是SECONDARY\"uptime\" : 976,\"optime\" : {\"ts\" : Timestamp(1646382581, 2),\"t\" : NumberLong(1)},\"optimeDate\" : ISODate(\"2022-03-04T08:29:41Z\"),\"infoMessage\" : \"could not find member to sync from\",\"electionTime\" : Timestamp(1646382581, 1),\"electionDate\" : ISODate(\"2022-03-04T08:29:41Z\"),\"configVersion\" : 1,\"self\" : true},{\"_id\" : 1,\"name\" : \"10.0.0.11:27018\",\"health\" : 1,\"state\" : 2,\"stateStr\" : \"SECONDARY\",\"uptime\" : 123,\"optime\" : {\"ts\" : Timestamp(1646382581, 2),\"t\" : NumberLong(1)},\"optimeDate\" : ISODate(\"2022-03-04T08:29:41Z\"),\"lastHeartbeat\" : ISODate(\"2022-03-04T08:31:33.193Z\"),\"lastHeartbeatRecv\" : ISODate(\"2022-03-04T08:31:31.611Z\"),\"pingMs\" : NumberLong(0),\"syncingTo\" : \"10.0.0.11:27017\",\"configVersion\" : 1},{\"_id\" : 2,\"name\" : \"10.0.0.11:27019\",\"health\" : 1,\"state\" : 2,\"stateStr\" : \"SECONDARY\",\"uptime\" : 123,\"optime\" : {\"ts\" : Timestamp(1646382581, 2),\"t\" : NumberLong(1)},\"optimeDate\" : ISODate(\"2022-03-04T08:29:41Z\"),\"lastHeartbeat\" : ISODate(\"2022-03-04T08:31:33.193Z\"),\"lastHeartbeatRecv\" : ISODate(\"2022-03-04T08:31:31.663Z\"),\"pingMs\" : NumberLong(0),\"syncingTo\" : \"10.0.0.11:27018\",\"configVersion\" : 1}],\"ok\" : 1}rs2:PRIMARY>  #执行完查看状态的命令后,这里就变成了:复制集名称:主>我们推出来进入备的[mongod@mcw01 ~]$ mongo --port 27018......rs2:SECONDARY> #可以看到这里是复制集名称:SECONDARY>这样就已经配置好复制集了

4: 添加节点

rs.add(\'192.168.1.201:27018\');rs.add(\'192.168.1.201:27019\');根据后面删除的方法,已经删除了两个结点,只剩一个节点了。下面我们再将节点添加上rs2:PRIMARY> rs.remove(\'10.0.0.11:27018\'){ \"ok\" : 1 }rs2:PRIMARY> rs.status();{\"set\" : \"rs2\",\"date\" : ISODate(\"2022-03-04T08:43:55.005Z\"),\"myState\" : 1,\"term\" : NumberLong(1),\"heartbeatIntervalMillis\" : NumberLong(2000),\"members\" : [{\"_id\" : 0,\"name\" : \"10.0.0.11:27017\",\"health\" : 1,\"state\" : 1,\"stateStr\" : \"PRIMARY\",\"uptime\" : 1718,\"optime\" : {\"ts\" : Timestamp(1646383431, 1),\"t\" : NumberLong(1)},\"optimeDate\" : ISODate(\"2022-03-04T08:43:51Z\"),\"electionTime\" : Timestamp(1646382581, 1),\"electionDate\" : ISODate(\"2022-03-04T08:29:41Z\"),\"configVersion\" : 3,\"self\" : true}],\"ok\" : 1}rs2:PRIMARY>rs2:PRIMARY> rs.add(\'10.0.0.11:27018\')   #重新添加成员{ \"ok\" : 1 }rs2:PRIMARY> rs.add(\'10.0.0.11:27019\'){ \"ok\" : 1 }rs2:PRIMARY> rs.status();{\"set\" : \"rs2\",\"date\" : ISODate(\"2022-03-04T08:46:07.840Z\"),\"myState\" : 1,\"term\" : NumberLong(1),\"heartbeatIntervalMillis\" : NumberLong(2000),\"members\" : [{\"_id\" : 0,\"name\" : \"10.0.0.11:27017\",\"health\" : 1,\"state\" : 1,\"stateStr\" : \"PRIMARY\",\"uptime\" : 1850,\"optime\" : {\"ts\" : Timestamp(1646383557, 1),\"t\" : NumberLong(1)},\"optimeDate\" : ISODate(\"2022-03-04T08:45:57Z\"),\"electionTime\" : Timestamp(1646382581, 1),\"electionDate\" : ISODate(\"2022-03-04T08:29:41Z\"),\"configVersion\" : 5,\"self\" : true},{\"_id\" : 1,\"name\" : \"10.0.0.11:27018\",\"health\" : 1,\"state\" : 2,\"stateStr\" : \"SECONDARY\",\"uptime\" : 23,\"optime\" : {\"ts\" : Timestamp(1646383557, 1),\"t\" : NumberLong(1)},\"optimeDate\" : ISODate(\"2022-03-04T08:45:57Z\"),\"lastHeartbeat\" : ISODate(\"2022-03-04T08:46:06.067Z\"),\"lastHeartbeatRecv\" : ISODate(\"2022-03-04T08:46:06.914Z\"),\"pingMs\" : NumberLong(0),\"syncingTo\" : \"10.0.0.11:27017\",\"configVersion\" : 5},{\"_id\" : 2,\"name\" : \"10.0.0.11:27019\",\"health\" : 1,\"state\" : 2,\"stateStr\" : \"SECONDARY\",\"uptime\" : 7,\"optime\" : {\"ts\" : Timestamp(1646383557, 1),\"t\" : NumberLong(1)},\"optimeDate\" : ISODate(\"2022-03-04T08:45:57Z\"),\"lastHeartbeat\" : ISODate(\"2022-03-04T08:46:06.071Z\"),\"lastHeartbeatRecv\" : ISODate(\"2022-03-04T08:46:06.066Z\"),\"pingMs\" : NumberLong(0),\"syncingTo\" : \"10.0.0.11:27018\",\"configVersion\" : 5}],\"ok\" : 1}rs2:PRIMARY>

5:查看状态

rs.status();rs2:OTHER> rs.status();  #执行命令查看复制集状态,{\"set\" : \"rs2\",  #复制集名称\"date\" : ISODate(\"2022-03-04T08:31:33.312Z\"),\"myState\" : 1, #状态\"term\" : NumberLong(1),\"heartbeatIntervalMillis\" : NumberLong(2000),\"members\" : [  #有哪些成员{\"_id\" : 0,\"name\" : \"10.0.0.11:27017\",\"health\" : 1,\"state\" : 1,\"stateStr\" : \"PRIMARY\",  #这个成员是主,其它两个不是,是SECONDARY\"uptime\" : 976,\"optime\" : {\"ts\" : Timestamp(1646382581, 2),\"t\" : NumberLong(1)},\"optimeDate\" : ISODate(\"2022-03-04T08:29:41Z\"),\"infoMessage\" : \"could not find member to sync from\",\"electionTime\" : Timestamp(1646382581, 1),\"electionDate\" : ISODate(\"2022-03-04T08:29:41Z\"),\"configVersion\" : 1,\"self\" : true},{\"_id\" : 1,\"name\" : \"10.0.0.11:27018\",\"health\" : 1,\"state\" : 2,\"stateStr\" : \"SECONDARY\",\"uptime\" : 123,\"optime\" : {\"ts\" : Timestamp(1646382581, 2),\"t\" : NumberLong(1)},\"optimeDate\" : ISODate(\"2022-03-04T08:29:41Z\"),\"lastHeartbeat\" : ISODate(\"2022-03-04T08:31:33.193Z\"),\"lastHeartbeatRecv\" : ISODate(\"2022-03-04T08:31:31.611Z\"),\"pingMs\" : NumberLong(0),\"syncingTo\" : \"10.0.0.11:27017\",\"configVersion\" : 1},{\"_id\" : 2,\"name\" : \"10.0.0.11:27019\",\"health\" : 1,\"state\" : 2,\"stateStr\" : \"SECONDARY\",\"uptime\" : 123,\"optime\" : {\"ts\" : Timestamp(1646382581, 2),\"t\" : NumberLong(1)},\"optimeDate\" : ISODate(\"2022-03-04T08:29:41Z\"),\"lastHeartbeat\" : ISODate(\"2022-03-04T08:31:33.193Z\"),\"lastHeartbeatRecv\" : ISODate(\"2022-03-04T08:31:31.663Z\"),\"pingMs\" : NumberLong(0),\"syncingTo\" : \"10.0.0.11:27018\",\"configVersion\" : 1}],\"ok\" : 1}

6:删除节点

rs.remove(\'192.168.1.201:27019\');rs2:PRIMARY> rs.remove(\'10.0.0.11:27019\'){ \"ok\" : 1 }rs2:PRIMARY> rs.status();{\"set\" : \"rs2\",\"date\" : ISODate(\"2022-03-04T08:40:48.604Z\"),\"myState\" : 1,\"term\" : NumberLong(1),\"heartbeatIntervalMillis\" : NumberLong(2000),\"members\" : [{\"_id\" : 0,\"name\" : \"10.0.0.11:27017\",\"health\" : 1,\"state\" : 1,\"stateStr\" : \"PRIMARY\",\"uptime\" : 1531,\"optime\" : {\"ts\" : Timestamp(1646383235, 1),\"t\" : NumberLong(1)},\"optimeDate\" : ISODate(\"2022-03-04T08:40:35Z\"),\"electionTime\" : Timestamp(1646382581, 1),\"electionDate\" : ISODate(\"2022-03-04T08:29:41Z\"),\"configVersion\" : 2,\"self\" : true},{\"_id\" : 1,\"name\" : \"10.0.0.11:27018\",\"health\" : 1,\"state\" : 2,\"stateStr\" : \"SECONDARY\",\"uptime\" : 679,\"optime\" : {\"ts\" : Timestamp(1646383235, 1),\"t\" : NumberLong(1)},\"optimeDate\" : ISODate(\"2022-03-04T08:40:35Z\"),\"lastHeartbeat\" : ISODate(\"2022-03-04T08:40:47.168Z\"),\"lastHeartbeatRecv\" : ISODate(\"2022-03-04T08:40:47.188Z\"),\"pingMs\" : NumberLong(0),\"syncingTo\" : \"10.0.0.11:27017\",\"configVersion\" : 2}],\"ok\" : 1}rs2:PRIMARY>

7:主节点插入数据

>use test
>db.user.insert({uid:1,name:\’lily\’});

8:连接secondary查询同步情况

./bin/mongo --port 27019>use test>show tablesrsa:SECONDARY> show tables;Sat Aug 17 16:03:55.786 JavaScript execution failed: error: { \"$err\" : \"not master and slaveOk=false\", \"code\" : 13435 }在主上是可以查看的rs2:PRIMARY> show dbs;local  0.000GBrs2:PRIMARY>但是当我们进入从的时候,无法查询,报错不是主[mongod@mcw01 ~]$ mongo --port 27018......rs2:SECONDARY> show dbs;2022-03-04T16:51:53.478+0800 E QUERY    [thread1] Error: listDatabases failed:{ \"ok\" : 0, \"errmsg\" : \"not master and slaveOk=false\", \"code\" : 13435 } :_getErrorWithCode@src/mongo/shell/utils.js:25:13Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1shellHelper.show@src/mongo/shell/utils.js:761:19shellHelper@src/mongo/shell/utils.js:651:15@(shellhelp2):1:1SECONDARY默认不允许读写rs2:SECONDARY>rs2:SECONDARY> rs.slaveOk(); 那我们按错误信息将它设置成ok,这样从就可以读取数据了rs2:SECONDARY> show dbs;local  0.000GBrs2:SECONDARY>在主上插入数据rs2:PRIMARY> use testswitched to db testrs2:PRIMARY> db.user.insert({uid:1,name:\'lily\'});WriteResult({ \"nInserted\" : 1 })rs2:PRIMARY> db.user.find();{ \"_id\" : ObjectId(\"6221d4bfabd9594d8ce84607\"), \"uid\" : 1, \"name\" : \"lily\" }rs2:PRIMARY>我们在从上去查看,数据是同步的。一样的。rs2:SECONDARY> show dbs;local  0.000GBtest   0.000GBrs2:SECONDARY> use test;switched to db testrs2:SECONDARY> show tables;userrs2:SECONDARY> db.user.find();{ \"_id\" : ObjectId(\"6221d4bfabd9594d8ce84607\"), \"uid\" : 1, \"name\" : \"lily\" }rs2:SECONDARY>8.1 出现上述错误,是因为slave默认不许读写>rs.slaveOk();>show tables#看到与primary 一致的数据

9、当我将主上的服务停掉,模拟服务宕了

rs2:PRIMARY> use adminswitched to db adminrs2:PRIMARY> db.shutdownServer()server should be down...2022-03-04T17:02:15.304+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed2022-03-04T17:02:16.387+0800 I NETWORK  [thread1] Socket recv() errno:104 Connection reset by peer 127.0.0.1:270172022-03-04T17:02:16.388+0800 I NETWORK  [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:27017]2022-03-04T17:02:16.388+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed2022-03-04T17:02:16.391+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed2022-03-04T17:02:16.392+0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused2022-03-04T17:02:16.392+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed>然后我们去27018节点上去查看复制集状态。发现27017健康是0。并且主节点已经由27017变为27018了rs2:SECONDARY> rs.status();{\"set\" : \"rs2\",\"date\" : ISODate(\"2022-03-04T09:04:46.442Z\"),\"myState\" : 1,\"term\" : NumberLong(2),\"heartbeatIntervalMillis\" : NumberLong(2000),\"members\" : [{\"_id\" : 0,\"name\" : \"10.0.0.11:27017\",\"health\" : 0,\"state\" : 8,\"stateStr\" : \"(not reachable/healthy)\",\"uptime\" : 0,\"optime\" : {\"ts\" : Timestamp(0, 0),\"t\" : NumberLong(-1)},\"optimeDate\" : ISODate(\"1970-01-01T00:00:00Z\"),\"lastHeartbeat\" : ISODate(\"2022-03-04T09:04:46.304Z\"),\"lastHeartbeatRecv\" : ISODate(\"2022-03-04T09:02:13.762Z\"),\"pingMs\" : NumberLong(0),\"lastHeartbeatMessage\" : \"Connection refused\",\"configVersion\" : -1},{\"_id\" : 1,\"name\" : \"10.0.0.11:27018\",\"health\" : 1,\"state\" : 1,\"stateStr\" : \"PRIMARY\",\"uptime\" : 2769,\"optime\" : {\"ts\" : Timestamp(1646384546, 1),\"t\" : NumberLong(2)},\"optimeDate\" : ISODate(\"2022-03-04T09:02:26Z\"),\"electionTime\" : Timestamp(1646384545, 1),\"electionDate\" : ISODate(\"2022-03-04T09:02:25Z\"),\"configVersion\" : 5,\"self\" : true},{\"_id\" : 2,\"name\" : \"10.0.0.11:27019\",\"health\" : 1,\"state\" : 2,\"stateStr\" : \"SECONDARY\",\"uptime\" : 1123,\"optime\" : {\"ts\" : Timestamp(1646384546, 1),\"t\" : NumberLong(2)},\"optimeDate\" : ISODate(\"2022-03-04T09:02:26Z\"),\"lastHeartbeat\" : ISODate(\"2022-03-04T09:04:46.267Z\"),\"lastHeartbeatRecv\" : ISODate(\"2022-03-04T09:04:45.029Z\"),\"pingMs\" : NumberLong(0),\"syncingTo\" : \"10.0.0.11:27018\",\"configVersion\" : 5}],\"ok\" : 1}rs2:PRIMARY>在当前的主27018上插入数据rs2:PRIMARY> use testswitched to db testrs2:PRIMARY> show tables;userrs2:PRIMARY> db.user.insert({uid:2,name:\'xiaoma\'})WriteResult({ \"nInserted\" : 1 })rs2:PRIMARY> db.user.find(){ \"_id\" : ObjectId(\"6221d4bfabd9594d8ce84607\"), \"uid\" : 1, \"name\" : \"lily\" }{ \"_id\" : ObjectId(\"6221d775ef402350b10ce148\"), \"uid\" : 2, \"name\" : \"xiaoma\" }rs2:PRIMARY>然后去27019上现在这个从上去查看,能看到新的主27018上创建的数据[mongod@mcw01 ~]$ mongo --port 27019.......rs2:SECONDARY> show dbs;2022-03-04T17:11:47.714+0800 E QUERY    [thread1] Error: listDatabases failed:{ \"ok\" : 0, \"errmsg\" : \"not master and slaveOk=false\", \"code\" : 13435 } :_getErrorWithCode@src/mongo/shell/utils.js:25:13Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1shellHelper.show@src/mongo/shell/utils.js:761:19shellHelper@src/mongo/shell/utils.js:651:15@(shellhelp2):1:1rs2:SECONDARY> rs.slaveOk();rs2:SECONDARY> show dbs;local  0.000GBtest   0.000GBrs2:SECONDARY> use test;switched to db testrs2:SECONDARY> show tables;userrs2:SECONDARY> db.user.find();{ \"_id\" : ObjectId(\"6221d4bfabd9594d8ce84607\"), \"uid\" : 1, \"name\" : \"lily\" }{ \"_id\" : ObjectId(\"6221d775ef402350b10ce148\"), \"uid\" : 2, \"name\" : \"xiaoma\" }rs2:SECONDARY>

免交互执行mogodb的命令,可实现程序一键部署

启动3个实例,然后进入某个实例进行初始化就完成单机复制集 集群配置了。[mongod@mcw01 ~]$ mongo --port 27018 <<EOFuse adminvar rsconf = {_id: \'rs2\',members:[{},{},]};printjson(rsconf);<<EOFMongoDB shell version: 3.2.8connecting to: 127.0.0.1:27018/testswitched to db admin{ \"_id\" : \"rs2\", \"members\" : [ { }, { } ] }bye[mongod@mcw01 ~]$

[mongod@mcw01 ~]$ sh 1.sh installabout to fork child process, waiting until server is ready for connections.forked process: 19633child process started successfully, parent exitingabout to fork child process, waiting until server is ready for connections.forked process: 19652child process started successfully, parent exitingabout to fork child process, waiting until server is ready for connections.forked process: 19679child process started successfully, parent exiting[mongod@mcw01 ~]$[mongod@mcw01 ~]$ sh 1.sh replMongoDB shell version: 3.2.8connecting to: 127.0.0.1:27017/testswitched to db admin{ \"ok\" : 1 }bye[mongod@mcw01 ~]$ cat 1.sh#!/bin/bash#复制集配置IP=\'10.0.0.11\'  #主机ipNA=\'rs3\'  #复制集名称if [ \"$1\" = \"reset\" ]thenkill -2 `ps -ef|grep -v grep |grep logpath|awk \'{print $2}\'`sleep 2rm -rf /mongodb/m{17,18,19,log}/*exitfiif [ \"$1\" = \"install\" ]thenmkdir -p /mongodb/m{17,18,19,log}/mongod --dbpath /mongodb/m17 --logpath /mongodb/mlog/m17.log --logappend --fork --port 27017 --replSet ${NA}  --smallfilesmongod --dbpath /mongodb/m18 --logpath /mongodb/mlog/m18.log --logappend --fork --port 27018 --replSet ${NA}  --smallfilesmongod --dbpath /mongodb/m19 --logpath /mongodb/mlog/m19.log --logappend --fork --port 27019 --replSet ${NA}  --smallfilesexitfiif [ \"$1\" = \"repl\" ]thenmongo --port 27017 <<EOFuse admin;var rsconf = {_id:\'${NA}\',members:[{_id:0,host:\'${IP}:27017\'},{_id:1,host:\'${IP}:27018\'},{_id:2,host:\'${IP}:27019\'},]};rs.initiate(rsconf);<<EOFfi[mongod@mcw01 ~]$

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » mogodb replication set复制集