Convert MongoDB Single Node to Replica Set
Based on the system architecture of this product, MongoDB single nodes are divided into using storage component mirroring built-in MongoDB or using separately deployed MongoDB. In addition, on this basis, it is also necessary to distinguish whether to enable authentication. So it includes the following 4 situations:
- Use built-in MongoDB without authentication enabled
- Turn on authentication using built-in MongoDB
- When using external MongoDB, authentication is not enabled
- Use external MongoDB to enable authentication
- Use built-in MongoDB without authentication
- Use built-in MongoDB to enable authentication
- Authentication is not enabled when using external MongoDB
- Use external MongoDB and authentication is enabled
Built-in MongoDB refers to the MongoDB component included in the mingdaoyun-sc:3.2.0 image
-
Add
ENV_MONGODB_DAEMON_ARGS: "--replSet sc-mongodb"in docker-compose.yaml as follows:After adding
ENV_MONGODB_DAEMON_ARGS, it will automatically convert to replica set mode. The built-in script is:rs.initiate({_id: "sc-mongodb",members:[ {_id : 1, host : "sc:27017"} ]})(If there are other clients connecting to this MongoDB, you need to add a host resolution record for the sc service name on the client)services:core:image: registry.cn-hangzhou.aliyuncs.com/mdpublic/mingdaoyun-core:7.4.2environment: &app-environmentENV_MONGODB_DAEMON_ARGS: "--replSet sc-mongodb" -
Restart the service and execute in the root directory of the manager:
bash ./service.sh restartall
Built-in MongoDB refers to the MongoDB component included in the mingdaoyun-sc:3.2.0 image
-
Create keyfile file
echo $(openssl rand -base64 32) > /data/mingdao/script/volume/data/mongodb/keyfilechmod 400 /data/mingdao/script/volume/data/mongodb/keyfile -
Modify the environment variable
ENV_MONGODB_DAEMON_ARGSand add--keyFile /data/mongodb/keyfile --replSet sc-mongodbservices:core:image: registry.cn-hangzhou.aliyuncs.com/mdpublic/mingdaoyun-core:7.4.2environment: &app-environmentENV_MONGODB_DAEMON_ARGS: "--auth --keyFile /data/mongodb/keyfile --replSet sc-mongodb" -
Restart the service and execute in the root directory of the manager:
bash ./service.sh restartall -
Replica set conversion
Enter the storage component container
docker exec -it script_sc_1 bash
Use the root account to connect to the admin library
mongo mongodb://root:******@sc:27017/admin
Initialize the replica set (If there are other clients connecting to this MongoDB, you need to add a host resolution record for the sc service name on the client)
rs.initiate({_id: "sc-mongodb",members:[ {_id : 1, host : "sc:27017"} ]})
-
Restart the service again and execute in the root directory of the manager:
bash ./service.sh restartall
-
Restart MongoDB and append
--replSet sc-mongodbto the startup command, such as:mongod --bind_ip 0.0.0.0 --dbpath /data/mongodb --logpath /data/logs/mongodb.log --replSet sc-mongodb -
Connect to MongoDB and convert replica sets
Connect to admin library
mongo mongodb://sc:27017/admin
Initialize the replica set (If there are other clients connecting to this MongoDB, but the client is not connected to the IP, you can configure the service name, but all clients need to add host resolution records for the sc service name)
rs.initiate({_id:"sc-mongodb",members:[{_id:1,host:"service-name-or-IP:27017"}]})
-
Restart the service and execute in the root directory of the manager:
bash ./service.sh restartall
-
Create a keyfile file in the MongoDB data directory (eg:
/data/mongodb/)echo $(openssl rand -base64 32) > /data/mongodb/keyfilechmod 400 /data/mongodb/keyfile -
Restart MongoDB and append
--keyFile /data/mongodb/keyfile --replSet sc-mongodbto the startup command, such as:mongod --bind_ip 0.0.0.0 --dbpath /data/mongodb --logpath /data/logs/mongodb.log --auth --keyFile /data/mongodb/keyfile --replSet sc-mongodb -
Connect to MongoDB and convert replica sets
Use the root account to connect to the admin library
mongo mongodb://root:******@sc:27017/admin
Initialize the replica set (If there are other clients connecting to this MongoDB, but the client is not connected to the IP, you can configure the service name, but all clients need to add host resolution records for the sc service name)
rs.initiate({_id:"sc-mongodb",members:[{_id:1,host:"service-name-or-IP:27017"}]})
-
Restart the service and execute in the root directory of the manager:
bash ./service.sh restartall