How To Install MongoDB On Mac

MongoDB is most popular today, it is a NoSQL database. This article will tell you how to install it on macOS correctly.

1. Install MongoDB Server On macOS.

  1. Download MongoDB for the macOS version.
  2. Unzip the download file to a local folder.
  3. Add MongoDB bin folder into the PATH environment variable value with the following steps in the 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
    PATH=/Users/zhaosong/Downloads/mongodb-osx-x86_64-4.0.2/bin:

2. Start MongoDB Server.

  1. Open a terminal and run mongod command in it.
    192:~ $ mongod
  2. If you meet the below error message, that means it can not find the /data/db folder, MongoDB needs this folder to run.
    exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
  3. Create the /data/db folder with the below command in the 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 To MongoDB Server.

  1. Now the MongoDB server has been started successfully.
  2. You can run the command mongo in a terminal to connect to 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
    account_info
    >
    # 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 NoSQL database.
  4. Download Compass to a local folder and install it.
  5. It will add an icon to the macOS desktop, click the icon to open it. It will open the MongoDB Compass – Connect dialog window.
  6. Click the New Connection item in the left panel, then you can input the connection properties data in the right Connect to Host panel.
  7. The database connection properties include Hostname ( localhost ), Port ( 27017 ), SRV Record, Authentication ( None ), Replica Set Name, Read Preference ( Primary ), SSL ( None ), SSH Tunnel ( None ), Favorite Name.
  8. Select None in the Authentication drop-down list then click the Connect button.
  9. After successfully login, you can click the Databases tab Create Database button to create a database. Input database name and collection name. In this example, I create a database user_account and the collection name is the same.
  10. Click the database user_account in the left panel to go into it, then click user_account collection, now you can insert, update, delete or query documents in it.
  11. After inserting a document, you can edit the fields in the document. Just click the edit button on the record right side, then click the plus button at each line beginning. There will popup Add Field After menu item. Click it to add a field. After changing the document, click the UPDATE button to save the changes.
    mongodb-add-field-in-document
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x