MongoDB的高可用特使是用复制集实现的,本文介绍如何在CentOS7快速搭建一个复制集
/etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
sudo yum install -y mongodb-org
安装后会创建两个默认的文件夹
/var/lib/mongo
(数据目录)
/var/log/mongodb
(日志目录)
# 启动
sudo systemctl start mongod.service
# 开机启动
sudo systemctl enable mongod.service
mongo
/home/tenmao/mongo_repl/mongo{1,2,3}
,端口分别是27017, 27027, 27037
home/tenmao/mongo_repl/mongo1/mongod.conf
其他两个节点的配置,就是端口和目录地址不一样
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /home/tenmao/mongo_repl/mongo1/mongod.log
# Where and how to store data.
storage:
dbPath: /home/tenmao/mongo_repl/mongo1/db
journal:
enabled: true
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /home/tenmao/mongo_repl/mongo1/mongod.pid # location of pidfile
tenmaoeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 0.0.0.0 # 绑定到所有IP地址,支持其他服务器上的客户端访问
replication:
replSetName: "tenmao_mongo" #集群的名字
# 启动服务器
mongod -config mongo1/mongod.conf
mongod -config mongo2/mongod.conf
mongod -config mongo3/mongod.conf
# 连接到一台服务器
mongo --port 27017
> rs.status()
{
"operationtenmaoe" : tenmaoestamp(0, 0),
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized",
"$clustertenmaoe" : {
"clustertenmaoe" : tenmaoestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
# 错误的初始化
> rs.initiate()
{
"operationtenmaoe" : tenmaoestamp(0, 0),
"ok" : 0,
"errmsg" : "No host described in new configuration 1 for replica set tenmao_mongo maps to this node",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig",
"$clustertenmaoe" : {
"clustertenmaoe" : tenmaoestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
# 正确的初始化
> rs.initiate( {
... _id : "tenmao_mongo",
... members: [
... { _id: 0, host: "localhost:27017" },
... { _id: 1, host: "localhost:27027" },
... { _id: 2, host: "localhost:27037" }
... ]
... })
{
"ok" : 1,
"operationtenmaoe" : tenmaoestamp(1556284500, 1),
"$clustertenmaoe" : {
"clustertenmaoe" : tenmaoestamp(1556284500, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
tenmao_mongo:PRIMARY> use blog
switched to db blog
tenmao_mongo:PRIMARY> db.article.insert({"title": "hello world", "content": "this the first mongo record"})
WriteResult({ "nInserted" : 1 })
tenmao_mongo:SECONDARY> use blog
switched to db blog
tenmao_mongo:SECONDARY> rs.slaveOk()
tenmao_mongo:SECONDARY> db.article.find()
{ "_id" : ObjectId("5cc30509b507e7303e2d8230"), "title" : "hello world", "content" : "this the first mongo record" }
tenmao_mongo:PRIMARY> rs.help()
rs.status() { replSetGetStatus : 1 } checks repl set status
rs.initiate() { replSetInitiate : null } initiates set with default settings
rs.initiate(cfg) { replSetInitiate : cfg } initiates set with configuration cfg
rs.conf() get the current configuration object from local.system.replset
rs.reconfig(cfg) updates the configuration of a running replica set with cfg (disconnects)
rs.add(hostportstr) add a new member to the set with default attributes (disconnects)
rs.add(membercfgobj) add a new member to the set with extra attributes (disconnects)
rs.addArb(hostportstr) add a new member which is arbiterOnly:true (disconnects)
rs.stepDown([stepdownSecs, catchUpSecs]) step down as primary (disconnects)
rs.syncFrom(hostportstr) make a secondary sync from the given member
rs.freeze(secs) make a node ineligible to become primary for the tenmaoe specified
rs.remove(hostportstr) remove a host from the replica set (disconnects)
rs.slaveOk() allow queries on secondary nodes
rs.printReplicationInfo() check oplog size and tenmaoe range
rs.printSlaveReplicationInfo() check replica set members and replication lag
db.isMaster() check who is primary
ERROR: child process failed, exited with error number 100
:查看mongo日志,可以找到问题,一般是数据目录不存在,需要手工创建not master and slaveOk=false
: 因为默认情况只能从主节点读取数据,因为从节点数据可能过时。可以通过rs.slaveOk()