环境
host | ip | software | port | Usage | os |
master01 | 192.168.3.16 | mongodb5.0.1 | 27017 | sencondary | Centos7.9 |
master02 | 192.168.3.26 | mongodb5.0.1 | 27017 | sencondary | Centos7.9 |
master03 | 192.168.3.36 | mongodb5.0.1 | 27017 | primary | Centos7.9 |
- 下载
https://www.mongodb.com/try/download/enterprise
或者
wget https://downloads.mongodb.com/linux/mongodb-linux-x86_64-enterprise-rhel70-5.0.1.tgz
解压
#解压到三台主机对应目录
tar -zxvf mongodb-linux-x86_64-enterprise-rhel70-5.0.1.tgz
#重命名
mv mongodb-linux-x86_64-enterprise-rhel70-5.0.1 mongodb
配置各节点配置文件
官网配置项说明:
https://docs.mongodb.com/manual/reference/configuration-options/#footnote-yaml-json
#master03(主节点)
vim /home/mongodb/config/primary.conf
systemLog:
path: /home/mongodb/primary/primary.log
logAppend: true
destination: file
net:
bindIp: 192.168.3.36
port: 27017
storage:
dbPath: /home/mongodb/primary
directoryPerDB: true
oplogMinRetentionHours: 72.0
replication:
replSetName: dxmongodb
oplogSizeMB: 500
#mastor02(副本节点)---------
vim /home/mongodb/config/secondary.conf
systemLog:
path: /home/mongodb/secondary/secondary.log
logAppend: true
destination: file
net:
bindIp: 192.168.3.16
port: 27017
storage:
dbPath: /home/mongodb/secondary
directoryPerDB: true
oplogMinRetentionHours: 72.0
replication:
replSetName: dxmongodb
oplogSizeMB: 500
#master01(副本节点)--------
vim /home/mongodb/config/secondary.conf
systemLog:
path: /home/mongodb/secondary/secondary.log
logAppend: true
destination: file
net:
bindIp: 192.168.3.26
port: 27017
storage:
dbPath: /home/mongodb/secondary
directoryPerDB: true
oplogMinRetentionHours: 72.0
replication:
replSetName: dxmongodb
oplogSizeMB: 500
启动报错
#直接启动看是否有错误
/home/mongodb/bin/mongod -f /home/mongodb/config/secondary.conf(此处注意要用自己配置的配置文件)
#有可能会报如下错误:
error while loading shared libraries: libnetsnmpmibs.so.31: cannot open shared object file: No such file or directory
#需要安装
yum install -y net-snmp
# 测试,注意ip是自己配置文件中配置的,如果没有配置0.0.0.0 那么只有你配置的ip才可以访问
mongo 192.128.3.26
设置开机启动:
# vim /lib/systemd/system/mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=simple
RuntimeDirectory=mongodb
RuntimeDirectoryMode=0751
PIDFile=/home/mongodb/mongod.pid
ExecStart=/home/mongodb/bin/mongod --config /home/mongodb/config/secondary.conf
ExecStop=/home/mongodb/bin/mongod --shutdown --config /home/mongodb/config/secondary.conf
PrivateTmp=false
[Install]
WantedBy=multi-user.target
授权
chmod 754 mongodb.service
#从新加载
systemctl daemon-reload
#启动
systemctl start mongodb.service
#停止
systemctl stop mongodb.service
#设置开机启动
systemctl enable mongodb.service
- 设置集群
官网
#
[root@localhost ~]# /home/mongodb/bin/mongo
MongoDB Enterprise > use admin
#复制是不能有换行
MongoDB Enterprise > cfg={ _id: "dxmongodb",members: [
... {
... _id: 0,host:'192.168.3.16: 27017',priority: 2
... },
... {
... _id: 1,host:'192.168.3.26: 27017',priority: 1
... },
... {
... _id: 2,host:'192.168.3.36: 27017',priority: 3
... }
... ]
... };
#响应:
cfg={ _id: "dxmongodb",members: [
{
_id: 0,host:'192.168.3.16: 27017',priority: 2
},
{
_id: 1,host:'192.168.3.26: 27017',priority: 1
},
{
_id: 2,host:'192.168.3.36: 27017',priority: 3
}
]
};
MongoDB Enterprise > rs.initiate(cfg)
{ "ok" : 1 }
MongoDB Enterprise dxmongodb:SECONDARY>use admin
#设置root账号,启用权限登录时使用
MongoDB Enterprise dxmongodb:SECONDARY>db.createUser({user:"root",pwd:"123456",roles:["root","userAdminAnyDatabase"]}) -- 建立root并且分配角色
db.system.users.find()