1. 创建副本集
1.1 创建副本主节点
创建数据库路径和log路径
mkdir /var/lib/mongodb \ &
mkdir /var/log/mongodb
创建配置文件
# /etc/mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
keyFile: /var/lib/mongo_key/autokey # 这个就是生成keyfile!
authorization: enabled
#operationProfiling:
replication:
replSetName: myrs
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
启动命令
#创建秘钥
mkdir /var/lib/mongo_key/
openssl rand -base64 756 > /var/lib/mongo_key/autokey
chmod 400 /var/lib/mongo_key/autokey
#启动
mongod -f /etc/mongod.conf --fork
1.2 创建副本从节点 (仲裁节点一样)
- 和主节点一样 换ip或者端口 两个路径也换
1.3 初始化副本集节点
#连接主节点MongoDB
mongo --port 27017
use admin
db.createUser({user:"test",pwd:"test",roles:["root"]})
#初始化
rs.initiate()
#添加从节点
rs.add("192.168.202.9:37017")
#添加仲裁节点(我们暂时不要)
rs.addArb("192.168.202.9:47017")
#连接从节点MongoDB
mongo --port 27017
#设置为从节点
rs.secondaryOk()