Cloning Mongo Database From Heroku to Local
These are the steps I compiled to clone a Mongo database from Heroku to local. It is provided as-is without warranty nor guarantee nor support.
- Open command prompt (Windows) or Terminal (Mac/Linux) and navigate to the project’s working folder
.
- Get the config vars from Heroku with
heroku config --app <appname>
- Find the
MONGOLAB_URI
value. It will be in the form of:mongodb://<username>:<password>@<url>:<port>/<database>
- Find the
- Run the following command to make a copy of the production database to your local working directory.
- Use the values from the config file:
mongodump -h <url>:<port> -d <database> -u <username> -p <password>
mongodump
will create the following directory to store the data:dump/<database>/
- Use the values from the config file:
- Make sure your local instance of
mongod
is running. - Drop your existing local database with the following command.
mongo <dbname> --eval "db.dropDatabase()"
<dbname>
Β is your local database name specified in your project’s config file underMONGOLAB_URI
.
- Use
mongorestore
to put the prod data into your local mongodb.mongorestore -d <dbname> dump/<database>/
- Ensure data was restored
- Start mongo command line interface:
mongo
- Show databases and ensure
<dbname>
is in the list:> show databases
- Switch to
<dbname>
:> use <dbname>
- Ensure
games
andplayers
tables exist:> show collections
- If satisfied, then exit mongo command line:
> exit
- Start mongo command line interface:
- Run the app and see the prod data (see “Running” section below)
- ???
- Profit!