windows单机版mongo4搭建教程参考:windows安装mongo4单机版
mongo-1(主节点):192.168.16.197:27010
mongo-2(副本集):192.168.16.197:27011
mongo-3(仲裁点):192.168.16.197:27012
切换到mongo安装路径的\bin\目录,打开cmd分别执行以下命令:
mongod --replSet mongo --bind_ip 192.168.16.197 --port 27010 --dbpath "C:\Program Files\MongoDB\Server\4.0\data\db1" --logpath "C:\Program Files\MongoDB\Server\4.0\log\db1.log"
mongod --replSet mongo --bind_ip 192.168.16.197 --port 27011 --dbpath "C:\Program Files\MongoDB\Server\4.0\data\db2" --logpath "C:\Program Files\MongoDB\Server\4.0\log\db2.log"
mongod --replSet mongo --bind_ip 192.168.16.197 --port 27012 --dbpath "C:\Program Files\MongoDB\Server\4.0\data\db3" --logpath "C:\Program Files\MongoDB\Server\4.0\log\db3.log"
mongo --host 192.168.16.197 --port 27010
#查看状态
rs.status()
#配置参数(右键cmd窗口,可开启插入模式)
config = {_id:"mongo",members:[{_id:0,host:"192.168.16.197:27010",priority:2},{_id:1,host:"192.168.16.197:27011",priority:1},{_id:2,host:"192.168.16.197:27012",arbiterOnly:true}]}
#初始化
rs.initiate(config)
# 查看副本集配置信息
rs.conf()
show dbs,查看原有的数据库(集合)是否正常
数据库和集合正常,单机版改为副本集成功!
注册教程参考:使用nssm注册windows服务
命令参数如下:
--replSet mongo --bind_ip 192.168.16.197 --port 27010 --dbpath "C:\Program Files\MongoDB\Server\4.0\data\db1" --logpath "C:\Program Files\MongoDB\Server\4.0\log\db1.log"
--replSet mongo --bind_ip 192.168.16.197 --port 27011 --dbpath "C:\Program Files\MongoDB\Server\4.0\data\db2" --logpath "C:\Program Files\MongoDB\Server\4.0\log\db2.log"
--replSet mongo --bind_ip 192.168.16.197 --port 27012 --dbpath "C:\Program Files\MongoDB\Server\4.0\data\db3" --logpath "C:\Program Files\MongoDB\Server\4.0\log\db3.log"
注册完毕,启动服务即可!