mongodb製作副本集 以及使用者名稱密碼認證
阿新 • • 發佈:2018-11-17
192.168.56.11 192.168.56.12 192.168.56.13 三臺機器 1.##基礎配置
mongodb-linux-x86_64-3.4.10.tgz tar xf mongodb-linux-x86_64-3.4.10.tgz mv mongodb-linux-x86_64-3.4.10 /root/mongodb cd /root/mongodb mkdir data logs
2.###mongo配置檔案
[[email protected] ~]# cat /root/mongodb/mongo.conf logpath=/root/mongodb/logs/mongo.log logappend=true port=27018 fork=true dbpath=/root/mongodb/data/ replSet=replSet keyFile=/root/mongodb/keyfile
3.####keyfile生成
openssl rand -base64 90 -out ./keyfile chmod 600 keyfile mv keyfile /root/mongodb/
scp -r /root/mongodb 192.168.56.12:/root/ scp -r /root/mongodb 192.168.56.13:/root/
4.啟動mongodb 所有節點都啟動
mongod -f mongodb/mongo.conf
5.初始化副本集以及新增管理員(在一臺節點上面 我這是192.168.56.11)
mongo --port 27018 config = {_id: 'replSet', members: [{_id: 0, host: '192.168.56.11:27018'},{_id: 1, host: '192.168.56.12:27018'},{_id: 2, host:'192.168.56.13:27018'}]} rs.initiate(config) rs.status() use admin db.createUser({user:"admin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
6.新增管理員可以操作複製集的許可權(在primary節點上面)
use admin db.auth("admin","123456") db.grantRolesToUser( "admin" , [ { role: "dbOwner", db: "admin" },{ "role": "clusterAdmin", "db": "admin" }, { "role": "userAdminAnyDatabase", "db": "admin" }, { "role": "dbAdminAnyDatabase", "db": "admin" }])
7.測試 primary節點(通過rs.status可以看到)
use dbname db.createUser({ user: "test", pwd: "123456", roles:[{"role":"readWrite","db":"dbname"}, {"role":"dbAdmin","db":"dbname"}, {"role":"dbOwner","db":"dbname"}, {"role":"read","db":"dbname"}]}) db.inventory.insert( { _id: 10, type: "misc", item: "card", qty: 15 } )