There is an existing standalone MongoDB running in Docker, and now it needs to read its oplog, so the replSet mode needs to be enabled.
1. Shutdown#
Copy the data directory for backup.
2. Modify the docker-compose file#
mongodb:
command: mongod --replSet rs -f /etc/mongod.conf
volumes:
- ./mongod.conf:/etc/mongod.conf
- ./mongo.keyfile:/etc/mongo.keyfile
-
Add
--replSet rs
and-f
parameters to specify the running mode as rs and the location of the configuration file. -
Specify the locations of the configuration file and key file.
3. mongod.conf configuration file#
The file specifies the data location and key file location.
storage:
dbPath: /data/db
journal:
enabled: true
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
authorization: enabled
keyFile: /etc/mongo.keyfile
4. mongo.keyfile#
The content of the key file can be generated using the openssl tool.
openssl rand -base64 756 > mongo.keyfile
Set permissions:
chmod 400 mongo.keyfile
Set user:
chown 999:999 mongo.keyfile
Otherwise, permission errors such as "permission too open" will occur and MongoDB will not start.
5. Start MongoDB#
Enter the client of the MongoDB container.
rs.initiate({ _id: "rs", members: [{_id:0,host:"ip:port"}]}
Initialize.