master配置
本机 :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 >>keyfile chmod 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 = true journal = 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()