创建docker:
创建docker5000:docker run -d -p 5000:5000 -p 27017:27017 -e MONGO_ONE_NODE=True -e MONGO_ADMIN_PASSWORD=admin mm2:latest 创建docker5001:docker run -d -p 5000:5001 -p 27018:27017 -e MONGO_ONE_NODE=True -e MONGO_ADMIN_PASSWORD=admin mm2:latest
启动mongo服务:
/usr/bin/mongod --bind_ip_all --logpath /var/log/mongodb/mongod.log --replSet rs0 --fork
创建副本集:
/usr/bin/mongo admin --eval "rs.initiate({"_id":"rs0","members":[{"_id":0,"host":"172.17.0.1:27017"},{"_id":1,"host":"172.17.0.1:27018"}]});"
创建角色:
/usr/bin/mongo admin --eval " db.createUser({ user: ‘admin‘, pwd: ‘admin‘, roles: [{role: ‘userAdminAnyDatabase‘, db: ‘admin‘}], mechanisms:[‘SCRAM-SHA-1‘] });"
关闭mongo:
/usr/bin/mongod --shutdown --dbpath=/var/lib/mongodb --logpath=/var/log/mongodb/mongod.log --port=27017 --logappend --fork
主节点:
db.isMaster()
查看副本集:
rs.status()
rs.config()
设置权重:
conf = rs.config() #获取副本集的配置,默认权重都是1 conf.members[0].priority = 10 # 设置权重
为SECONDARY添加查询权限:
rs.slaveOk()
mongo副本集设置主库权重,即使主库宕机了再重启也还是主库:
cfg = rs.conf() cfg.members[0].priority = 10 cfg.members[1].priority = 1 rs.reconfig(cfg)
结束!