Skip to main content

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 refers to the MongoDB component included in the hap-sc:3.0.0 mirror.

  1. 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"
  2. Restart the service and execute the following in the root directory of the manager:

    bash ./service.sh restartall