docker安装MongoDB复制集详细步骤
主节点ip: 192.168.124.122:27017
从节点ip: 192.168.124.129:27017
192.168.124.122 mongo-1
192.168.124.129 mongo-2
1. 配置hosts域名解析
cat >> /etc/hosts << EOF
192.168.124.122 mongo-1
192.168.124.129 mongo-2
EOF
2. 创建mongo文件夹
cd /opt
mkdir /mongo
cd /opt/mongo
mkdir m1
cd /opt/mongo/m1
mkdir log conf db
chmod 777 log
cd /opt/mongo/m1/conf
vim mongod.conf
3. 配置mongod.conf
- mongod.conf文件的内容如下,将内容复制到mongod.conf中保存并退出
- 节点1与节点2一致
storage:
dbPath: /data/db
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /data/log/mlogs
net:
port: 27017
bindIp: 0.0.0.0
security:
authorization: enabled
clusterAuthMode: keyFile
keyFile: /data/conf/mongodb.keyfile
replication:
oplogSizeMB: 10240
replSetName: rs1
cd /opt/mongo/conf
openssl rand -base64 736 > /opt/mongo/m1/conf/mongodb.keyfile
chmod 600 mongodb.keyfile
chown 999 mongodb.keyfile
chmod 600 mongodb.keyfile
chown 999 mongodb.keyfile
4. 启动mongo容器
docker search mongo
docker pull mongo:5.0.3
docker images
docker run --network=host --restart always \
--name="m1" \
-v /opt/mongo/m1:/data \
-v /opt/mongo/m1/db:/data/db \
-v /etc/localtime:/etc/localtime \
-d mongo:5.0.3 \
--config /data/conf/mongod.conf
docker run --network=host --restart always \
--name="m1" \
-v /opt/mongo/m1:/data \
-v /opt/mongo/m1/db:/data/db \
-v /etc/localtime:/etc/localtime \
-d mongo:5.0.3 \
--config /data/conf/mongod.conf
firewall-cmd --permanent --zone=public --add-port=27017/tcp
firewall-cmd --state
firewall-cmd --reload
docker restart m1
docker ps
docker exec -it m1 bash
mongo admin
myconf = {"_id":"rs1","members":[{"_id":0,"host":"mongo-1:27017"},{"_id":1,"host":"mongo-2:27017"}]}
rs.initiate(myconf)
rs.status()
rs.secondaryOk()
use admin
db.createUser( {user: "admin",pwd: "123456",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
db.auth("admin", "123456")
db.createUser({user:"root",pwd:"123456",roles:["root"]})
db.auth("root", "123456")
use audit_db
db.createCollection("audit_doc")
show dbs
5. 进入容器相关命令
[root@CentOS-7 m1]
root@CentOS-7:/
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("73fad32c-c7e5-4b0b-b72d-a1b5cc9758ef") }
MongoDB server version: 5.0.3
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
> myconf = {"_id":"rs1","members":[{"_id":0,"host":"mongo-1:27017"},{"_id":1,"host":"mongo-2:27017"}]}
{
"_id" : "rs1",
"members" : [
{
"_id" : 0,
"host" : "mongo-1:27017"
},
{
"_id" : 1,
"host" : "mongo-2:27017"
}
]
}
> rs.initiate(myconf)
{ "ok" : 1 }
rs1:SECONDARY> rs.status()
{
"set" : "rs1",
"date" : ISODate("2021-11-18T08:03:46.124Z"),
"myState" : 2,
"term" : NumberLong(0),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 2,
"writableVotingMembersCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastCommittedWallTime" : ISODate("1970-01-01T00:00:00Z"),
"appliedOpTime" : {
"ts" : Timestamp(1637222617, 1),
"t" : NumberLong(-1)
},
"durableOpTime" : {
"ts" : Timestamp(1637222617, 1),
"t" : NumberLong(-1)
},
"lastAppliedWallTime" : ISODate("2021-11-18T08:03:37.387Z"),
"lastDurableWallTime" : ISODate("2021-11-18T08:03:37.387Z")
},
"lastStableRecoveryTimestamp" : Timestamp(0, 0),
"members" : [
{
"_id" : 0,
"name" : "mongo-1:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 170,
"optime" : {
"ts" : Timestamp(1637222617, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2021-11-18T08:03:37Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 0,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "mongo-2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 8,
"optime" : {
"ts" : Timestamp(1637222617, 1),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(1637222617, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2021-11-18T08:03:37Z"),
"optimeDurableDate" : ISODate("2021-11-18T08:03:37Z"),
"lastHeartbeat" : ISODate("2021-11-18T08:03:46.047Z"),
"lastHeartbeatRecv" : ISODate("2021-11-18T08:03:45.817Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 0
}
],
"ok" : 1
}
rs1:SECONDARY> rs.secondaryOk()
rs1:PRIMARY> use admin
switched to db admin
rs1:PRIMARY> db.createUser( {user: "admin",pwd: "123456",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
rs1:PRIMARY> db.auth("admin", "123456")
1
rs1:PRIMARY> db.createUser({user:"root",pwd:"123456",roles:["root"]})
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
rs1:PRIMARY> db.auth("root", "123456")
1
rs1:PRIMARY> use audit_db
switched to db audit_db
rs1:PRIMARY> db.createCollection("audit_doc")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1637222699, 1),
"signature" : {
"hash" : BinData(0,"wPlhpziQOHbFcOD8aB9gbPu+Rc8="),
"keyId" : NumberLong("7031817639236206595")
}
},
"operationTime" : Timestamp(1637222699, 1)
}
rs1:PRIMARY> show dbs
admin 0.000GB
audit_db 0.000GB
config 0.000GB
local 0.000GB
rs1:PRIMARY> rs.isMaster()
{
"topologyVersion" : {
"processId" : ObjectId("619608386bc3246c119d2348"),
"counter" : NumberLong(6)
},
"hosts" : [
"mongo-1:27017",
"mongo-2:27017"
],
"setName" : "rs1",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "mongo-1:27017",
"me" : "mongo-1:27017",
"electionId" : ObjectId("7fffffff0000000000000001"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1637222777, 1),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2021-11-18T08:06:17Z"),
"majorityOpTime" : {
"ts" : Timestamp(1637222777, 1),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2021-11-18T08:06:17Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2021-11-18T08:06:20.444Z"),
"logicalSessionTimeoutMinutes" : 30,
"connectionId" : 5,
"minWireVersion" : 0,
"maxWireVersion" : 13,
"readOnly" : false,
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1637222777, 1),
"signature" : {
"hash" : BinData(0,"O6gJHv2zBriDd3owVjkFuTaPMss="),
"keyId" : NumberLong("7031817639236206595")
}
},
"operationTime" : Timestamp(1637222777, 1)
}
rs1:PRIMARY> exit
bye
root@CentOS-7:/
exit
[root@CentOS-7 m1]
- 在主数据库中导入数据,查看从数据库,发现数据已经同步