This installation is based on Linux Centos 7, using Yum to install MongoDB

Yum source

Use to summarize a few common ones:

/ / 1 installation
yum install package  // Install the specified installation package package

// 2 Updates and upgrades
yum update  // All updates
yum update package  // Update the specified package package
yum check-update  // Check for updatable programs
yum upgrade package  // Upgrade the specified package package

// 3 Find and display
yum info // Lists all packages that can be installed or updated
yum info package // Displays installation package information package
yum list // Displays all installed and installable packages
yum list package  // Displays the installation status of the specified package package
yum search package // Searches for details of packages that match specific characters

// 4 Delete the program
yum remove | erase package  // Delete the package package
yum deplist package  // Check program package dependencies

// 5 Clear the cache
yum clean packages  // Clear the software package in the cache directory
yum clean headers // Clear headers from the cache directory
yum clean oldheaders // Clear the old headers from the cache directory
yum clean, yum clean all  // (= yum clean packages; Yum clean oldheaders) clears packages and oldheaders in the cache directory
Copy the code

Install the mongo

Configure the system yum source

1. Create a. Repo file to generate the mongodb source

Vi/etc/yum. Repos. D/mongo - org - 4.0. RepoCopy the code

2. Add the following 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

A:

Name # baseurl # gpkCheck =1 # Enable =1 # enables the source. Gpgkey # GPG validationCopy the code

3. Save the Settings and exit

Wq # Exit and save the configurationCopy the code

Install MongoDB using yum

1. Install the mongo

sudo yum install -y mongodb-org
Copy the code

2. Verify the installation

rpm -qa |grep mongodb
Copy the code
rpm -ql mongodb-org-server
Copy the code

3. Start the mongo

Starting the MongoDB Service

systemctl start mongod.service
Copy the code

The default MongoDB port is 27017. Check whether the MongoDB port is enabled

netstat -natp | grep 27017
Copy the code

Check whether the database is installed successfully

Ps - aux | grep mongod # to check the database process existsCopy the code

4. Verify that the service is enabled

mongo
Copy the code

Common Commands

// 1. Start MongoDBMongoDB sudo chkconfig mongod on sudo chkconfig mongod on Mongod restart MongoDB// stop MongoDBSudo service mongod stop// 3. Uninstall MongoDBSudo yum erase $(RPM - qa | grep mongo - org) # uninstall mongo sudo rm - r /varSudo rm -r /var/lib/mongo # delete data fileCopy the code

Remote connection to Mongodb

1. Modify the mongodb

Vi /etc/mond. conf # network interfaces net: port: 27017 bindIp: 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

Change the binding IP address 127.0.0.1 by default, only local connections are allowed. Therefore, change the binding IP address to bindIp:0.0.0.0 and exit

2. Restart the mongodb service

sudo service mongod restart 
Copy the code

3. Open external ports

Methods a

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

Method 2

iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 27017 -j ACCEPT
Copy the code

4. Remote connection

The default connection

Mongo 10.128.218.14:27017Copy the code

Connect to a custom user

  1. Create users, set accounts, passwords, and permissions
Admin > use admin switched to db admin > db.createUser({user:"root", PWD :"123456", roles:["root"] }) Successfully added user: { "user" : "root", "roles" : Use test switched to db test > db.createUser({user:"admin", PWD :"123456", roles:["readWrite", "dbAdmin"] }) Successfully added user: { "user" : "root", "roles" : [ "root" ] }Copy the code
  1. Modify the mongodb. Conf file to enable authentication
vi /etc/mongod.conf

security:
  authorization: "enabled"   # disable or enabled
Copy the code
  1. Restart the mongo
sudo service mongod restart 
Copy the code
  1. User authentication
> Use admin switched to DB admin > db.auth("root", "123456") 1 // Authorization succeededCopy the code
Db.updateuser (user, writeConcern) # update db. DropUser ('test') # delete dbCopy the code
  1. The remote connection
/ / terminal connected mongo 10.128.218.14:27017 / database -u username -p password / / mongoose way connection mongoose.connect('mongodb://username:password@host:port/database? options... ', {useNewUrlParser: true}); // Connect through the clientCopy the code

User rights role description

The rules instructions
root Only available in the admin database. Super account, super permission
Read Allows the user to read the specified database
readWrite Allows users to read and write to 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 It is available only in the admin database, giving the user management rights over all sharding and replication set-related functions
readAnyDatabase It is available only in the admin database and is granted the read permission of all databases
readWriteAnyDatabase This parameter is available only in the admin database and is granted read and write permission to all databases
userAdminAnyDatabase Only available in the admin database, the user is granted the userAdmin permission of all databases
dbAdminAnyDatabase Only available in the admin database, the user is granted the dbAdmin permission of all databases

reference

  • Yum use details
  • Install MongoDB Community Edition on Red Hat Enterprise or CentOS Linux
  • CentOS 7 Install and uninstall the MongoDB database
  • Ubuntu mongodb remote connection configuration
  • How to Enable Authentication on MongoDB
  • db.createUser()