docker pull mongo
複製代碼
rs是副本集名稱,m0是主庫,另外兩個從庫,主庫能讀寫,從庫只讀docker
docker run --name m0 -p 27017:27017 -d mongo --replSet "rs"
docker run --name m1 -p 37017:27017 -d mongo --replSet "rs"
docker run --name m2 -p 47017:27017 -d mongo --replSet "rs"
複製代碼
docker exec -it m0 bash
複製代碼
mongo
rs.slaveOk();
show dbs;
use admin
var config={_id:"rs",members:[{_id:0,host:"172.17.0.7:27017"},{_id:1,host:"172.17.0.7:37017"},{_id:2,host:"172.17.0.7:47017"}]};
rs.initiate(config)
use admin
db.createUser( { user: "root", pwd: "root", roles: [ { role: "root", db: "admin" } ] } );
複製代碼