Create a. Repo file to generate the mongodb source

Vi/etc/yum. Repos. D/mongo - org - 4.0. Repo

Adding Configuration Information

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

The installation

Choose to download

  • Sudo yum install -y mongodb-org

  • Sudo yum install -y mongodb-org-4.0.11 mongodb-org-server-4.0.11 mongodb-org-shell-4.0.11 mongodb-org-mongos-4.0.11 Mongo – org – tools – 4.0.11

  • Exclude = mongodb-org, mongodb-org-server, mongodb-org-shell, mongodb-org-mongos, mongodb-org-tools

Verify the installation

rpm -qa |grep mongodb rpm -ql mongodb-org-server

Start the

Start the systemctl start mongod

Open port netstat natp | grep, 27017

View the process of ps – aux | grep mongod

Verify the mongo

Common commands

MongoDB sudo chkconfig mongod on // 1, MongoDB sudo chkconfig mongod on // run the following command to restart the MongoDB service: // run the following command to restart the MongoDB service $(RPM - qa | grep mongo - org) # uninstall mongo sudo rm - r/var/log/mongo sudo rm # delete log files - r/var/lib/mongo # delete data fileCopy the code

The remote connection

1. Modify the configuration

vi /etc/mongod.conf

# network interfaces
net:
  port: 27017
  #127.0.0.1 Can only be connected to hosts. Set this parameter to 0.0.0.0BindIp: 0.0.0.0 # Enter 0.0.0.0,: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.Copy the code

2. Restart

sudo service mongod restart

3. Open the port

Systemctl status firewalld # check firewall status firewall-cmd --zone=public --add-port=27017/ TCP --permanent Firewall-cmd --reload # reload firewall firewall-cmd --zone=public --query-port=27017/ TCP # Check whether the port number is opened successfullyCopy the code

4. Remote connection

mongo your server address:27017

Set the password

1 (Create) Enter the admin database

use admin

2. Create an administrator account

db.createUser({ user: "root", pwd: "123456", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })

3. The test

Db. Auto (‘root’,’123456′) returns 1

4. Enable authentication

Sudo vi /etc/mongod. Conf modify #security

security:
  authorization: enabled
Copy the code

5. Restart the service

sudo service mongod restart

6. Test the admin database

Auth (‘root’,’123456′), run show users again to display the required information.

7. Create another database password

Auth (‘root’,’123456′). Then switch to the test database use test, that is, create user permissions on the corresponding database. Create a user with write/write permissions on test database and read-only permissions on other databases. db.createUser({user:’test’,pwd:’test’,roles:[{role:’readWrite’,db:’test’},{role:’dbAdmin’,db:’test’},’read’]})

8. Test other databases

Exit Exit data and re-enter mongo,use test to enter test database, show collections attempts to query the collection, and an error is reported. Db.auth (‘test’,’test’) verifies login and queries collection show Collections again without an error (no information is displayed if there is no collection).

9. Test the remote connection

Mongo your server address:27017/test

10. Complete ~ ~

Character sheet

classification Role (s) Brief description
DB User Roles read readWrite Create a user for a database and assign read and write rights to the database
DB Admin Roles dbAdmin dbOwner userAdmin Have the authority to create databases and create users
Culster Administration Roles clusterAdmin clusterManager clusterMonitor hostManager Administrator group that manages the entire system
Backup and Restoration Roles backup restore Back up and restore the database
All-database Roles readAnyDatabase readWriteAnyDatabase userAdminAnyDatabase dbAdminAnyDatabase You have the permission to perform operations on admin
Roles of Superuser (Super administrator) root DbOwner userAdmin userAdminAnyDatabase These roles provide the ability to have any permissions on any data for any user. Users with this role can define their own permissions on any database

Role description

  • Read: Allows the user to Read the specified database
  • ReadWrite: allows users to read and write specified databases
  • DbAdmin: allows users to perform administrative functions in a specified database, such as index creation, deletion, viewing statistics, or accessing system.profile
  • UserAdmin: Allows users to write to the System. users collection and to create, delete, and manage users in the specified database
  • ClusterAdmin: Available only in the Admin database, it grants the management rights to all sharding and replication set-related functions.
  • ReadAnyDatabase: only available in the admin database, granting the user read permission for all databases
  • ReadWriteAnyDatabase: This parameter is available only in the admin database and is granted read and write permissions to all databases
  • UserAdminAnyDatabase: Only available in the admin database, granting the user the userAdmin permission for all databases
  • DbAdminAnyDatabase: only available in the admin database, giving the user the dbAdmin permission for all databases.
  • Root: available only in the admin database. Super account, super permission

Role of operation

User Management Methods — MongoDB Manual