The installation

Previously, we installed mongodb via Homebrew and only needed to execute commands

brew install mongodb
Copy the code

But recently I found an error in the installation

website

After downloading the official website, we will get a zip package, we unzip:

Unzip the unzip folder (I renamed it mongoDB) and move it to /usr/local:

Run the command to open the /usr/local directory and drag the decompressed mongoDB

$ open /usr/local
Copy the code

configuration

Once installed, we need to configure mongoDB to start properly

1. First we create a data/db directory under the root directory

$ sudo mkdir -p /data/db
Copy the code

2. Set read and write permissions on /data/db

Select * from user where username = 'username'
$ whoami
->username

# set permissions
$ sudo chown username /data/db
Copy the code

3. Set environment variables

Open the.bash_profile file

$ open ~/.bash_profile
Copy the code

Add configuration: PATH = $PATH: / usr/local/mongo/bin (note that this PATH is directing the PATH of the installation package, don’t write wrong)

Save the Settings and run the command to take effect

$ source ~/.bash_profile
Copy the code

Start the mongo

$ mongod
Copy the code

If a waiting link appears, it starts normally

If an error occurs during startup, the following is displayed:

The dbpath may be incorrect. Setting the DBpath should be ok

$ mongod --dbpath /data/db
Set dbpath before starting
$mongod
Copy the code

If an error persists, check the environment variables. If a permission error is reported, run the following command to start the system

$ sudo mongod
Copy the code

After mongod has successfully started, open a new command window and execute the following command

$ mongo
Copy the code

Exit the mongo

It’s at the mongo window

Stop the Mongod service first$ use admin; $ db,shutdownServer(); Then quit Mongo$exit;Copy the code