Online Learning System
 
STUDENT
 
FACULTY
 
SCHOOL
 
SUPPORT
 
PUBLIC
 
SIGNUP
DAILY QUIZ
 
     
  B U L L E T I N    B O A R D

Installation of mongoDB for Windows and MacOS

(Subject: Database/Authored by: Liping Liu on 8/23/2022 5:00:00 AM)/Views: 5661
Blog    News    Post   

Installation of mongoDB for Windows

  • Download the MongoDB installer file from https://www.mongodb.com/download-center/community. The current version is 5.0.9 and the package type is msi. 

  • Find the downloaded .msi file in your Download folder. Double click the file and follow the prompts to install Mongo. Choose Complete as Setup Type:

  

 

  • Create data and log folder, e.g., c:\MongoDB\Server\5.0\data and c:\MongoDB\Server\5.0\log and then choose Run MongoDB as Service and Run service as Network Service user. Specify custom directories for data and logs (see below):  

  • You may choose to install MongoDB compass, a GUI client for access MongoDB server. 

 

 

  • Start the mongodb daemon : Restart your computer, or go to Start, type services, and then open Services app. There you should see MongoDB Server is running. Otherwise, start it manually there. 

  • Create a path to MongoDB installation folder, which is by default at c:\programs files\MongoDB. In the Windows Search box, type in “env”, and choose “Edit the system environment variables”. Then press Environment Variables button . 

 

 

  • Find Path in System Variables, and then press Edit button:

 

 

  • Press Add button and then Browse button to navigate to C:\Program Files\MongoDB\Server\5.0\bin directory and then open. You will add a path variable (Note: make sure you browse to the directory if you installed a different version of MongoDB or to a different location. For example, if you installed version 4.2, then you need to replace 5.0 to 4.2):

 

 

  • Logout or Restart your computer to make the path variable activated.

  • Now connect to MongoDB using the Mongo shell: While the MongoDB daemon is running, go to Start, type cmd, and run command prompt. Then type mongo to connect to your local MongoDB server.

  • After your connect to mongoDB using command mongo, type the following commands to see if everything works:
    
    > use employees;
    
    > db.departments.insertOne({deptno:10, dname:"Marketing"})
    
    > db.departments.find()
    > use orders;
    
    > db.customers.insertOne({cid:1001, cname:"John Doe"})

    Congratulation’s You have successfully installed mongodb server on your system.

  • Enable Access Control to MongoDB Databases: By default, the above installation will create and run MongoDB as a windows service without security control: anybody can connect to the database and perform any operation on it. To secure your database and allow only authenticated users to access and perform authorized actions, you will need to follow the following three steps to re-create the windows service:
  • First, while the default service is running, connect to MongoDB shell by command "mongo --port 27017" or "mongosh --port 27017" and then switch to admin database to create an admin account as follows. Note a list of MongoDB built-in rules are read, readWrite, dbAdmin, dbOwner, and userAdmin for a single database, and readAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabase, and dbAdminAnyDatabase for all databases. 

use admin
db.createUser({
    user: "admin",
    pwd: "password", // or passwordPrompt()
    roles: [
    { role: "userAdminAnyDatabase", db: "admin" }, 

    { role: "dbAdminAnyDatabase", db: "admin" },
    { role: "readWriteAnyDatabase", db: "admin" }
    ]
})

    db.createUser({
        user:"scott",
        pwd:"tiger",
        roles: [
            {role:"read", db:"orders"},
            {role: "dbOwner", db:"employees"}
           ]
        })
  • Exit the mongoDB shell, and run the command prompt as an administrator. First, remove the default windows service:

   mongod --remove

  • Then re-create a new windows service that enable authentication,and start the service:

    mongod --auth --port 27017 --bind_ip 0.0.0.0 --dbpath=C:\MongoDB\Server\5.0\data --logpath=C:\MongoDB\Server\5.0\log\mongod.log --serviceName MongoDB --install

    net start MongoDB

Test Connection to MongoDB

We can test the above installation using either Mongo shell or MongoDB compass. To connect to mongoDB shell, you will need to use the following command and enter password as prompted:

    mongo --port 27017 --host localhost -u admin -p password --authenticationDatabase admin

 We can replace the localhost to another one such as cobismcourses.uakron.edu by using the testing scott/tiger account as follows:

    mongo --port 27017 --host cobismcourses.uakron.edu -u scott -p tiger --authenticationDatabase admin

To test connections using MongoDB compass, make sure you use Advanced Connection Options to enter host name, port number, and user account as show below. Replace cobismcourses.uakron.edu by localhost, you can connect to your own MongoDB server, which was installed above. 

Installation of MongoDB Database Tools

The MongoDB Database Tools are a collection of command-line utilities for working with a MongoDB deployment. These tools release independently from the MongoDB Server. For example, in order to use GridFS for file management, we need to install the tools. 

Open the MongoDB Download Center. Using the drop-down menu on the right-hand side of the page:

  1. Select the Windows x86_64 Platform
  2. Select the msi Package
  3. Click the Download button

