Objective
MongoDB is needed in recent projects, and this is a time to learn how to use the database.
Here I first briefly introduce what is MongoDB, the following will be described in detail:
MongoDB is a universal document-oriented database, with MySQL and other relational database common functions. In addition to being able to create, read, update, and delete data, it also provides some extended functionality
such as indexes, aggregations, special collection types (such as collections with limited time), shards, and clusters, is an easy-to-use, easy-to-expand database. It is also because of its characteristics, but also the use of front-end children's shoes to compare
A more database.
First, install MongoDB1.1 in CentOS, introduction
MongoDB has two version types: Open Source Community Edition (Community Edition) and Enterprise Edition (Enterprises), the functionality of the two is no different, the Enterprise Edition is mainly to provide more management services,
There are more advanced security, such as LDAP authentication, Red Hat Identity Management certification. The main discussion here is the Open source Community Edition. The official offers the Mongodb-org software package, which contains almost all of MongoDB
The required features yum
can be easily installed using the system's own package manager .
1.2. Configure the Resource library
To ensure that the system can get the package directly from the specified address, create the /etc/yum.repos.d/mongodb-org-3.4.repo
file , fill in the following content to configure the MongoDB repository:
[mongodb-org-3.4]name=MongoDB repositorybaseurl=https:// repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/gpgcheck=1enabled=1 gpgkey=https://WWW.MONGODB.ORG/STATIC/PGP/SERVER-3.4.ASC
Execute the Install command
sudo yum install-y mongodb-org
II. Launch 2.1, Introduction
After MongoDB is installed, Many commands are generated by default, such as MONGO, Mongod, Mongodump, Mongooplog, and so on .
Among the more commonly used are Mongod (for starting MongoDB servers), MONGO (MONGO Shell Interactive features that provide a powerful JavaScript-like environment).
If you simply start the mongod
MongoDB server by executing a command, you cannot make it run in the background, the console shuts down the service automatically stops, you cannot quickly start the specified configuration of the service, and you cannot do
To boot from the boot. Centos provides service
commands to help us start some services quickly, but it is actually running a specified shell script. when we enter the service command, Linux will go to
/etc/rc.d/init.d
go down and find this script to run , init.d a lot of this script (like Common httpd) below. You can also set the Chkconfig command when we need to set up boot
These system services start when they are powered on.
2.2. Start command and configure boot
1) Start command
After MongoDB is installed /etc/rc.d/init.d
, the startup script Mongod is generated by default, so we can start the MongoDB server with the following command:
sudo service Mongod start
2) If you need to set up boot can execute the following script
sudo chkconfig mongod on
3) See if it starts
Method One:
Use command: NETSTAT-LANP |grep "" 27017 "
Way two:
Accessing the/var/log/mongodb/mongod.log log file, if you see [Initandlisten] waiting for connections on port <port> logs, indicates a successful startup.
Iii. MongoDB Settings 3.1, MongoDB configuration file
Each Mongod server process needs its own data store path, which is/var/lib/mongo by default. If you need to start multiple server processes, you can specify a different path to start.
The default configuration file that is started is/etc/mongod.conf. We can use this configuration file to achieve the desired configuration: the official profile detailed
# mongod.conf# fordocumentation of all options, see:# http://docs.mongodb.org/manual/reference/configuration-options/# whereTo write logging data.systemLog:destination:file logappend:truePath:/var/log/mongodb/mongod.log# Where and how to store Data.storage:dbPath:/var/lib/MONGO journal:enabled:true# engine:# mmapv1:# wiredtiger:# How the process runsprocessmanagement:fork:true# Fork and runinchbackground Pidfilepath:/var/run/mongodb/Mongod.pid # Location of pidfile# network Interfacesnet:port:27017Bindip:127.0.0.1# Listen to LocalInterfaceOnly , comment to listen on all interfaces. #operationProfiling: #replication: #sharding: # # Enterprise-Only Options#auditlog: #snmp:
Attention:
There is a need to be aware of the configuration is Net BINGIP, if set up bindIp: 127.0.0.1
the external network machine cannot directly connect to this server process .
Iv. Authentication Authorization (MongoDB create user Library) 4.1, Introduction
After the MongoDB server process is started by default, there is no authentication mechanism, that is, any user can connect to the database for read and write operations . MongoDB has a very special database admin,
specifically used to store database administrator information . In MongoDB there are different roles, each role can only have the appropriate permissions, users can also customize the role. Official explanation
4.2. Certification Authorization Step
In order to enable the authentication mechanism, we need to create two users: Users who have the useradminanydatabase role, users with the corresponding database read and write permissions ReadWrite , such as:
Start a server process that does not require authentication, and enter the configuration directly with the MONGO shell:
1) Add a super user "root"
Step one: Open the MONGO shell
Mongo
Step Two: Enter the admin table
Use admin
Step three: Create a Super Admin account
Step three: See if the user was created successfully
Indicates that the creation was successful!
Five, MongoDB permissions
1) database User role: Read, ReadWrite
READ: Allows the user to read the specified database
ReadWrite: Allows the user to read and write to the specified database
2) database Management role: DbAdmin, Dbowner, useradmin
DbAdmin: Allows the user to perform administrative functions in the specified database, such as index creation, deletion, viewing statistics, or accessing System.profile
Useradmin: Allows the user to write to the System.users collection to create, delete, and manage users in the specified database
3) Cluster Management role: Clusteradmin, Clustermanager, Clustermonitor, Hostmanager
Clusteradmin: Available only in the admin database, giving the user administrative permissions on all shard and replica set related functions
4) Backup Recovery role: backups, restore
5) All database roles: Readanydatabase, Readwriteanydatabase, Useradminanydatabase, dbadminanydatabase
Readanydatabase: Only available in the Admin database, giving users read access to all databases
Readwriteanydatabase: Only available in the Admin database, giving users read and write access to all databases
Useradminanydatabase: Only available in the admin database, giving the user useradmin permissions for all databases
Dbadminanydatabase: Only available in the Admin database, giving the user dbadmin permissions for all databases.
6) Super User role: Root
. Root: Available only in the admin database. Super account, Super privilege
Note: There are also several roles that indirectly or directly provide access to System Superuser (Dbowner, Useradmin, Useradminanydatabase)
MongoDB (i) Environment construction and initial configuration