Author: Han Qingxin

The installation

1. Configure the official MongoDB yum to install the latest MongoDB version

Create the corresponding file:

Vim/etc/yum. Repos. D/mongo - org - 4.2. RepoCopy the code

Write the following configuration items:

[mongo - org - 4.2] name = directing a Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.1/x86_64/ gpgcheck = 1 enabled = 1 gpgkey=https://www.mongodb.org/static/pgp/server-4.2.ascCopy the code

2. Download

Download the latest stable version by default:

sudo yum install -y mongodb-org-unstable
Copy the code

You can also customize the download version:

Sudo yum install - y mongo - org - unstable - 4.1.9 | | mongo - org - unstable - server - 4.1.9 | | mongo - org - unstable - the shell - 4.1.9 | | mongo - org - unstable - mongos - 4.1.9 | | mongo - org - unstable - tools - 4.1.9Copy the code

The second configuration

1. Edit the default configuration file

vim /etc/mongo.conf
Copy the code

2. Common configurations

SystemLog: quiet: false # MongoDB tries to minimize the number of logs to be run in quiet mode. File # log output destination, if the value is the file, the file type, you must specify the path the value of the path: / var/log/mongo/mongod log # log path, the value is the default path logAppend: If the value is false, the original logs will be backed up and the log file verbosity: 2 will be added. The value ranges from 0 to 5. The default value is 0, that is, the simplest log file. The maximum is 5, the most detailed log. Journal: enabled: true # Whether to enable persistent log storage. The default value is true for 64-bit systems. The default value is false for 32-bit systems. Port: 27017 # TCP port that MongoDB listens to. Default is 27017. 0.0.0.0(::,0.0.0.0) to allow all IPv4(IPv6) linksCopy the code

For details about other configurations, see official Configuration

Three start

1. Start the

sudo service mongod start
Copy the code

2. Check whether the service is started

sudo chkconfig mongod on
Copy the code

3. Close the

sudo service mongod stop
Copy the code

4. Restart

sudo service mongod restart
Copy the code

5. Start the MongoDB cli tool

mong
Copy the code

6. In the MongoDB command line tool, create a user name and password for the specified database

> db.createUser(
  {
    user: 'userName',
    pwd: 'password',
    roles: [{role: 'readWrite', db: 'database'}]
  }
)
Copy the code

The above command creates a user with permission ‘readWrite’ for database’ database’

For other precautions, refer to the official link

Four connection

Connect using the connection string

uri: mongodb://[userName]:[password]@host:port/database

The following is an example of the username and password created in Step 6 of the previous section:

Mongo: / / userName: [email protected]:27017 / database