Convert MongoDB Single Node to Replica Set
Based on the HAP system architecture, a MongoDB single node involves either built-in MongoDB using a storage component mirror or single-deployed MongoDB. It also needs to consider whether authentication is enabled. In summary, there are the following 4 cases:
- Built-in MongoDB with authentication disabled
- Built-in MongoDB with authentication enabled
- External MongoDB with authentication disabled
- External MongoDB with authentication enabled
- Built-in MongoDB with authentication disabled
- Built-in MongoDB with authentication enabled
- External MongoDB with authentication disabled
- External MongoDB with authentication enabled
Built-in MongoDB refers to the MongoDB component included in the hap-sc:3.0.0 mirror.
-
Add
ENV_MONGODB_DAEMON_ARGS: "--replSet sc-mongodb"
in the docker-compose.yaml file 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:
app:
image: nocoly/hap-community:5.8.3
environment: &app-environment
ENV_MONGODB_DAEMON_ARGS: "--replSet sc-mongodb" -
Restart the service and execute the following in the root directory of the manager:
bash ./service.sh restartall
Built-in MongoDB refers to the MongoDB component included in the hap-sc:3.0.0 mirror.
-
Create a keyfile
echo $(openssl rand -base64 32) > /data/hap/script/volume/data/mongodb/keyfile
chmod 400 /data/hap/script/volume/data/mongodb/keyfile -
Modify the environment variable
ENV_MONGODB_DAEMON_ARGS
, and add--keyFile /data/mongodb/keyfile --replSet sc-mongodb
services:
app:
image: nocoly/hap-community:5.8.3
environment: &app-environment
ENV_MONGODB_DAEMON_ARGS: "--auth --keyFile /data/mongodb/keyfile --replSet sc-mongodb" -
Restart the service and execute the following in the root directory of the manager:
bash ./service.sh restartall
-
Replica set conversion
Access the storage component container:
docker exec -it script_sc_1 bash
Connect to the admin database using the root account
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 the following in the root directory of the manager:
bash ./service.sh restartall
-
Restart MongoDB, and append
--replSet sc-mongodb
to the start command, like:mongod --bind_ip 0.0.0.0 --dbpath /data/mongodb --logpath /data/logs/mongodb.log --replSet sc-mongodb
-
Connect to MongoDB and convert it to a replica set
Connect to the admin database
mongo mongodb://sc:27017/admin
Initialize the replica set (If there are clients connecting to this MongoDB that cannot communicate with the IP address directly, you can configure a service name instead. However, all clients will need to add a host resolution record for the sc service name.)
rs.initiate({_id:"sc-mongodb",members:[{_id:1,host:"service name or IP:27017"}]})
-
Restart the service and execute the following in the root directory of the manager:
bash ./service.sh restartall
-
Create a keyfile file in the MongoDB data directory (e.g.,
/data/mongodb/
)echo $(openssl rand -base64 32) > /data/mongodb/keyfile
chmod 400 /data/mongodb/keyfile -
Restart MongoDB, and append
--keyFile /data/mongodb/keyfile --replSet sc-mongodb
to the start command, like: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 it to a replica set
Connect to the admin database with root account
mongo mongodb://root:******@sc:27017/admin
Initialize the replica set (If there are clients connecting to this MongoDB that cannot communicate with the IP address directly, you can configure a service name instead. However, all clients will need to add a host resolution record for the sc service name.)
rs.initiate({_id:"sc-mongodb",members:[{_id:1,host:"service name or IP:27017"}]})
-
Restart the service and execute the following in the root directory of the manager:
bash ./service.sh restartall