Migrate database from MongoDB Atlas to your server

MongoDB Atlas is a hosted MongoDB server hosting in cloud. If you are starting with MongoDB, they have free hosting, that work for smaller sites.

https://www.mongodb.com/cloud

MongoDB Atlas

On this page, you can see the version of MongoDB is Version 4.2.10. You need to install same or newer version of MongoDB on your server.

MongoDB documentation have detailed instructions for installing MongoDB on various Operating systems.

https://docs.mongodb.com/manual/administration/install-on-linux/

Take MongoDB backup

MongoDB atlas provide you command to take backup. On “Clusters” page, click the 3 dots, then select command line tools. Next page will show you command to take backup of your MongoDB database.

mkdir /root/mongodb_backup
cd /root/mongodb_backup
mongodump --uri mongodb+srv://YOURNAME_HERE:[email protected]/DATABASE_NAME_HERE

You can find database name in atlas, for this, click “Cluster0”, then go to Collections tab. You will see database name on this page.

Once you run the mongodump command, it will create a folder “dump” and save backup inside.

backup mongodb database using mongodump

Restart MongoDB backup

To restart MongoDB backup locally, run

mongorestore --username=USER_NAME_HERE --password=PW_HERE --authenticationDatabase "admin"  /root/mongodb_backup/dump/

USER_NAME_HERE and PW_HERE is MongoDB user and password. You can enable MongoDB authentication following instructions at

https://docs.mongodb.com/guides/server/auth/

Update Application to use local MongoDB

The application used following connection string

mongoose.connect("mongodb+srv://USER:[email protected]/DB_NAME?retryWrites=true&w=majority", {

I replaced it with

mongoose.connect("mongodb://USER:[email protected]:27017/DB_NAME?retryWrites=true&w=majority&authSource=admin", {

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *