How to Migrate MongoDB to a New Server
Adam C. |

Recently I migrated MongoDB (over 2GB) from a cheap server hosted on Hudson Valley Host to a better server hosted on Digital Ocean to improve the performance.

Photo by Ray Hennessy on Unsplash

Servers Specs

  • Old: Ubuntu 16.04 / 4G Memory /  4vCPUs E3-1240 V2 @ 3.40GHz / MongoDB 3.6.4
  • New: Ubuntu 20.04 / 8G Memory  / 4vCPUs E5-2650 v4 @ 2.20GHz / MongoDB 4.4

Install MongoDB

It's pretty straightforward by following the docs on MongoDB official website: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

Note that: By default, MongoDB has no default user or password, and you can only access them via localhost. But very likely, you would need to enable access control (i.e., auth) and create a user to access the databases. 

Create DB User

Right after you install the MongoDB, you should be able to log in Mongo Shell, by running the command “mongo” via SSH terminal. And then enter the following command:

use admin
db.createUser(
  {
    user: "dbUser",
    pwd: "password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

Note that I assign the user userAdminAnyDatabase role, which gives the user the ability to create users and assign arbitrary roles to them.  It sounds like a superuser, but it's not. To be a superuser or root user, you have to assign the following roles:

Export Remote Database

Run the following command to dump the existing remote database:

mongodump  --host REMOTE_DB_HOST --username REMOTE_DB_USERNAME --password REMOTE_DB_PASS --authenticationDatabase admin --db DB_NAME --out backup

Note

  1.  ‘admin’ is the default authenticationDatabase,  you may use a different one.
  2. use “--out” instead of “--archive” as some other tutorial mentioned, and “backup” is the folder name where I save the dump files

Import dumped MongodDB

In the folder having ‘backup’ subfolder, run the following command:

mongorestore backup/

Enable Auth and Remote Access

NOTE: DON'T DO THIS STEP UNTIL ALL STEPS ABOVE ARE COMPLETED!

Now it's time to enable auth and remote access. It can be configured in the mongod.conf (by default: it's under /etc/mongod.conf.) Run ‘vim /etc/mongod.conf’, and then modify the following lines:

 net:
  port: 27017
# change bindIP to all IPV4 addresses, otherwise mongod only listens to 127.0.0.1
  bindIp: 0.0.0.0
# ...
# uncomment security and enable authorization
security:
  authorization: enabled

By default,  Ubuntu does not enable UFW (Uncomplicated Firewall) If you use it, then you add the remote server IP the whitelist using the following command:

sudo ufw allow from remote_server_ip to any port 27017

Updated

On: 12/21.2021

Recently I worked on a project involved in a large dataset. I had to process the data on my local before moving them to the remote database. The way I showed above does not work, because I could not connect to my local machine from the remote server.  Here is what I figured out:

Dump the database on my local machine

mongodump --db=DBNAME --out=backup1221

Since it's local, I don't have authorization enabled. If it's not your case, you could do this:

mongodump --username=REMOTE_DB_USERNAME -p --authenticationDatabase=admin --db=DBNAME --out=backup1221

Upload dump folder to the remote server

My local server is Windows, so I use WinSCP. It's slow - about 1M/s. I zipped the folder before uploading. Or you may try gizp when doing the mongodump, like

mongodump --gzip --db=DBNAME --out=backup1221

Restore on the remote server

My remote server has authorization enabled, so I use the command below:

 mongorestore -u username --authenticationDatabase=admin dump122121

Note:

  1. The authenticationDatabase have to match where user was originally created. In the other words, it's not necessary to be “admin”.
  2. Some parameter keys have two formats, for example: username could be “--username” or “-u”. We could use “=” or “ ” to assign the value, like -u=USERNAME or -u USERNAME. Check the doc for details.
  3. I did not include the password from the command line,  it will prompt you to enter.