MongoDB 副本集群搭建
参考链接:
- https://www.jianshu.com/p/13bd4121de12
- https://blog.csdn.net/zhanngle/article/details/105132527
- http://blog.itpub.net/29785807/viewspace-2928737/
使用Docker部署MongoDB副本集
mongodb从4.0开始支持事务,最近在使用mongodb的过程中,发现在单节点状态下,无法支持事务,只有配置了副本集才支持事务,故留下搭建MongoDB单机三节点副本集的记录。
1. 创建目录
git clone https://git.litsoft.com.cn/TechnicalData/005.MongoDB.git
cd 005.MongoDB/001.MongoDB_Cluster
mkdir -p ./MongoData/{Backup,Key,Mongo-RS01,Mongo-RS02,Mongo-RS03}
2. 创建密钥
openssl rand -base64 756 > MongoData/Key/mongo-rs.key && \
chmod 400 MongoData/Key/mongo-rs.key && \
chown polkitd:input MongoData/Key/mongo-rs.key
-
3. 创建mongodb三节点
创建了三个mongodb节点,并指定使用相同密钥文件
docker-compose up -d
或者:
docker run -d -p 30010:27017 \
--user root \
--restart always \
--name MongoDB-Cluster-RS01 \
--volume /etc/localtime:/etc/localtime \
--volume `pwd`/apps/mongo-rs/key:/data/key \
--volume `pwd`/apps/mongo-rs/data01:/data/db \
--volume `pwd`/apps/mongo-rs/backup:/data/backup \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=P@88W0rd \
mongo:7.0.1 --replSet mongo-rs --auth \
--keyFile /data/key/mongo-rs.key \
--bind_ip_all
docker run -d -p 30011:27017 \
--user root \
--restart always \
--name MongoDB-Cluster-RS02 \
--volume /etc/localtime:/etc/localtime \
--volume `pwd`/apps/mongo-rs/key:/data/key \
--volume `pwd`/apps/mongo-rs/data02:/data/db \
--volume `pwd`/apps/mongo-rs/backup:/data/backup \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=P@88W0rd \
mongo:7.0.1 --replSet mongo-rs --auth \
--keyFile /data/key/mongo-rs.key \
--bind_ip_all
docker run -d -p 30012:27017 \
--user root \
--restart always \
--name MongoDB-Cluster-RS03 \
--volume /etc/localtime:/etc/localtime \
--volume `pwd`/apps/mongo-rs/key:/data/key \
--volume `pwd`/apps/mongo-rs/data03:/data/db \
--volume `pwd`/apps/mongo-rs/backup:/data/backup \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=P@88W0rd \
mongo:7.0.1 --replSet mongo-rs --auth \
--keyFile /data/key/mongo-rs.key \
--bind_ip_all
---
docker run --name mongo-rs02 \
-p 30011:27017 \
-v `pwd`/apps/mongo-rs/data02:/data/db \
-v `pwd`/apps/mongo-rs/backup:/data/backup \
-v `pwd`/apps/mongo-rs/key:/data/key \
-v /etc/localtime:/etc/localtime \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=P@88W0rd \
-d mongo --replSet mongo-rs --auth \
--keyFile /data/key/mongo-rs.key \
--bind_ip_all
docker run --name mongo-rs03 \
-p 30012:27017 \
-v `pwd`/apps/mongo-rs/data03:/data/db \
-v `pwd`/apps/mongo-rs/backup:/data/backup \
-v `pwd`/apps/mongo-rs/key:/data/key \
-v /etc/localtime:/etc/localtime \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=P@88W0rd \
-d mongo --replSet mongo-rs --auth \
--keyFile /data/key/mongo-rs.key \
--bind_ip_all
4. 配置副本集
docker exec -it MongoDB-Cluster-RS01 mongosh
root@0193f5fb161b:/# mongosh
Current Mongosh Log ID: 64fa911becb736026fe841a6
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.5
Using MongoDB: 7.0.0
Using Mongosh: 1.10.5
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
test> use admin
switched to db admin
admin> db.auth("root","P@88W0rd")
{ ok: 1 }
// ---Dev配置---
var config={
_id:"mongo-rs",
members:[
{_id:0,host:"10.17.0.17:30010"},
{_id:1,host:"10.17.0.17:30011"},
{_id:2,host:"10.17.0.17:30012",arbiterOnly:true}
]};
rs.initiate(config)
rs.status()
// arbiterOnly:true 表示该节点为仲裁节点,只负责投票,不负责存储数据
// 执行rs.initiate()方法,初始化副本集,同时执行该方法的节点为主节点
// rs.status()可查看副本集节点状态
// ---Pvt配置---
var config={
_id:"mongo-rs",
members:[
{_id:0,host:"172.17.18.66:30010"},
{_id:1,host:"172.17.18.66:30011"},
{_id:2,host:"172.17.18.66:30012",arbiterOnly:true}
]};
rs.initiate(config)
rs.status()
// ---Uat配置---
var config={
_id:"mongo-rs",
members:[
{_id:0,host:"10.17.0.21:30010"},
{_id:1,host:"10.17.0.21:30011"},
{_id:2,host:"10.17.0.21:30012",arbiterOnly:true}
]};
rs.initiate(config)
rs.status()
// ---Prod配置---
var config={
_id:"mongo-rs",
members:[
{_id:0,host:"172.17.16.89:30010"},
{_id:1,host:"172.17.16.89:30011"},
{_id:2,host:"172.17.16.89:30012",arbiterOnly:true}
]};
rs.initiate(config)
rs.status()
5. 验证数据同步
在主节点插入数据
// Dev插入测试数据
use test
db.test.insert({name:"mongo rs test"})
// Prod 插入数据测试
mongo-rs [direct: primary] test> show dbs;
admin 108.00 KiB
config 116.00 KiB
local 308.00 KiB
mongo-rs [direct: primary] test> use task;
switched to db task
mongo-rs [direct: primary] task> show dbs;
admin 140.00 KiB
config 164.00 KiB
local 436.00 KiB
mongo-rs [direct: primary] task> db.task.insert({TestName:"北京联和利泰生产MongoDB"})
DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{
acknowledged: true,
insertedIds: { '0': ObjectId("6512394bd5a1a44de54e4b4b") }
}
mongo-rs [direct: primary] task>
// Uat插入测试数据
}
],
ok: 1
}
mongo-rs [direct: secondary] admin> show dbs;
admin 140.00 KiB
config 116.00 KiB
local 420.00 KiB
mongo-rs [direct: primary] admin> use task;
switched to db task
mongo-rs [direct: primary] task> db.task.insert({TestName:"北京联和利泰Uat环境MongoDB测试"})
DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{
acknowledged: true,
insertedIds: { '0': ObjectId("6512705f75690696305e6d4b") }
}
mongo-rs [direct: primary] task>
在从节点查询数据
[root@GitLab ~]# docker exec -it MongoDB-Cluster-RS02 mongosh
Current Mongosh Log ID: 65123964115ee898e7d71277
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.6
Using MongoDB: 7.0.1
Using Mongosh: 1.10.6
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
mongo-rs [direct: secondary] test> use admin
switched to db admin
mongo-rs [direct: secondary] admin> db.auth("root","P@88W0rd")
{ ok: 1 }
mongo-rs [direct: secondary] admin> show dbs;
admin 140.00 KiB
config 244.00 KiB
local 452.00 KiB
task 40.00 KiB
mongo-rs [direct: secondary] admin> db.task.find()
// 上述命令查询为空;因为没有切到task库中,所以为空
mongo-rs [direct: secondary] admin> use task
switched to db task
mongo-rs [direct: secondary] task> db.getMongo().setReadPref("secondary")
// 这个命令必须执行,要为然执行find命令时报错:MongoServerError: not primary and secondaryOk=false - consider using db.getMongo().setReadPref() or readPreference in the connection string
mongo-rs [direct: secondary] task>
mongo-rs [direct: secondary] task> db.task.find()
[
{
_id: ObjectId("6512394bd5a1a44de54e4b4b"),
TestName: '北京联和利泰生产MongoDB'
}
]
mongo-rs [direct: secondary] task>
// ---pvt配置---
[root@InfoTest ~]# docker exec -it mongo-rs02 mongosh
Current Mongosh Log ID: 64fae2e551c5e3bb2f3a76f9
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.6
Using MongoDB: 7.0.1
Using Mongosh: 1.10.6
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
mongo-rs [direct: secondary] test> use admin
switched to db admin
mongo-rs [direct: secondary] admin> db.auth("root","P@88W0rd")
{ ok: 1 }
mongo-rs [direct: secondary] admin> db.getMongo().setReadPref("secondary")
mongo-rs [direct: secondary] admin> db.test.find()
mongo-rs [direct: secondary] admin> use test;
switched to db test
mongo-rs [direct: secondary] test> db.test.find()
[
{
_id: ObjectId("64fae2afef6af564b121aa0a"),
name: 'Litsoft mongo rs test'
}
]
评论区