一、创建复制集
1、创建数据目录
mkdir -p /data/mongodb/mongodb{2,3,4}
2、创建日志文件并赋予权限
cd /data/mongodb
mkdir logs
touch logs/mongodb{2,3,4}.log
cd logs
chmod 777 *
3、修改配置文件
vim /etc/mongod.conf
replication
replSetName: hzh
重启方法:
mongod -f /etc/mongod.conf --shutdown # 关闭数据库
mongod -f /etc/mongod.conf # 开启数据库
4、创建多实例
cp -p /etc/mongod.conf /etc/mongod2.conf
vim /etc/mongod2.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/mongodb2.log
storage:
dbPath: /data/mongodb/mongodb2
journal:
enabled: true
net:
port: 27018
bindIp: 0.0.0.0
cp -p /etc/mongod2.conf /etc/mongod3.conf
vim /etc/mongod3.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/mongodb3.log
storage:
dbPath: /data/mongodb/mongodb3
journal:
enabled: true
net:
port: 27019
bindIp: 0.0.0.0
cp -p /etc/mongod3.conf /etc/mongod4.conf
vim /etc/mongod4.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/mongodb4.log
storage:
dbPath: /data/mongodb/mongodb4
journal:
enabled: true
net:
port: 27020
bindIp: 0.0.0.0
5、启动多实例并验证是否可以进入实例
mongod -f /etc/mongod2.conf
mongod -f /etc/mongod3.conf
mongod -f /etc/mongod4.conf。
mongo --port 27018
mongo --port 27019
mongo --port 27020
6、进入默认数据库
mongo
cfg={"_id":"hzh","members":[{"_id":0,"host":"192.168.241.9:27017"},{"_id":1,"host":"192.168.241.9:27018"},{"_id":2,"host":"192.168.241.9:27019"}]}
{
"_id" : "hzh",
"members" : [
{
"_id" : 0,
"host" : "192.168.241.9:27017"
},
{
"_id" : 1,
"host" : "192.168.241.9:27018"
},
{
"_id" : 2,
"host" : "192.168.241.9:27019"
}
]
}
rs.initiate(cfg) # 初始化配置时,保证从节点上没有数据
{
"ok" : 1,
"operationTime" : Timestamp(1618208035, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1618208035, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
rs.status() # 查看复制集状态
7、添加/删除节点(需要在主节点上添加)
rs.add("192.168.241.9:27020") #添加
rs.remove("192.168.241.9:27020") #删除
rs.status()
8、故障集自动切换
ps aux | grep mongod
kill -9 [PID] # PID是主的pid号
mongo --port 27018
#手动切换
rs.freeze(30) #暂停30s不参与选举
rs.stepDown(60,30) #交出主节点位置,维持从节点状态不少于60s,等待30s使主节点和从节点日志同步
二、复制集管理
1、标准节点(hosts) 2、仲裁节点(arbiters) 3、被动节点(passsives)
创建复制集的方法在上面,就不重复写了
cfg={"_id":"hzh","members":[{"_id":0,"host":"192.168.241.9:27017","priority":100},{"_id":1,"host":"192.168.241.9:27018","priority":100},{"_id":2,"host":"192.168.241.9:27019","priority":0},{"_id":3,"host":"192.168.241.9:27020","arbiterOnly":true}]}
rs.initiate(cfg) #初始化
rs.isMaster
{
"hosts" : [
"192.168.241.9:27017",
"192.168.241.9:27018"
],
"passives" : [
"192.168.241.9:27019"
],
"arbiters" : [
"192.168.241.9:27020"
.......
1、在主库上创建集合及数据
use info
db.t1.insert({"id":1,"name":"tom"})
db.t1.insert({"id":2,"name":"jack"})
db.t1.find()
db.t1.update({"id":1},{$set:{"name":"jerry"}}) #替换
db.t1.remove({"id":2}) #删除
show dbs
use local
show tables
oplog.rs #日志
db.oplog.re.find()
2、登录从数据集
rs.slaveOk() #允许从节点读取数据
3、查看复制状态信息
rs.help()
rs.printReplicationInfo()
configured oplog size: 990MB
log length start to end: 991secs (0.28hrs)
oplog first event time: Mon Apr 12 2021 15:21:19 GMT+0800 (CST)
oplog last event time: Mon Apr 12 2021 15:37:50 GMT+0800 (CST)
now: Mon Apr 12 2021 15:37:57 GMT+0800 (CST)
rs.printSlaveReplicationInfo()
WARNING: printSlaveReplicationInfo is deprecated and may be removed in the next major release. Please use printSecondaryReplicationInfo instead.
source: 192.168.241.9:27018
syncedTo: Mon Apr 12 2021 15:38:10 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 192.168.241.9:27019
syncedTo: Mon Apr 12 2021 15:38:10 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
发现仲裁节点并不具备数据复制
三、更改oplog大小
1、从复制集———关闭复制集
mongo --port 27018
use admin
db.shutdownServer()
注销replication相关启动参数,并修改port端口号27028
vim /etc/mongod2.conf
net:
port: 27028
#replication #注释
# replSetName: hzh #注释
2、单实例模式启动
mongod -f /etc/mongod2.conf
3、备份当前节点的所有oplog记录
mongodump --port 27028 --db local --collection 'oplog.rs'
2021-04-12T16:14:34.074+0800 writing local.oplog.rs to
2021-04-12T16:14:34.075+0800 done dumping local.oplog.rs (250 documents)
mongo --port 27028
use local
db.oplog.rs.drop() #删除
db.runCommand({create:"oplog.rs",capped:true,size:(2*1024*1024*1024)}) #重建日志集合
4、再次进服务进行关闭
use admin
db.shutdownServer()
vim /etc/mongod2.conf
net:
port: 27018
replication
replSetName: hzh
oplogSizeMB: 2048
mongod -f /etc/mongod2.conf
5、主节点让出主的身份
rs.stepDown()
四、部署认证复制
1、创建认证用户
mongo
use admin
db.createUser({"user":"root","pwd":"123","roles":["root"]})
2、复制集中四个实例的配置文件中加入密码文件参数
vim /etc/mongod.conf
security:
keyFile: /usr/bin/mykey1
clusterAuthMode: keyFile
vim /etc/mongod2.conf
security:
keyFile: /usr/bin/mykey2
clusterAuthMode: keyFile
vim /etc/mongod3.conf
security:
keyFile: /usr/bin/mykey3
clusterAuthMode: keyFile
vim /etc/mongod4.conf
security:
keyFile: /usr/bin/mykey4
clusterAuthMode: keyFile
3、创建密钥文件并修改权限为600
cd /usr/bin/
echo "1" > mykey1 #echo的内容自定义,但是4个文件内容要一致
echo "1" > mykey2
echo "1" > mykey3
echo "1" > mykey4
chmod 600 mykey*
4、对四个实例依次重启
mongod -f /etc/mongod.conf --shutdown
mongod -f /etc/mongod.conf
mongod -f /etc/mongod2.conf --shutdown
mongod -f /etc/mongod2.conf
mongod -f /etc/mongod3.conf --shutdown
mongod -f /etc/mongod3.conf
mongod -f /etc/mongod4.conf --shutdown
mongod -f /etc/mongod4.conf
5、进入primary
show dbs #无法查看数据库
rs.status() #无法查看复制集
use admin #身份登录验证
db.auth("root","123")
show dbs #这就可以看到了