How To Install Mongodb On Mac

Mongodb is most popular today, it is a no-sql database. This article will tell you how to install it on MacOS correctlly.

1. Install Mongodb On Mac.

  1. Download Mongodb for MacOS version.
  2. Unzip the download file to a local folder.
  3. Add Mongodb bin folder into the PATH environment variable value with following steps in terminal.
    # Go to user home directory.
    192:~ $ cd ~
    # Edit user profile file, add Mongodb bin folder to the PATH env variable value.
    192:~ $ vim .bash_profile
    # Make environment variable change take effect.
    192:~ $ source .bash_profile
    # Display new environment variable to verify the PATH value has been changed.
    192:~ $ env

2. Start Mongodb Server.

  1. Open a terminal and run mongod command in it.
    192:~ $ mongod
  2. If you meet bellow error message, that means it can not find the /data/db folder, mongodb need this folder to run.

    exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
    mongodb initialize data directory not found error

  3. Create /data/db folder with below command in terminal.
    sh-3.2# mkdir /data
    sh-3.2# cd data
    sh-3.2# mkdir db
  4. Now run mongod command in terminal again, the mongodb server will start successfully.
  5. The db server used default port number is 27017.

3. Connect Mongodb.

  1. Now the mongodb server has been started successfully.
  2. You can run mongo command in a terminal to connect it.
    $ mongo
    # list all databases.
    > show databases
    admin         0.000GB
    config        0.000GB
    local         0.000GB
    test_db       0.000GB
    user_account  0.000GB
    # switch to one database.
    > use user_account
    switched to db user_account
    # list current database collections.
    > show collections
    # get the documents saved in this collection.
    > db.account_info.find()
    { "_id" : ObjectId("5f660745c658b4bb177dde73"), "user_name" : "hello", "password" : "haha", "email" : "[email protected]" }
  3. Besides command, you can also use MongoDB GUI client MongoDB Compass to manage the no-sql database.
  4. Download Compass to local folder and install it.
  5. It will add an icon in macOS desktop, click to open it.
  6. Select None in the Authentication drop down list then click Connect button.
    mongodb manager compass login page
  7. After successfully login, you can click Create Database button to create a database. Input database name and collection name. In this example, i create database user_account and the collection name is same.
    mongodb compass create database user_account
  8. Click database user_account to go into it, then click user_account collection, now you can insert, update, delete or query document in it.
    insert document in mongodb manager compass
  9. After insert a document, you can edit the fields in the document. Just click the edit button at record right, then click the plus button at each line beginning. There will popup Add Field After menu item. Click it to add a field. After edit document, click UPDATE button to save the changes.
    mongodb add field in document
0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x