Install MongoDB on Amazon EC2 and Point Parse Server to Work With Local MongoDb

Parse is going to shut down its services very soon and developers are encourage to migrate their apps from Parse.com to another cloud of their choice. If you have not migrated your app from Parse.com I strongly recommend you do it ASAP before it is too late and your data is gone. 

To help you migrate your app from Parse.com I have published a video course teaching How to Migrate Your App from Parse.com to your Own Parse Server running in Amazon cloud and with this tutorial and I am going to share with you how to install mongo db on your Amazon EC2 server and make Parse Server use your own database rather than external hosted with mlab.com for example. 

This tutorial assumes you already have Amazon EC2 instance running and Parse Server installed. If you do not, please refer to my video course on how to do it.

To install MongoDb on Amazon EC2 Linux Machine 

  1. Open new terminal window on your mac and connect to your EC2 linux machine. 
  2. Create a new file using text editor of your choice. I prefer vi. For example:
sudo vi /etc/yum.repos.d/mongodb-org-3.2.repo

 

when a new file is opened in your terminal window press “o” on your keyboard to enable editing and the paste this content into a file:

[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc

then to save your file:

  • esc button on your keyboard and
  • :qw enter

This will save new file with the content you have provided. 

Install MongoDB packages on you AWS EC2 Instance and associated tools

To install the latest stable version of MongoDB, issue the following command:

sudo yum install -y mongodb-org

Once installation is complete, run mongo database server this command: 

sudo mongod &

This will start mongo db server on your linux machine. 


To shut down mongodb server

To shut down mongodb server follow these steps: 

  1. Open new terminal window
  2. Start mongo shell by executing:
>mongo
>use admin 
>db.shutdownServer()

or you can use command killall mongod 

  1. Open new terminal window
  2. Execute killall mongod like so: 
>killall mongod

Create MongoDb Admin user

  1. Open terminal window 
  2. Execute following commands: 
> mongo
> use admin
> db.createUser({user:"someadmin",pwd:"secret", roles:[{role:"userAdminAnyDatabase",db:"admin"}]})

This will create a new user with userAdminAnyDatabase role


Create database user

db.createUser({ user:"sergey", pwd: "sergey", roles: [{role: "readWrite", db: "mycoolapp"}] })

Now, before we can import the JSON files of our Parse app exported from Parse.com into a MongoDb, we need to create a new mongo database on our linux machine. In my case the Parse app I have is called “mycoolapp”, so I will use this name for database name as well.

Create a new database on AWS EC2 linux server

If you do not have mongo console started use these commands:

>use mycoolapp

other wise:

>mongo
>use mycoolapp

Export Parse App as a collection of JSON files

Another way to export Parse app data from Parse.com is to not migrate the app to external database but use Export data button which is found on the same page “Settings Page” as Migrate This App button. To find Settings page: 

  • Login into Parse.com 
  • Select your App 
  • Open App Settings Page. 

On the Settings page click on “Export Data” button and in few minutes you will receive an email message from Parse.com with a link to download a zip archive with your App JSON files. 

export_app_image
Export Data from your Parse app


Import Parse database JSON files into MongoDb

To import Parse app JSON files into MongoDb follow these steps: 

  1. Open new terminal window and navigate to the folder that contains unzipped JSON files 
  2. For each JSON file exported from Parse.com perform mongoimport command. See example below: 
> mongoimport -d mycoolapp -c _User  "< PATH TO _User.json file here>/_User.json" --host=127.0.0.1

Once this is done, we can try fetching data from our mongo database via Parse Server. If your Parse Server is not running, run it now. 

Run Parse with Local mongo 

> parse-server --appId <Parse App Id here> --masterKey <Parse App Master key here> --databaseURI mongodb://<mongo database user name>:<mongo db user password>@localhost:27017/mycoolapp

Once our parse server is running we can fetch some data from our database using our Parse App

Fetch Data from MongoDB via Parse Server

Execute HTTP GET request using CURL and fetch data from _User Class: 

> curl -X GET -H "X-Parse-Application-Id: <Parse App Id here> " -H "X-Parse-Master-Key: < Parse App master Key here>" http://localhost:1337/parse/classes/_User

You should get back a success full response with User details in JSON format.

I hope this information was helpful to you! 🙂

Happy coding!