创建副本集
一主二从
创建数据库目录
mkdir -p /data/mongodb/rs0-0 /data/mongodb/rs0-1 /data/mongodb/rs0-2
创建日志文件
touch /data/mongodb/rs0-0.log /data/mongodb/rs0-1.log /data/mongodb/rs0-2.log
创建3个mongod实例(测试站)
mongod --bind_ip 0.0.0.0 --port 23317 --dbpath /data/mongodb/rs0-0 -fork --logpath /data/mongodb/rs0-0.log --replSet rs0 --oplogSize 128
mongod --bind_ip 0.0.0.0 --port 23318 --dbpath /data/mongodb/rs0-1 -fork --logpath /data/mongodb/rs0-1.log --replSet rs0 --oplogSize 128
mongod --bind_ip 0.0.0.0 --port 23319 --dbpath /data/mongodb/rs0-2 -fork --logpath /data/mongodb/rs0-2.log --replSet rs0 --oplogSize 128
创建3个mongodb配置文件(正式站)
vim /data/mongodb/rs0-0.conf
vim /data/mongodb/rs0-1.conf
vim /data/mongodb/rs0-2.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/mongodb/rs0-0.log
# Where and how to store data.
storage:
dbPath: /data/mongodb/rs0-0
journal:
enabled: true
# engine:
# 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: 23317
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
security:
keyFile: /data/mongodb/config/rs0/keyfile
authorization: enabled
#operationProfiling:
replication:
replSetName: rs0
启动mongodb
mongod -f /data/mongodb/rs0.conf
连接数据库
mongo --port 23317
初始化配置编写
rsconf = {
"_id" : "rs0",
"members" : [
{
"_id" : 0,
"host" : "127.0.0.1:23317"
},
{
"_id" : 1,
"host" : "127.0.0.1:23318"
},
{
"_id" : 2,
"host" : "127.0.0.1:23319"
}
]
}
初始化副本集
rs.initiate(rsconf)
查看当前配置
rs.conf()
查看副本集状态
rs.status()
查看所有mongod的进程命令
ps -ef | grep mongod
创建管理员
进入主副本集
mongo --port 23317
切换为admin数据库
use admin
创建管理员账号
db.createUser(
... {
... user:"root",
... pwd:"123123",
... roles:[{role:"root",db:"admin"}]
... })
db.auth("root","123123")
踩坑
报错Unrecognized option: replication
解决方法:
replication: 冒号前加一个空格