Setting up MongoDB for MAMP

Welcome to my first blog post of 2018!

Setting up MongoDB in MAMP can be tricky. Finding a guide that shows you how to do this can be even harder. In this post I’ll walk you through the steps required to do so.

This guide assumes a few things. First, that you have homebrew installed. Second, that you have MAMPs version of PHP accessible from terminal/CLI. If all that is setup you can follow the steps below.

1) Install AutoConf, OpenSSL, and pkg-config using Homebrew
The first step is to install all of the packages required by MongoDB.

brew install autoconf openssl pkg-config

2) Link OpenSSL to /usr/local/include
This step is required for homebrew’s version of OpenSSL to be accessed by MAMP’s version of PHP.

cd /usr/local/include
$ ln -s ../opt/openssl/include/openssl

3) Install MongoDB
After we have the dependencies out of the way, you can now install MongoDB.

brew install mongodb

After setting up MongoDB you need to create a “db” directoy or else MongoDB will fail to start.

mkdir -p /data/db

Then make sure the /data/db directory’s permissions are correct because the wrong permissions will also stop MongoDB from starting.

sudo chmod 0755 /data/db

4) Install PHP’s MongoDB driver in MAMP
This next step enables MAMP’s version of PHP to access MongoDB.

Be sure to replace php7.0.22 below with the version of PHP that terminal is using from MAMP. I’m using PHP 7.0.22 but your version may be different.

cd /Applications/MAMP/bin/php/php7.0.22/bin
sudo pecl install mongo

5) Edit MAMP’s php.ini to include the MongoDB extension

First, check which php.ini file is being used by MAMP’s PHP

php -i | grep -nr "php.ini"

You should be shown a php.ini located within the /Applications/MAMP/ folder.

Next, you’ll need to edit this php.ini file to include the MongoDB extension.

vim /Applications/MAMP/bin/php/php7.0.22/conf/php.ini

You can edit this file with any text editor you wish, I prefer vim.

You’ll look for the section beginning with ;Extensions and add to the list of extensions being loaded being sure to save before closing.

6) Restart MAMP’s servers
Now that you have done all necessary steps to install MongoDB the next step is simple, restart MAMPs servers by stopping then starting servers.

7) Start MongoDB
In a terminal window type mongod which will start the MongoDB service. You can quit by typing ctrl + c.

Then you can type mongo to run the MongoDB CLI. You can exit by typing

8) Run a test script
With everything installed, it’s time to make sure MongoDB is working successfully. Create the below script, add it to your MAMP host’s root directory and run it in your browser by going to (of course, replacing with your MAMP domain and script.php with the the name you saved the test script as).

// Show PHP errors (to make sure code is working)
error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", 2);
// Create a MongoDB connection
$mongodb = new MongoClient("mongodb://localhost");
// Choose the database and collection
$db = $mongodb->your_db_name;
$collection = $db->your_collection_name;

// Save a record to your collection
"name" => "Chris Mallory",
"age" => 29,
"occupation" => "Web Developer"

// Retrieve the record and display it
$record = $collection->findOne();

echo "Hi, my name is " . $record['name'] . ". I'm " . $record['age'] . " years old and work as a " . $record['occupation'] . ".";

If you see no errors and a sentence saying your name, age, and occupation (or mine if you just copy and pasted the above), then you have successfully setup MongoDB to work with MAMP!