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.
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.
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:
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 backupNote
authenticationDatabase, you may use a different one.In the folder having ‘backup’ subfolder, run the following command:
mongorestore backup/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: enabledBy 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 27017On: 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:
mongodump --db=DBNAME --out=backup1221Since 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=backup1221My 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=backup1221My remote server has authorization enabled, so I use the command below:
mongorestore -u username --authenticationDatabase=admin dump122121Note: