Mongodb主从手动切换
程序员文章站
2022-06-29 08:58:19
...
#方法1 调整权重值(靠谱)
#提升SECONDARY的权重值大于PRIMARY,数值越大,优先级越高
#目前三台机器的权重都为: "priority" : 1
#必须在PRIMARY节点上执行
PRIMARY> config=rs.conf() //查看当前配置,存入config变量中。
PRIMARY> config.members[2].priority = 3 //修改config变量,第三组成员的优先级为3. [id]值
PRIMARY> rs.reconfig(config) //配置生效
#方法2 降级(有可能重新选举主节点)
PRIMARY> rs.stepDown() //rs.stepDown(30) 单位:S
这个命令会让primary降级为Secondary节点,并维持60s,如果这段时间内没有新的primary被选举出来,这个节点可以要求重新进行选举。
#方法3 关闭主节点 让从节点自动切换到主(也可以,但是有些笨拙)
> use admin; switched to db admin
> db.shutdownServer();
server should be down... 2018-07-20T23:06:30.230+0800 I NETWORK [initandlisten] waiting for connections on port 27017 2018-07-20T23:13:37.028+0800 I NETWORK [listener] connection accepted from 127.0.0.1:56738 #1 (1 connection now open) 2018-07-20T23:13:37.029+0800 I NETWORK [conn1] received client metadata from 127.0.0.1:56738 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.0.0" }, os: { type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "18.04" } } 2018-07-20T23:13:59.951+0800 I COMMAND [conn1] terminating, shutdown command received { shutdown: 1.0, $db: "admin" } 2018-07-20T23:13:59.951+0800 I NETWORK [conn1] shutdown: going to close listening sockets... 2018-07-20T23:13:59.951+0800 I NETWORK [conn1] removing socket file: /tmp/mongodb-27017.sock 2018-07-20T23:13:59.952+0800 I FTDC [conn1] Shutting down full-time diagnostic data capture 2018-07-20T23:13:59.956+0800 I STORAGE [conn1] WiredTigerKVEngine shutting down 2018-07-20T23:14:00.049+0800 I STORAGE [conn1] shutdown: removing fs lock... 2018-07-20T23:14:00.049+0800 I CONTROL [conn1] now exiting 2018-07-20T23:14:00.049+0800 I CONTROL [conn1] shutting down with code:0