1. 编写docker-compose.conf 文件
version: '3'
services:
mongo:
image: mongo:latest # 指定 MongoDB 版本,确保 >= 3.6
container_name: mongo-replica
restart: always
command: ["mongod", "--replSet", "rs0", "--oplogSize", "128", "--bind_ip_all"]
ports:
- 27017:27017
volumes:
- ./mongo_data:/data/db
environment:
#初始化MongoDb管理员用户名
MONGO_INITDB_ROOT_USERNAME: root
#初始化MongoDb的管理员密码
MONGO_INITDB_ROOT_PASSWORD: example
2. 启动
在包含 docker-compose.yml 文件的目录中运行以下命令启动 MongoDB:
docker-compose up -d
这将创建一个 MongoDB 副本集实例。--replSet 选项用于指定副本集名称为 rs0,--bind_ip_all 确保 MongoDB 绑定到所有 IP 地址。
本地27017端口映射到容器内部27017端口,把容器内部的/data/db目录挂载当前目录下的mongo_db目录,并且初始化了mongo的管理员用户和密码
3. 验证启动是否成功
命令行验证
docker ps
4. 初始化副本集
1.首次启动容器后,需要初始化 MongoDB 副本集。连接到 MongoDB 并运行以下命令:
docker exec -it mongo-replica mongo
2.然后在 MongoDB shell 中运行以下命令:
rs.initiate()
这样就初始化一个名为 rs0 的副本集。
3.验证配置
在 MongoDB shell 中,可以使用以下命令验证副本集的状态:
rs.status()
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2024-10-24T09:31:21.318Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 1,
"writeMajorityCount" : 1,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1729762278, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2024-10-24T09:31:18.528Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1729762278, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2024-10-24T09:31:18.528Z"),
"appliedOpTime" : {
"ts" : Timestamp(1729762278, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1729762278, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2024-10-24T09:31:18.528Z"),
"lastDurableWallTime" : ISODate("2024-10-24T09:31:18.528Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1729762278, 1),
"lastStableCheckpointTimestamp" : Timestamp(1729762278, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2024-10-24T07:16:15.041Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1729754175, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 1,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"newTermStartDate" : ISODate("2024-10-24T07:16:15.048Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2024-10-24T07:16:15.053Z")
},
"members" : [
{
"_id" : 0,
"name" : "5ffc77d51cd0:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 8176,
"optime" : {
"ts" : Timestamp(1729762278, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2024-10-24T09:31:18Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1729754175, 2),
"electionDate" : ISODate("2024-10-24T07:16:15Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1729762278, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1729762278, 1)
}
5. 登陆MongoDb
可以通过mongo客户端或者Docker命令登陆,这里选择使用MongoDb自带的图形化工具进行登录
1.输入host,勾选Direct Connecton
2.点到Authentication,输入用户名密码和认证数据库
3.保存&登录
6. 创建角色和用户
1.点击Open MongoDB shell进入mongo shell
2.创建角色 用户分配权限
use admin;
db.createRole(
{
role: "flinkrole",
privileges: [{
// 所有数据库中所有非系统集合的 grant 权限
resource: { db: "", collection: "" },
actions: [
"splitVector",
"listDatabases",
"listCollections",
"collStats",
"find",
"changeStream" ]
}],
roles: [
// 阅读 config.collections 和 config.chunks
// 用于分片集群快照拆分。
{ role: 'read', db: 'config' }
]
}
);
db.createUser(
{
user: 'flinkuser',
pwd: 'flinkpw',
roles: [
{ role: 'flinkrole', db: 'admin' }
]
}
);
就可以使用新创建的用户进行登录MongoDb了