Installing and Running MongoDB on OSX

node.jsMongodbMacosShellDatabase

node.js Problem Overview


If someone can provide some insights here I would GREATLY appreciate it.

I am new to MongoDB, and (relatively) new to the command line.

I had a express/node.js app running on MongoDB locally successfully, but upon restarting my computer, I attempted to restart the Mongo server and it began giving errors and wouldn't start. Since then, I have re-installed Mongo several times only to find the same error occurring. this is what I am receiving:

privee:mongodb-osx-x86_64-2.4.6 jonlinton$ ./bin/mongo
MongoDB shell version: 2.4.6
connecting to: test
Mon Aug 26 14:48:47.168 Error: couldn't connect to server 127.0.0.1:27017 at     src/mongo/shell/mongo.js:145
exception: connect failed

Am I missing a step? Should I be creating a config file? Some basic guidance would be really helpful. Thanks in advance for any insights.

node.js Solutions


Solution 1 - node.js

If you have installed mongodb through homebrew then you can simply start mongodb through

brew services start mongodb

Then access the shell by

mongo

You can shut down your db by

brew services stop mongodb

You can restart your db by

brew services restart mongodb

For more options

brew info mongodb

Solution 2 - node.js

Updated answer (9/2/2019):

Homebrew has removed mongodb formula from its core repository, see this pull request.

The new way to install mongodb using Homebrew is as follows:

~> brew tap mongodb/brew
~> brew install mongodb-community

After installation you can start the mongodb service by following the caveats:

~> brew info mongodb-community
mongodb/brew/mongodb-community: stable 4.2.0
High-performance, schema-free, document-oriented database
https://www.mongodb.com/
Not installed
From: https://github.com/mongodb/homebrew-brew/blob/master/Formula/mongodb-community.rb
==> Caveats
To have launchd start mongodb/brew/mongodb-community now and restart at login:
  brew services start mongodb/brew/mongodb-community
Or, if you don't want/need a background service you can just run:
  mongod --config /usr/local/etc/mongod.conf

Deprecated answer (8/27/2019):

I assume you are using Homebrew. You can see the additional information that you need using brew info $FORMULA

~> brew info mongo                                                           255
mongodb: stable 2.4.6, devel 2.5.1
http://www.mongodb.org/
/usr/local/Cellar/mongodb/2.4.5-x86_64 (20 files, 287M) *
  Built from source
From: https://github.com/mxcl/homebrew/commits/master/Library/Formula/mongodb.rb
==> Caveats
To reload mongodb after an upgrade:
    launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

Caveats is what you need to follow after installation.

Solution 3 - node.js

Assuming you have created the data/db directory under bin after install.

  1. Start a terminal for your mongo server

  2. Go to <mongodb-install-directory>/bin directory

  3. Run the command

    ./mongod

  4. Start a terminal for your mongo shell

  5. Go to <mongodb-install-directory>/bin directory

  6. Run the command (make sure you put the name of the database)

    ./mongo test

Solution 4 - node.js

Problem here is you are trying to open a mongo shell without starting a mongo db which is listening to port 127.0.0.1:27017(deafault for mongo db) thats what the error is all about:

Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145 exception: connect failed

The easiest solution is to open the terminal and type

$ mongod --dbpath ~/data/db

Note: dbpath here is "Users/user" where data/db directories are created

i.e., you need to create directory data and sub directory db in your user folder. For e.g say `

> /Users/johnny/data

After mongo db is up. Open another terminal in new window and type

$ mongo

it will open mongo shell with your mongo db connection opened in another terminal.

Solution 5 - node.js

Mac Installation:

  1. Install brew

     ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    
  2. Update and verify you are good with

     brew update
     brew doctor
    
  3. Install mongodb with

     brew install mongodb
    
  4. Create folder for mongo data files:

     mkdir -p /data/db
    
  5. Set permissions

     sudo chown -R `id -un` /data/db
    
  6. Open another terminal window & run and keep running a mongo server/daemon

     mongod
    
  7. Return to previous terminal and run a mongodb shell to access data

     mongo
    

To quit each of these later:

  1. The Shell:

     quit()
    
  2. The Server

     ctrl-c
    

Solution 6 - node.js

For those that could be facing the same problem and the solutions suggested above aren't working, for example in my case, I had installed mongodb-community, so you might wanna run the command below to restart your mongo server.

For those that installed mongodb-community using brew

brew services start mongodb-community

Solution 7 - node.js

additionally you may want mongo to run on another port, then paste this command on terminal,

mongod --dbpath /data/db/ --port 27018

where 27018 is the port we want mongo to run on

assumptions
  1. mongod exists in your bin i.e /usr/local/bin/ for mac ( which would be if you installed with brew), otherwise you'd need to navigate to the path where mongo is installed
  2. the folder /data/db/ exists

Solution 8 - node.js

mongo => mongo-db console
mongodb => mongo-db server

If you're on Mac and looking for a easier way to start/stop your mongo-db server, then MongoDB Preference Pane is something that you should look into. With it, you start/stop your mongo-db instance via UI. Hope it helps!

Solution 9 - node.js

Make sure you are logged in as root user in your terminal.

Steps to start mongodb server in your mac

  1. Open Terminal
  2. Run the command sudo su
  3. Enter your administrator password
  4. run the command mongod
  5. MongoDb Server starts

Hope it helps you. Thanks

Solution 10 - node.js

Nothing less likely to be outdated that the official docs: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

Solution 11 - node.js

Download MongoDB and install it on your local machine. Link https://www.mongodb.com/try/download/enterprise

Extract the file and put it on the desktop. Create another folder where you want to store the data. I have created mongodb-data folder. Then run the below command.

Desktop/mongodb/bin/mongod --dbpath=/Users/yourname/Desktop/mongodb-data/

Before the hyphen is the executable path of your mongoDB and after hyphen is your data store.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2517788View Question on Stackoverflow
Solution 1 - node.jsgaurav.singharoyView Answer on Stackoverflow
Solution 2 - node.jsRixView Answer on Stackoverflow
Solution 3 - node.jsJesus RuizView Answer on Stackoverflow
Solution 4 - node.jssecretgenesView Answer on Stackoverflow
Solution 5 - node.jsjasonleonhardView Answer on Stackoverflow
Solution 6 - node.jsFahd JamyView Answer on Stackoverflow
Solution 7 - node.jsEdwin O.View Answer on Stackoverflow
Solution 8 - node.jskarthiksView Answer on Stackoverflow
Solution 9 - node.jsPiyush BansalView Answer on Stackoverflow
Solution 10 - node.jsdrjorgepolancoView Answer on Stackoverflow
Solution 11 - node.jsrajiv patelView Answer on Stackoverflow