Double-click the downloaded MSI installer to install the Database Tools. During the install you may customize the installation directory if desired.

You may wish to make the Database Tools available in your system's PATH environment variable, which allows referencing each tool directly on the command prompt by name, without needing to specify its full path, or first navigating to its parent directory.

Once you've installed the Database Tools, follow the instructions below to add the install directory to your system's PATH environment variable.:

  1. Open the Control Panel.
  2. In the System and Security category, click System.
  3. Click Advanced system settings. The System Properties modal displays.
  4. Click Environment Variables.
  5. In the System variables section, select Path and click Edit. The Edit environment variable modal displays.
  6. Click New and add the filepath (e.g., c:\Program Files\MongoDB\Tools\100\bin) to the location where you installed the Database Tools or use Browse button to select the path
  7. Click OK to confirm your changes. On each other modal, click OK to confirm your changes.
  8. Logout or restart the computer

Once set, you can run any of the Database Tools directly from your command prompt. Consult the reference page for the specific tool you wish to use for its full syntax and usage.

 

Installation of Cmder and Atom on Windows (optional)

In order to run mongoDB commands, it is convenient to have access to a command line tool and a command editor. Cmder and Atom allow us to have both in one place. 

First, download Cmder full version from https://cmder.net and then unzip it to c:\cmder folder. You can start using cmder by clicking cmd.exe file.

To install ATOM, go to atom.io to download Windows Installer. Run the downloaded AtomSetup.exe file. Atom will launch after the installation is complete. After installation, you will see

  • An Atom desktop shortcut is added
  • An Open With Atom context menu is added to the Explorer for selected files and folders
  • atom and apm are added to the Path environment variable so they can be run from the Command Prompt or PowerShell.

To Iintegrate Cmder into ATOM:

  • Open Atom
  • Go to File --> settings
  • Find the package Platformia-ide-terminal (install it if you do not have it yet): 

 

  • Click on the setting button for Platformia-ide-terminal (see below) and enter c:\cmder\vendor\\init.bat as Command to run on terminal initialization and C:\Windows\system32\cmd.exe as Shell Override:

 

 
Manual Installation of MongoDB on MacOS
 
 
Note: MongoDB 5.0 Community Edition removes support for macOS 10.13. You will need to upgrade your MacOS to 10.14 or above.

You can install MongoDB using two different methods one is using .tgz tarball and another is using Homebrew. Here, we will discuss how to install MongoDB using .tgz tarball. Follow each step carefully : 
  • Go to mongodb-community and select the version 5.0.11. Make sure you choose macOS as the platform and 'tgz' as the file format and click the download button.
  • Once the tgz file is downloaded, go to the macOS terminal  (open Finder and go to Applications/Utility folder) to extract it. Most likely, your MongoDB will get downloaded in the Downloads folder. For this, type the following command in the terminal (change file name after tar command according to the file you downloaded):
cd Downloads/ 
ls 
tar xzf mongodb-macos-x86_64-5.0.11.tgz

  • Now, let us move MongoDB folder to your home folder. This will ask for your system password. Provide the password.
sudo mv mongodb-macos-x86_64-5.0.11 ~/mongodb
  • Next, you have to create the db folder. By default, MongoDB writes or stores the data in the folder called /data/db, but newer MacOS disallowed it. So let use set it data/db inside your home folder. The command for this will be 

sudo mkdir -p ~/data/db
sudo chown $USER ~/data/db

Note: For changing the permission, you need to know your username first. To know your username, type the command: whoami
  • In order to run mongo shell we need to set an environment variable or path. You can set your path in .bash_profile file If you are using Mojave OS or below, or you can set your path in .zprofile file if you are using Catalina or above. If you do not have .bash_profile or .zprofile, then you can create them inside the terminal by issuing the following commands:

nano ~/.bash_profile

   At the end of the opened .zprofile file, enter the following line:

     export PATH="~/mongodb/bin:$PATH"

  Press CTR+ X to exit, and then run the .bash_profiel file using command or simply logout and re-login your Mac:

source ~/.bash_profile

  Now you can run mongodb server using the command:

sudo mongod --dbpath ~/data/db

Here sudo mongod –dbpath ~/data/db command to start mongod server and ~/data/db is data directory. Note that your terminal will show a lot of messages in JSON format about the server status and responses. Don't close the terminal or the mongodb server will stop.

Run Mongo Shell to access the mongoDB server.  Oen a new terminal window to mongo shell by the command command

            mongo     

Now you are connected to your running mongodb server. Type quit() to exit the mongo shell   

Important Point: For Catalina users when you run mongo or mongo –nodb command in your terminal, your macOS would not allow to run mongo. So, to run mongo you need to give them permission manually, i.e, System Preferences > Security & Privacy > Allow Anyway. Same procedure to run mongod. 


           Register

Blog    News    Post
 
     
 
Blog Posts    News Digest    Contact Us    About Developer    Privacy Policy

©1997-2024 ecourse.org. All rights reserved.