ssh root@192.168.1.2
git@github.com:anjingjingan/mongodb-cluster-docker-compose.git
ssh root@192.168.1.3
git@github.com:anjingjingan/mongodb-cluster-docker-compose.git
ssh root@192.168.1.4
git@github.com:anjingjingan/mongodb-cluster-docker-compose.git
cd mongodb-cluster-docker-compose
docker-compose up -d
permissions on /var/mongo-keyfile are too open
#设置只读权限
#Set read-only permissions
chmod -R 400 key
#设置mongodb所属的权限
#Set the permissions to which mongodb belongs
chown -R 999:999 key
#重启
#start
docker-compose up -d
docker exec -it mongo-cluster-1 mongosh admin
rs.initiate({
_id: "rs01",
members: [
{ _id: 0, host: "192.168.1.2:27017" },
{ _id: 1, host: "192.168.1.3:27017" },
{ _id: 2, host: "192.168.1.4:27017" }
]
})
rs.status()
stateStr
属性值为 "PRIMARY" 的是节点,以下账户在主节点设置
The stateStr
attribute value is "PRIMARY" is the node, and the following accounts are set on the primary node
#pwd 设置为你需要的密码
#pwd Set to your desired password
db.createUser(
{
user: "admin",
pwd: "password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
db.auth("admin","password")
db.createRole({
role: "readOnly",
privileges: [
{ resource: { db: "", collection: "" }, actions: [ "find" ] }
],
roles: []
})
db.createUser({
user: "read",
pwd: "Pfcuo4tUMwPE78gh",
roles: [
{ role: "readOnly", db: "admin" }
]
})
db.grantRolesToUser("admin", ["clusterAdmin"])
db.grantRolesToUser("admin", ["clusterManager"])
db.getUser("admin")