DEV Community

Anthony Slater
Anthony Slater

Posted on

Setting up MongoDB

It's been a while since I fired up MongoDB, so I opened up a terminal and typed mongo thinking everything would be fine. Instead macOS Catalina gave me some grief about the developer being unrecognized...

The solution: Homebrew

>> brew tap mongodb/brew
>> brew install mongodb-community
Enter fullscreen mode Exit fullscreen mode

Next I added the /data/db folder in System/Volumes/Data

>> sudo mkdir -p /System/Volumes/Data/data/db
>> sudo chown -R `id -un` /System/Volumes/Data/data/db
Enter fullscreen mode Exit fullscreen mode

The first command creates the folders and the second changes its permissions.

Finally

Here are the commands to start and stop MongoDB as a background service:

>> brew services run mongodb-community
>> brew services stop mongodb-community
Enter fullscreen mode Exit fullscreen mode

Unless you love typing things, edit your .bash_profile and set some aliases for the above commands.

Once MongoDB is running, you can type mongo in the terminal to start the mongo shell or run a Node.js program.

var MongoClient = require('mongodb').MongoClient;

// Connect to the db
(async () => {
  const client = await MongoClient.connect('mongodb://localhost:27017', {useUnifiedTopology: true });
  const dbs = await client.db().admin().listDatabases();
  console.log(dbs)
  client.close();
})();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)