MongoDB Single Node Upgrade to Replica Set High Availability Cluster
Project Background
Due to historical reasons, we have a business for data synchronization, and MongoDB is using single node in production environment. However, as the business grows, considering the importance of this synchronization business and avoiding business stoppage due to single node failure, we need to upgrade to a replica set to ensure high availability.
Replica set architecture
The following architecture diagram is the MongoDB replica set high availability architecture that needs to be implemented in this article.
Precautions before upgrading architecture
In the production environment, before doing single node upgrade to cluster, make sure to backup all the data of mongodb first to avoid data loss due to operation error.
And make sure that no program will connect to MongoDB for read and write operations during the upgrade, it is recommended to stop the service upgrade and operate in the early morning during the low business peak.
I. Original single-node MongoDB configuration information
IP: 192.168.30.207 |
1.1 Original configuration file
systemLog: |
1.2 Add replica set configuration to the original configuration file
replication: |
Note: Here you need to comment out the Authentication configuration first, and turn it on after the replica set configuration is complete.
II. New node information
角色 | IP | Port | |
---|---|---|---|
PRIMARY | 192.168.30.207 | 27017 | Original single-node MongoDB |
SECONDARY | 192.168.30.213 | 27017 | New node 1 |
SECONDARY | 192.168.30.214 | 27017 | New node 2 |
2.1 Add a new node profile
For these two SECONDARY node configuration files, simply copy the PRIMARY node configuration file and modify the corresponding “bindIp“.
- SECONDARY node 1 configuration file
systemLog: |
- SECONDARY node 2 configuration file
systemLog: |
2.2 Start 3 nodes
The PRIMARY node needs to be restarted and the 2 SECONDARY nodes are started directly.
# Start command |
III. Initialize the replica set
Use the mongo shell to connect to one of the nodes and execute the initialization command
3.1 Initial Configuration
config = { |
3.2 Initialization of the replica set
> rs.initiate(config) //Initialize copy set |
3.3 Viewing Replica Set Status
- To view cluster status information
rs1:PRIMARY> rs.status() |
- 查看延时从库信息
rs1:PRIMARY> rs.printSlaveReplicationInfo() |
- View delayed slave information
rs1:PRIMARY> rs.isMaster() |
IV. Replica set open authentication
4.1 Add a super administrator account via the master node
Note: If the original single-node mongo already has a super administrator account, this step can be ignored.
Simply add a user to the master node and the replica set will automatically synchronize the data on the master node.
Note that the account creation step needs to be done before authentication is enabled.
- Creating a hypervisor account
Super Admin User: mongouser Password: 123456 Authentication Library: admin
$ mongo --host 192.168.30.207 --port 27017 |
- View created accounts
rs1:PRIMARY> use admin |
4.2 Creating a keyfile for replica set authentication
All replica set nodes must use the same keyfile, which is usually generated on one machine and then copied to other machines, and must have read access, otherwise it will report errors in the future.
Make sure the keyfile is consistent, and the file location is random. But to make it easy to find, it is recommended to put it in a fixed location on each machine, all in a directory with the configuration file.
- Generate the
mongo.keyfile
file
$ openssl rand -base64 90 -out /home/server/mongodb/conf/mongo.keyfile |
- Copy the
mongo.keyfile
file to the same directory of the other 2 nodes
$ scp /home/server/mongodb/conf/mongo.keyfile root@192.168.30.213:/home/server/mongodb/conf/ |
4.3 Modify the MongoDB configuration file to enable authentication
- Add and modify the following configuration in the configuration file.
security: |
- Restart all mongo nodes
4.4 Verifying Replica Set Authentication
Login to the MongoDB replica set master node using username, password-free, and authentication library
$ mongo -u mongouser -p 123456 --host 192.168.30.207 --port 27017 -authenticationDatabase admin |