Install MongoDB4.2 on CentOS7
Reference
Official website Product manual NoSQLAdd REPO file
Use vi or vim to edit the /etc/yum.repos.d/mongodb-enterprise.repo file, copy the following content to the file, then save and exit. In the example, version 4.2 is installed. The latest version is 4.4. If you need to install the latest version, please modify 4.2 to 4.4 in the example.
[mongodb-enterprise]
name=MongoDB Enterprise Repository
baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/4.2/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
Perform the installation operation and start the service
yum install -y mongodb-enterprise
systemctl enable mongod
systemctl start mongod
Create an administrator account
Mongodb is connected anonymously by default. If we need to enable login authentication, we must first create a login account and authorize it in the anonymous connection state. If you directly turn on the login verification without creating an account first, then we will not be able to connect to the mongodb service. In the following example, we create an account with an account of my_account and a password of my_password, and authorize it as the database administrator.
use admin;
db.createUser({user:"my_account",pwd:"my_password",roles:[{role:"userAdminAnyDatabase",db:"admin"},"readWriteAnyDatabase"]});
exit
Modify the configuration file and enable login verification
Use vi or vim to edit /etc/mongod.conf, modify the bound IP and port, and then enable login authentication. For details, please refer to the following example.
net:
port: 27017
bindIp: 0.0.0.0
security:
authorization: enabled
Restart the service and verify that the modification takes effect
After restarting the service, we connect to mongodb. Note that the connection can be successfully established at this time. When we execute the command, we will prompt for authorization. Then it means that we have successfully turned on the login authorization. Next, we use the account we just created for authorization. After the authorization is successful, we can perform any operation on the database. Refer to the example below for the commands used.
systemctl restart mongod
mongo
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
use admin;
db.auth('my_account','my_password');
Create your own data
mongodb does not need to execute the command to create a database, it can be created automatically by directly using the database name that does not exist. We usually do not recommend using an administrator account to connect to the database, so we also need to re-create an account belonging to the new database. In the following example, we create a database named my_db and create an account for it.
use my_db;
db.createUser({user:"my_db_admin",pwd:"my_db_password",roles:[{role:"dbAdmin",db:"my_db"},"readWrite"]});
Database backup and restore
The mongodb database backup file is multiple files stored in a directory, and the backup result of the relational database that we usually use is an independent file. Therefore, after the backup is completed, we usually need to compress it into a separate file with a compression tool and then copy and transfer it. The following is an example of backing up and restoring the database.
mongodump -u my_db_account -p my_db_password -d my_db -o /home
mongorestore -d my_db -u my_db_account -p my_db_password --drop /home/my_db/