Mongodb-副本集部署
Posted dingkailinux
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mongodb-副本集部署相关的知识,希望对你有一定的参考价值。
一、简单理解MongoDB副本集
1.概念:
MongoDB副本集相当于mysql的主从复制,一主多从,单与MySQL不同的是,MongoDB的副本集实例的数量至少有三个。
2.结构
(1)一个主节点,多个从节点
(2)本次部署采用一主两从的结构
参考【http://www.mongoing.com/docs/core/replica-set-architecture-three-members.html】
(3)优点:可以实现故障转移
二、部署副本集
参考文档【https://github.com/johnnian/Blog/issues/8】
注:本次因资源限制,所以在一台服务器上运行三个实例
1.部署MongoDB实例
请参考【http://www.cnblogs.com/dingkailinux/p/9007057.html】
(1)实例端口
实例01:27017 实例02:27018 实例03:27019
(2)创建应用目录
# mkdir -p /opt/mongo/{27017,27018,27019}/{mongo_data,mongo_log,etc}
# mkdir -p /opt/mongo/mongokeyfile
# usermod -d /opt/mongo mongod
# chown -R mongod.mongod /opt/mongo/
(3)修改mongo配置文件
标红部分,需要根据实际情况更改
# cat << EOF >> /opt/mongo/27017/mongo_27017.conf #生成配置文件,或者在原有的配置文件里做更改 # mongod.conf, Percona Server for MongoDB # for documentation of all options, see: # http://docs.mongo.org/manual/reference/configuration-options/ # Where and how to store data. storage: dbPath: /opt/mongo/27017/mongo_data/ journal: enabled: true engine: wiredTiger # Storage engine various options # More info for wiredTiger: https://docs.mongo.com/v3.4/reference/configuration-options/#storage-wiredtiger-options wiredTiger: engineConfig: cacheSizeGB: 1 #内存的70%,单位是G # checkpointSizeMB: 1000 # statisticsLogDelaySecs: 0 # journalCompressor: snappy # directoryForIndexes: false # collectionConfig: # blockCompressor: snappy # indexConfig: # prefixCompression: true # Two options below can be used for wiredTiger and inMemory storage engines #setParameter: # wiredTigerConcurrentReadTransactions: 128 # wiredTigerConcurrentWriteTransactions: 128 # where to write logging data. systemLog: destination: file logAppend: true logRotate: rename path: /opt/mongo/27017/mongo_log/mongod.log processManagement: fork: true pidFilePath: /opt/mongo/27017/mongo_log/mongod.pid # network interfaces net: port: 27017 #监听端口 bindIp: 10.140.0.4 http: JSONPEnabled: false RESTInterfaceEnabled: false maxIncomingConnections: 100000 wireObjectCheck : true #security: # authorization: enabled # keyFile: /opt/mongo/mongo_key/mongokeyfile #集群节点间必须使用同一个文件 #operationProfiling: replication: replSetName: "repset01" #副本集的名称,同一内所有实例必须使用同一个名称 #sharding: ## Enterprise-Only Options: #auditLog: #snmp: EOF
# cp /opt/mongo/27017/mongo_27017.conf /opt/mongo/27018/mongo_27018.conf
# cp /opt/mongo/27017/mongo_27017.conf /opt/mongo/27019/mongo_27019.conf
(4)生成systemd相关配置
生成system启动文件 # /usr/lib/systemd/system # cp mongod.service mongod27017.service # cp mongod.service mongod27018.service # cp mongod.service mongod27019.service
------------------------------------------------------------- # cp /etc/sysconfig # cp mongod mongod_27017 # cp mongod mongod_27018 # cp mongod mongod_27019 修改以下标红部分 OPTIONS="-f /opt/mongo/27017/mongo_27017.conf" STDOUT="/opt/mongo/27017/mongo_log/mongod.stdout" STDERR="/opt/mongo/27017/mongo_log/mongod.stderr" NUMACTL="numactl --interleave=all"
(5)修改systemd相关配置
分别修改mongod27017.service、mongod27018.service、mongod27019.service以下标红部分 # cd /usr/lib/systemd/system/
# vim mongod27017.service
[Unit] Description=High-performance, schema-free document-oriented database After=time-sync.target network.target [Service] Type=forking User=mongod Group=mongod PermissionsStartOnly=true LimitFSIZE=infinity LimitCPU=infinity LimitAS=infinity LimitNOFILE=64000 LimitNPROC=64000 EnvironmentFile=-/etc/sysconfig/mongod_27017 #进程启动环境变量定义文件 ExecStartPre=/usr/bin/percona-server-mongodb-helper.sh ExecStart=/usr/bin/env bash -c "${NUMACTL} /usr/bin/mongod ${OPTIONS} > ${STDOUT} 2> ${STDERR}" PIDFile=/opt/mongo/27017/mongo_log/mongod.pid #进程pid文件位置 [Install] WantedBy=multi-user.target
#######################################
分别修改mongod_27017、mongod mongod_27018、mongod mongod_27019中标红部分
# cd /etc/sysconfig/
# vim mongod_27017
OPTIONS="-f /opt/mongo/27017/mongo_27017.conf" #指定实例的配置文件
STDOUT="/opt/mongo/27017/mongo_log/mongod.stdout" #指定实例启动的输出日志
STDERR="/opt/mongo/27017/mongo_log/mongod.stderr" #指定实例启动的报错日志
NUMACTL="numactl --interleave=all"
(6)启动实例
# systemctl start mongod27017
# systemctl start mongod27018
# systemctl start mongod27019
# systemctl enable mongod27017
# systemctl enable mongod27018
# systemctl enable mongod27019
2.配置副本集
(1)配置副本集
# mongo --host 10.140.0.4 --port 27017
> config = {_id: \'repset01\', members: [{_id: 0, host: \'10.140.0.4:27017\'},{_id: 1, host: \'10.140.0.4:27018\'},{_id: 2, host:\'10.140.0.4:27019\'}]} { "_id" : "repset01", "members" : [ { "_id" : 0, "host" : "10.140.0.4:27017" }, { "_id" : 1, "host" : "10.140.0.4:27018" }, { "_id" : 2, "host" : "10.140.0.4:27019" } ] } > rs.initiate(config) #副本集初始化 { "ok" : 1 } repset01:OTHER> rs.status() #查看副本集状态 { "set" : "repset01", "date" : ISODate("2018-05-11T07:01:58.428Z"), "myState" : 1, "term" : NumberLong(1), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1526022117, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1526022117, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1526022117, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, "name" : "10.140.0.4:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 158, "optime" : { "ts" : Timestamp(1526022117, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-05-11T07:01:57Z"), "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1526022095, 1), "electionDate" : ISODate("2018-05-11T07:01:35Z"), "configVersion" : 1, "self" : true }, { "_id" : 1, "name" : "10.140.0.4:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 34, "optime" : { "ts" : Timestamp(1526022117, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1526022117, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-05-11T07:01:57Z"), "optimeDurableDate" : ISODate("2018-05-11T07:01:57Z"), "lastHeartbeat" : ISODate("2018-05-11T07:01:57.333Z"), "lastHeartbeatRecv" : ISODate("2018-05-11T07:01:58.168Z"), "pingMs" : NumberLong(0), "syncingTo" : "10.140.0.4:27019", "configVersion" : 1 }, { "_id" : 2, "name" : "10.140.0.4:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 34, "optime" : { "ts" : Timestamp(1526022117, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1526022117, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-05-11T07:01:57Z"), "optimeDurableDate" : ISODate("2018-05-11T07:01:57Z"), "lastHeartbeat" : ISODate("2018-05-11T07:01:57.333Z"), "lastHeartbeatRecv" : ISODate("2018-05-11T07:01:58.153Z"), "pingMs" : NumberLong(0), "syncingTo" : "10.140.0.4:27017", "configVersion" : 1 } ], "ok" : 1 }
(2)创建用户
在主节点上,用客户端连接,创建用户权限(主节点,可以用 rs.status() 查看)
# mongo --host 10.140.0.4 --port 27018
> use admin
> db.createUser({user:"admin", pwd:"admin", roles:[{role: "userAdminAnyDatabase", db:"admin" }]})
> db.createUser({user:"clusterAdmin", pwd:"clusterAdmin", roles:[{role: "clusterAdmin", db:"admin" }]})
> db.createUser({user:"root", pwd:"dingkai.123", roles:[{role: "root", db:"admin" }]})
3.开启数据库认证
(1)生成密钥文件
# openssl rand -base64 504 -out /opt/mongo/mongo_key/mongokeyfile
# chmod 600 /opt/mongo/mongo_key/mongokeyfile
# chown -R mongod.mongod opt/mongo/mongo_key/mongokeyfile
(2)开启MongoDB的认证配置
#关闭副本集:分别关闭每个节点的mongod
理论上应该这样关闭
# mongo --host 10.140.0.4 --port 27017
> use admin
> db.shutdownServer()
但是db.shutdownServer()无法关闭,所以本人粗暴的使用了:
# systemctl stop mongod27017
# systemctl stop mongod27018
# systemctl stop mongod27019
#开启配置文件中以下选项
security:
authorization: enabled
keyFile: /opt/mongo/mongo_key/mongokeyfile #集群节点间必须使用同一个文件
#重启所有实例
# systemctl restart mongod27017
# systemctl restart mongod27018
# systemctl restart mongod27019
(3)验证复制集可用性
在主节点创建数据
# mongo --host 10.140.0.4 --port 27017 Percona Server for MongoDB shell version v3.4.14-2.12 connecting to: mongodb://10.140.0.4:27017/ Percona Server for MongoDB server version: v3.4.14-2.12 repset01:PRIMARY> use admin switched to db admin repset01:PRIMARY> db.auth("admin","admin") 1 repset01:PRIMARY> use test003 switched to db test003 repset01:PRIMARY> db.createUser({user:"test003", pwd:"test003", roles:[{role: "readWrite", db:"test003" }]}) Successfully added user: { "user" : "test003", "roles" : [ { "role" : "readWrite", "db" : "test003" } ] } repset01:PRIMARY> db.auth("test003","test003") 1 repset01:PRIMARY> db.runoob.insert({"name":"丁凯"}) WriteResult({ "nInserted" : 1 }) repset01:PRIMARY> show dbs admin 0.000GB local 0.000GB test001 0.000GB test002 0.000GB test003 0.000GB 查看从节点是否有数据 # mongo --host 10.140.0.4 --port 27018 Percona Server for MongoDB shell version v3.4.14-2.12 connecting to: mongodb://10.140.0.4:27018/ Percona Server for MongoDB server version: v3.4.14-2.12 repset01:SECONDARY> use admin switched to db admin repset01:SECONDARY> db.auth("admin","admin") 1 repset01:SECONDARY> rs.slaveOk() repset01:SECONDARY> show dbs admin 0.000GB local 0.000GB test001 0.000GB test002 0.000GB test003 0.000GB repset01:SECONDARY>
以上是关于Mongodb-副本集部署的主要内容,如果未能解决你的问题,请参考以下文章
原创第1篇:Docker MongoDB 最简单副本集(集群)部署
高可用MongoDB集群部署详解——搭建MongoDB副本集