In this blog post, we will discuss how we can migrate from the enterprise/community edition of MongoDB to Percona Server for MongoDB. But before we begin, let’s take a second to explain why you should migrate to Percona Server for MongoDB.
Percona Distribution for MongoDB is a single solution that combines the best and most important enterprise components from the open source community, designed and tested to work together. Percona customers benefit from no lock-in and lower total cost of ownership, along with the freedom to run their MongoDB environment wherever they want to – in a public or private cloud, on-premises, or hybrid environment.
Percona Server for MongoDB offers the same, or equivalent, security features as MongoDB Enterprise without the price tag, and Percona experts are always available to help, bringing in-depth operational knowledge of MongoDB and open source tools so you can optimize database performance. If you’d like to learn more, please click here.
Anyway, let’s get back to the purpose of the blog: migrating from the enterprise/community edition of MongoDB to Percona Server for MongoDB.
Before starting the migration process it’s recommended that you perform a full backup (if you don’t have one already). See this post for MongoDB backup best practices.
The migration procedure:
- Backup the config files of the Mongo process.
- Stop the Mongo process. If it’s a replica set, then do it in a rolling fashion.
- Remove the package of MongoDB community/enterprise edition. For the replica set, do it in a rolling fashion.
- Install the Percona Server for MongoDB (PSMDB). It can be downloaded from here. Do it in a rolling fashion for a replica set and start the Mongo service.
Detailed migration plan:
Migrate standalone MongoDB community/enterprise edition to Percona Server for MongoDB (Debian/RHEL):
1. Backup the mongo config and service file:
sudo cp /etc/mongod.conf /etc/mongod.conf_bkp
Debian:
sudo cp /lib/systemd/system/mongod.service /lib/systemd/system/mongod.service_bkp
RHEL/CentOS:
sudo cp /usr/lib/systemd/system/mongod.service /usr/lib/systemd/system/mongod.service_bkp
2. Stop mongo service first and then remove mongodb-community/enterprise packages and repo:
To stop mongo services, connect to admin database and shutdown as below:
>use admin >db.shutdownServer()
Remove the package:
Debian:
sudo apt-get remove mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools sudo rm /etc/apt/sources.list.d/mongodb-org-4.0.list
RHEL/CentOS:
sudo yum erase $(rpm -qa | grep mongodb-org)
If it’s OpsManager then:
a. Unmanage the project in OpsManager GUI.
b. Make sure to uncheck enforce users in Opsmanager GUI.
c. Disable the automation agent with the below:
sudo apt disable mongodb-mms-automation-agent
d. Remove the automation agent with:
sudo systemctl remove mongodb-mms-automation-agent
3. Configure percona repo and install Percona Server for MongoDB (PSMDB):
Debian:
sudo wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb
Enable the repo:
sudo percona-release enable psmdb-44 release sudo apt-get update
Install the package:
sudo apt-get install percona-server-mongodb
RHEL/CentOS:
sudo yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
Enable the repo:
sudo percona-release setup pdmdb-44
Install the package:
sudo yum install percona-server-mongodb
4. Copy back the mongod config and service file:
sudo cp /etc/mongod.conf_bkp /etc/mongod.conf
Debian:
sudo cp /lib/systemd/system/mongod.service_bkp /lib/systemd/system/mongod.service
RHEL/CentOS:
sudo cp /usr/lib/systemd/system/mongod.service_bkp /usr/lib/systemd/system/mongod.service
NOTE: Kindly check that the permissions and ownership of the data directory, keyfile, and log directory are properly updated for the mongod user.
Also, if the SELinux policy is enabled, then set the necessary SELinux policy for dbPath, keyFile, and logs as below:
sudo semanage fcontext -a -t mongod_var_lib_t '/dbPath/mongod.*' sudo chcon -Rv -u system_u -t mongod_var_lib_t '/dbPath/mongod' sudo restorecon -R -v '/dbPath/mongod' sudo semanage fcontext -a -t mongod_log_t '/logPath/log.*' sudo chcon -Rv -u system_u -t mongod_log_t '/logPath/log' sudo restorecon -R -v '/logPath/log'
5. Enable and start mongod service:
sudo systemctl daemon-reload sudo systemctl enable mongod sudo systemctl start mongod sudo systemctl status mongod
Migrate Replica set MongoDB Enterprise/Community edition to Percona Server for MongoDB (Debian/RHEL):
This migration process involves stopping the Mongo process in the hidden/secondary node first, removing the MongoDB community/enterprise edition packages, installing Percona Server for MongoDB, and starting it with the same data files. Then, step down the current primary node and repeat the same process.
a. Make sure to check the current Primary and Secondary/hidden nodes.
db.isMaster().primary
b. Start with the hidden node (if there is no hidden node then start with one of the secondary nodes with the least priority) first.
c. Repeat steps from 1 to 5 from the section Migrate standalone MongoDB community/enterprise edition to Percona Server for MongoDB (Debian/RHEL).
d. Wait for each node to be synced with Primary. Verify it with
rs.printSecondaryReplicationInfo()
e. Once completed for all secondary nodes, step down the current primary with
rs.stepDown()
f. Wait for the new node to be elected as a Primary node and repeat steps 1 to 5 from the section Migrate standalone MongoDB community/enterprise edition to Percona Server for MongoDB (Debian/RHEL) and wait for the former primary node to be synced with the newly elected Primary.
Migrate Sharded cluster MongoDB community/enterprise edition to Percona Server for MongoDB (Debian/RHEL):
1. Stop the balancer first:
sh.stopBalancer()
2. Back up the configuration and service files for shards, CSRS, and Query router.
Backup the mongo config and service file for shards and CSRS:
sudo cp /etc/mongod.conf /etc/mongod.conf_bkp
Debian:
sudo cp /lib/systemd/system/mongod.service /lib/systemd/system/mongod.service_bkp
For router:
sudo cp /etc/mongos.conf /etc/mongos.conf_bkp sudo cp /lib/systemd/system/mongos.service /lib/systemd/system/mongos.service_bkp
RHEL/CentOS:
sudo cp /usr/lib/systemd/system/mongod.service /usr/lib/systemd/system/mongod.service_bkp
For router:
sudo cp /etc/mongos.conf /etc/mongos.conf_bkp sudo cp /usr/lib/systemd/system/mongos.service /usr/lib/systemd/system/mongos.service_backup
3. Start with the hidden node of the CSRS first (if there is no hidden node then start with one of the secondary nodes).
Repeat steps a to f from the section Migrate Replica set MongoDB community/enterprise edition to Percona Server for MongoDB (Debian/RHEL).
Once migrated, the CSRS to Percona Server for MongoDB moves to Shards for the migration. Repeat steps a to f from the section Migrate Replica set MongoDB community/enterprise edition to Percona Server for MongoDB (Debian/RHEL).
4. After the migration of the CSRS and Shards, start migrating the MongoS. Connect to one router at a time and execute the below steps followed by the remaining routers.
5. Stop mongo service and then remove mongodb-community/enterprise packages and repo:
sudo systemctl stop mongos
Debian:
sudo apt-get remove mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools sudo rm /etc/apt/sources.list.d/mongodb-org-4.0.list
RHEL/CentOS:
sudo yum erase $(rpm -qa | grep mongodb-org)
6. Configure percona repos and install Percona Server for MongoDB (PSMDB):
Debian:
sudo wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb
Enable the repo:
sudo percona-release enable psmdb-44 release sudo apt-get update
Install the package:
sudo apt-get install percona-server-mongodb
RHEL/CentOS:
sudo yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
Enable the repo:
sudo percona-release setup pdmdb-44
Install the package:
sudo yum install percona-server-mongodb
Copy back the config and service file:
sudo cp /etc/mongos.conf_bkp /etc/mongos.conf
Debian:
sudo cp /lib/systemd/system/mongos.service_bkp /lib/systemd/system/mongos.service
RHEL/CentOS:
sudo cp /usr/lib/systemd/system/mongos.service_bkp /usr/lib/systemd/system/mongos.service
NOTE: Kindly check that the permissions and ownership of keyfile and log directory are properly updated for the mongod user.
7. Enable and start mongos service:
sudo systemctl daemon-reload sudo systemctl enable mongos sudo systemctl start mongos sudo systemctl status mongos
8. Re-enable the balancer with below:
sh.startBalancer()
Conclusion:
To learn more about the enterprise-grade features available in the license-free Percona Server for MongoDB, we recommend going through our blog MongoDB: Why Pay for Enterprise When Open Source Has You Covered?
We also encourage you to try our products for MongoDB like Percona Server for MongoDB, Percona Backup for MongoDB, or Percona Operator for MongoDB.