One: Use the installation package to install

Website address: www.mongodb.com/try/downloa…

   

Using curl to install

# enter CD/usr/local/usr/local # download sudo curl - O https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-4.4.3.tgz # decompression sudo Tar -zxvf mongodb-macos-x86_64-4.4.3. TGZ # rename to sudo mv mongodb-macos-x86_64-4.4.3Copy the code

Create directory for storing logs and data:

  • Data storage path:

    sudo mkdir -p /usr/local/var/mongodb
    Copy the code
  • Log file path:

    sudo mkdir -p /usr/local/var/log/mongodb
    Copy the code

Next, make sure that the current user has read and write permission to the above two directories:

sudo chown root /usr/local/var/mongodb
sudo chown root /usr/local/var/log/mongodb
Copy the code
  1. Root The user name of the computer needs to be changed

  2. Next we start mongodb in the background with the following command:

    mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork
    Copy the code
    • –dbpath Sets the directory for storing data

    • –logpath Sets the directory for storing logs

    • –fork runs in the background

    • Check whether the Mongod service is started:

      ps aux | grep -v grep | grep mongod
      Copy the code

      Using the above command, if you see a record of Mongod, it indicates that the operation is successful.

      After startup we can use the mongo command to open a terminal:

      $CD/usr/local/mongo/bin $. / mongo mongo shell version v4.0.9 connecting to: mongo: / / 127.0.0.1:27017 /? gssapiServiceName=mongodb Implicit session: session { "id" : UUID(" 3C12BF4F-695C-48B2-b160-8420110ccDCf ")} MongoDB server version: 4.0.9...... > 1 + 1 2 >Copy the code

Three: Add environment variables

    sudo vi ~/.bash_profile

 source .bash_profile

Mongod –version: Check whether it was added successfully

Four: Start the service

     sudo mongod

After successful startup:

1. Create a super administrator account

use admin db.createUser({

    user:’admin’,

    pwd:’123456′, roles:[{role:’root’,db:’admin’}]

}) 

Navicat connects to the database

Five: BUG fixes

The following error occurs because the mongodb process was killed with kill -9 the previous day

The next day I rebooted and found the Mongod was unusable

zsh: command not found:mongod

then

cd /usr/local/mongodb/bin

sudo ./mongod

Data directory/Data /db not found., terminating

Create sudo mkdir /data sudo mkdir /data/db

Object Syntax Max Access Description Impleme nted Specifica tions mkdir: data: read-only File system Sip needs to be disabled on this machine

6. Disable SIP on the host (system integrity protection)

Enter CSrutil status and return to System Integrity Protection Status: enabled. SIP is enabled.

Restart the computer and holdcommand + RUntil the system recovery interface is entered, then click Utility to select terminal:



The inputcsrutil disableShut down the SIP:

7: Obtaining permission

Sudo mount -uw/is available to create a folder in the root directory. If there is still an error, please try the following steps:

  • The desktop uses Shift + Command + C to access the computer disk

  • Right – click the Macintosh HD disk and select Display Introduction

  • In share and permission, add your own user as the administrator and set read and write permissions

  • Recreate the folder

Finally: delete /data/db/mongod. Lock

Sudo mount -uw/Mount write permission

Sudo rm -rf /data/db/mongod. Lock delete files

Sudo chown username /data/db // Add permission to the user

And then you can start it again

To add the configuration of environment variables to the ~/. ZSHRC file: export PATH=/usr/local/mongodb/bin:$PATH

sudo vi ~/.zshrc

source .zshrc

Otherwise, you need to configure the global path each time you reboot

Eight: Error repetition

Address is already in use when the database is started locally

Sudo lsof -I :27017

End the process sudo kill PID

Before the start of view mongod ps – ef | grep mongo

Exit mongodb

      use admin; 

     db.shutdownServer();