mongodb 在認證(auth)的情況下,配置主從(master & slave)
本機 :192.168.100.23:27017
/etc/mongodb/mongo.conf
port = 27017
dbpath = /data/mongodb_t
logpath = /var/log/mongodb_t.log
logappend = true
journal = true
fork = true
1. 以上配置單機打開mongo,對admin數據庫進行設置
據資料, 必須建立全局用戶root,個人感覺是超級管理員; 必須對local數據庫建立repl用戶。
mongo –port 27017
> use admin > db.createUser({user:"root", pwd:"123456", roles:[{role:"root", db:"admin"}]}) > db.createUser({user:"repl", pwd:"123456", roles:[{role:"dbOwner", db:"local"}]}) > show users { "_id" : "admin.root", "user" : "root", "db" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ] } { "_id" : "admin.repl", "user" : "repl", "db" : "admin", "roles" : [ { "role" : "dbOwner", "db" : "local" } ] } >exit12345678910111213141516171819202122232425262728
2、 修改配置文件
添加以下三項:
port = 27017
dbpath = /data/mongodb_t
logpath = /var/log/mongodb_t.log
keyFile=/srv/mongodb/keyfile
logappend = true
journal = true
fork = true
keyFile=/srv/mongodb/keyfile #添加keyfile
auth = true #打開認證
master = true #打開主庫配置
3、添加keyfile
主要目的是對主從之間的驗證
cd /srv/mongodb/
openssl rand -base64 741 >>keyfilechmod 700 keyfile
4、重啟數據庫
salve配置
本機:與主庫不再同一臺設備上面,同一局域網
/etc/mongodb/mongo.conf
port = 27017
dbpath = /data/mongodb_t
logpath = /logs/mongo/mongodb_t.log
logappend = true
journal = true
fork = true
1. 同master第一步
2. 修改配置文件
port = 27017
dbpath = /data/mongodb_t
logpath = /logs/mongo/mongodb_t.log
logappend = truejournal = true
fork = true
auth = true
slave = true
keyFile=/srv/mongodb/keyfile
source = 192.168.100.23:27017
3、添加keyfile
cd /srv/mongodb/
將主庫設備上面的keyfile拷貝到這個目錄下
chmod 700 keyfile
4、重啟數據庫
重啟之後,登陸數據庫,就能看到與主庫的同步了
如果shell登陸, 需要
rs.slaveOk()
mongodb 在認證(auth)的情況下,配置主從(master & slave)