MongoDB is a database based on distributed file storage. Written by C++ language. Designed to provide scalable high-performance data storage solutions for WEB applications.
MongoDB is a product between relational and non-relational databases. It is the most versatile and most like relational database among non-relational databases. The data structure it supports is very loose, and it is a bson format similar to json, so it can store more complex data types.
The biggest feature of Mongo is that the query language it supports is very powerful. Its syntax is similar to an object-oriented query language. It can almost achieve most of the functions similar to single-table queries in relational databases, and it also supports indexing of data.
The following describes the installation of MongoDB under CentOS7
1、 Download the rpm installation package of the RHEL7 version of MongoDB Server and the rpm installation package of the MongoDB shell from the official website
https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/RPMS/mongodb-org-server-4.2.1-1.el7.x86_64.rpm
https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.2/x86_64/RPMS/mongodb-org-shell-4.2.1-1.el7.x86_64.rpm
2、 rpm -ivh to install
rpm -ivh mongodb-org-server-4.2.1-1.el7.x86_64.rpm
rpm -ivh mongodb-org-shell-4.2.1-1.el7.x86_64.rpm
3、 Start MongoDB service
systemctl start mongod.service
And set it to start systemctl enable mongod.service
The default listening port of the MongoDB database is 27017
Enter mongo to log in to the database and enter the shell command line
4、 MongoDB commonly used commands
show dbs: View the database list;
db: View the current database;
db.createCollection('The name of the table to be created'): Create a new table;
show collections: View the tables under the current database;
db.table name.drop(): delete the specified table in the current database
db.dropDatabase(): delete the current database
show users: View all users in the current library
db.cloneDatabase("127.0.0.1"): Clone the data of the database on the specified machine to the current database
db.copyDatabase("mydb", "temp", "127.0.0.1"): Copy the data of the local mydb to the temp database
db.repairDatabase(): Repair the current database
db.getName(): View the currently used database
db.stats(): Display the current db status
db.version(): current db version
db.getMongo(): View the link machine address of the current db
db.serverStatus().connections.current: View the current number of connections
5、 Create administrator and database accounts
use admin
db.createUser({
user:"admin",
pwd:"P@ssw0rd",
roles:[{ role:"root", db:"admin"},{ role:"userAdminAnyDatabase", db:"admin"}]})
Create a test database Lab and the database account
use Lab
db.createUser({
user:"yuanfan",
pwd:"lab@2019",
roles:[{ role:"dbOwner", db:"Lab"}]})
And insert the test data db.Lab.insert({"name":"yuanfan"})
The default database in MongoDB is test. If you do not create a new database, the collection will be stored in the test database.
Note: In MongoDB, the collection will only be created after the content is inserted, that is to say, after creating the collection (data table), insert a document (record) before the collection is actually created
6、 Turn on account authentication and remote access
[ root@VM_Server ~]# cp /etc/mongod.conf /etc/mongod.conf_bak
[ root@VM_Server ~]# vi /etc/mongod.conf
Modify the following two places
bindIp:0.0.0.0
security:
authorization: enabled
Remember, all colons in the configuration file must be followed by a space!
Do not write in the top box on the second line, add two spaces
Then restart the mongod service systemctl restart mongod.service
7、 Log in with account and password when logging in by shell
mongo -u "yuanfan"-p "Lab@2019"--authenticationDatabase "Lab"
8、 Use database management visualization tools
1) E.g. Navicat
Robo3T integrates the mongoDB shell, you can right-click the database to open the Shell
Recommended Posts