标签:客户端 技术 帮助 root 写入 ann not http alt
什么是复制集?mkdir -p /data/mongodb/mongodb{2,3,4} //创建数据目录
mkdir logs
touch logs/mongodb{2,3,4}.log //创建日志文件
cd logs/
chmod 777 *.log //赋予权限
vim /etc/mongod2.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/mongodb2.log //日志文件存放位置
# Where and how to store data.
storage:
dbPath: /data/mongodb/mongodb2 //数据文件存放位置
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27018 //监听端口及IP地址
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
replication:
replSetName: abc //打开主从复制集功能,每个节点都要配置相同名称
mongod -f /etc/mongod.conf --shutdown
mongod -f /etc/mongod.conf
mongod -f /etc/mongod2.conf
mongod -f /etc/mongod3.conf
mongod -f /etc/mongod4.conf
mongo
> show dbs //查看数据库
> rs.status() //查看复制集状态
> cfg={"_id":"abc","members":[{"_id":0,"host":"192.168.144.112:27017"},{"_id":1,"host":"192.168.144.112:27018"},{"_id":2,"host":"192.168.144.112:27019"}]} //配置复制集节点IP
> rs.initiate(cfg) //初始化配置时保证从节点没有数据
> rs.status() //此时再查看复制集状态
abc:PRIMARY> rs.add("192.168.144.112:27020") //添加节点
abc:PRIMARY> rs.remove("192.168.144.112:27020") //删除节点
> rs.status()
ps aux | grep mongod
kill -9 46374
abc:PRIMARY> rs.freeze(30) //暂停30s不参与选举
abc:PRIMARY> rs.stepDown(60,30) //交出主节点位置,维持从节点状态不少于60秒,等待30秒使主节点和从节点日志同步
节点类型分为:标准(host)节点、被动(passive)和仲裁(arbiter)节点。
replication:
replSetName: abc
mongo //进入数据库
cfg={"_id":"abc","members":[{"_id":0,"host":"192.168.144.112:27017","priority":100},{"_id":1,"host":"192.168.144.112:27018","priority":100},{"_id":2,"host":"192.168.144.112:27019","priority":0},{"_id":3,"host":"192.168.144.112:27020","arbiterOnly":true}]}
//设置节点IP端口以及节点类型
> rs.initiate(cfg) //初始化数据库
> rs.isMaster() //查看状态
abc:PRIMARY> use kgc //创建数据库
abc:PRIMARY> db.t1.insert({"id":1,"name":"tom"}) //创建集合t1并插入数据
abc:PRIMARY> db.t1.insert({"id":2,"name":"jerry"})
abc:PRIMARY> db.t1.find() //查看集合数据
abc:PRIMARY> db.t1.update({"id":2},{$set:{"name":"jack"}}) //修改集合数据
abc:PRIMARY> db.t1.remove({"id":1}) //删除数据
abc:PRIMARY> use local //进入oplog所在数据库
abc:PRIMARY> show collections //查看所有集合
oplog.rs //oplog集合
abc:PRIMARY> db.oplog.rs.find()
//查看日志记录所有操作,此时从节点会从oplog中同步数据
mongod -f /etc/mongod.conf --shutdown
mongo --port 27018
mongo --port 27019
在标准节点上写入数据,其他节点上也会同步复制,如何从从节点上读取数据?
[root@localhost]# mongo --port 27018
abc:SECONDARY> show dbs //此时从节点不允许读取数据
abc:SECONDARY> rs.slaveOk() //允许默认从节点读取数据
abc:SECONDARY> show dbs //再次查看时就可以了
abc:SECONDARY> rs.help() //查看命令帮助手册
abc:SECONDARY> rs.printReplicationInfo()
configured oplog size: 990MB
log length start to end: 1544secs (0.43hrs)
oplog first event time: Mon Jul 16 2018 05:49:12 GMT+0800 (CST)
oplog last event time: Mon Jul 16 2018 06:14:56 GMT+0800 (CST)
now: Mon Jul 16 2018 06:14:59 GMT+0800 (CST)
abc:SECONDARY> rs.printSlaveReplicationInfo()
source: 192.168.235.200:27018
syncedTo: Mon Jul 16 2018 06:16:16 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 192.168.235.200:27019
syncedTo: Mon Jul 16 2018 06:16:16 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
abc:ARBITER> rs.printReplicationInfo()
cannot provide replication status from an arbiter.
//会发现仲裁节点并不具备数据复制
abc:PRIMARY> db.printReplicationInfo()
configured oplog size: 1613.301513671875MB //默认大小
log length start to end: 18650secs (5.18hrs)
oplog first event time: Tue Jul 17 2018 11:08:30 GMT+0800 (CST)
oplog last event time: Tue Jul 17 2018 16:19:20 GMT+0800 (CST)
now: Tue Jul 17 2018 16:19:21 GMT+0800 (CST)
mongo
abc:PRIMARY> use admin
switched to db admin
abc:PRIMARY> db.shutdownServer()
也可以采用mongod -f /etc/mongod.conf --shutdown 方式关闭服务
vim /etc/mongod.conf
...
# network interfaces
net:
port: 27027 //修改端口
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
#replication: //注释复制功能
# replSetName: abc
...
mongo -f /etc/mongod.conf
mongodump --port 27027 --db local --collection ‘oplog.rs‘ //全备份当前节点oplog记录
mongo --port 27027
> use local //进入local数据库
> db.oplog.rs.drop() //删除oplog原有集合
> db.runCommand( { create: "oplog.rs", capped: true, size: (2 * 1024 * 1024 * 1024) } ) //重建oplog,并指定大小
> use admin
> db.shutdownServer() //关闭服务
...
# network interfaces
net:
port: 27017 //端口修改回原有端口
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
replication: //取消注释复制功能
replSetName: abc
oplogSizeMB:2048 //添加指定oplog日志大小
...
mongod -f /etc/mongod.conf
mongo --port 27017
abc:SECONDARY> db.printReplicationInfo()
configured oplog size: 2048MB //oplog日志大小已经改变
log length start to end: 30secs (0.01hrs)
oplog first event time: Tue Jul 17 2018 17:33:18 GMT+0800 (CST)
oplog last event time: Tue Jul 17 2018 17:33:48 GMT+0800 (CST)
now: Tue Jul 17 2018 17:33:54 GMT+0800 (CST)
abc:SECONDARY>
mongo
进入primary
kgcrs:PRIMARY> use admin
kgcrs:PRIMARY> db.createUser({"user":"root","pwd":"123","roles":["root"]}) //创建管理用户,并且使用管理用户认证
[root@localhost]# vim /etc/mongod.conf
[root@localhost]# vim /etc/mongod2.conf
[root@localhost]# vim /etc/mongod3.conf
[root@localhost]# vim /etc/mongod4.conf
...
security:
keyFile: /usr/bin/kgcrskey1
clusterAuthMode: keyFile
...
cd /usr/bin/
创建认证文件,并且输入统一认证密匙
[root@localhost bin]# echo "kgcrs key"> kgcrskey1
[root@localhost bin]# echo "kgcrs key"> kgcrskey2
[root@localhost bin]# echo "kgcrs key"> kgcrskey3
[root@localhost bin]# echo "kgcrs key"> kgcrskey4
[root@localhost bin]# chmod 600 kgcrskey{1..4} //设置权限,只有属主可以查看
进入primary
kgcrs:PRIMARY> show dbs //无法查看数据库
kgcrs:PRIMARY> rs.status() ///无法查看复制集
kgcrs:PRIMARY> use admin //身份登录验证
kgcrs:PRIMARY> db.auth("root","123")
kgcrs:PRIMARY> rs.status() //可以查看数据库
kgcrs:PRIMARY> show dbs //可以查看复制集
MongoDB复制集(实现选举复制、故障切换、升级oplog大小、认证复制)
标签:客户端 技术 帮助 root 写入 ann not http alt
原文地址:http://blog.51cto.com/13659253/2146029