About MongoDB
MongoDB is a cross-platform, document-oriented database. Can achieve high performance, high availability, and can easily scale. is a distributed file storage based open source database system, under high load situation, add more nodes, can guarantee server performance.
Features of MongoDB
- MongoDB features include collection-oriented storage, schema freedom, rich query statements and multilevel indexes, replication set mechanisms, easy horizontal scaling, pluggable storage engines, cross-platform multi-language support, and more.
- MongoDB is simple to install and provides a document-oriented storage function that is easier to operate.
- MONGODB provides replication, high availability, and automatic sharding capabilities. If the load increases (requiring more storage space and greater processing power), it can be distributed across other nodes in the computer network, which is called sharding.
- The MONGO supports rich query expressions. Query directives use a JSON-style tag to easily query objects and arrays embedded in the document.
- MongoDB supports a variety of programming languages: Ruby, Python, Java, C + +, PHP, C #, and more.
MongoDB Areas of Application
MongoDB can provide scalable, high-performance data storage solutions for Web applications. MongoDB's main areas of application are Web site data, distributed scenarios, data caching, and JSON document format storage. For Internet applications with large data volumes, high concurrency, and weak transactions, the built-in horizontal scaling mechanism provides data processing capabilities from millions of to 1 billion levels, which can be used to meet the requirements of Web2.0 and mobile Internet application storage.
The storage structure of MongoDB
The storage structure of MongoDB consists of logical storage and physical storage.
The logical structure of MONGODB consists of three parts: Document, Collection (collection) and database. The document is the core concept of MongoDB, which is the smallest unit of the MongoDB logical storage, which is equivalent to a row of records in a relational database, a collection of multiple documents, a collection equivalent to the concept of a table in a relational database, and multiple collections to form a database.
The physical storage structure of MongoDB mainly includes data storage and log storage.
Installation and Operation control
(1) Configuring the Yum Source repository
[[email protected] ~]# vim /etc/yum.repos.d/mongodb.repo [mongodb-org]name=MongoDB Repositorybaseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc[[email protected] ~]# yum list
(2) Installing MongoDB
[[email protected] ~]# yum install mongodb-org -y[[email protected] ~]# vim /etc/mongod.conf //修改主配置文件// net: port: 27017 //监听端口// bindIp: 0.0.0.0 //监听地址//
1) Start the MongoDB service and view the port information
[[email protected] ~]# systemctl start mongod.servicev[[email protected] ~]# netstat -anpt | grep 27017tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 14604/mongod
2) Connect and access the database
[[email protected] ~]# /usr/bin/mongo....//省略2018-07-17T09:54:54.595+0800 I CONTROL [initandlisten] > > db.version() //查看版本信息//3.6.6> show dbs; //查看数据库//admin 0.000GBconfig 0.000GBlocal 0.000GBschool 0.000GB> db.getMongo() //查看当前数据库机器的连接地址//connection to 127.0.0.1:27017
(3) Open multi-instance
1) In the case of a single server with sufficient resources, you can use multiple real columns to fully use the server resources. The steps are as follows:
cp -p /etc/mongod.conf /etc/mongod2.conf //复制主配置文件//vim /etc/mongod2.conf //修改主配置文件// path: /data/mongodb/mongod2.log //日志存放位置// dbPath: /data/mongodb/mongo //数据存放位置// port: 27018 //端口号//mkdir -p /data/mongodb/ //创建日志存放目录//cd /data/mongodb/mkdir mongotouch mongod2.log //创建日志文件//chmod 777 mongod2.log //提升权限//mongod -f /etc/mongod2.conf //启动服务//[[email protected] mongo]# netstat -ntap | grep mongod //查看端口//tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 14604/mongod tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN
2) Enter the database with port number 27018
MongoDB Basic Operations
(1) MongoDB database to increase, delete, change, check operation.
> use school //不存在会创建,不建立集合又会删除//> db.createCollection(‘info‘) //创建集合//> show collections //查看集合(表),也可以使用show tables查看//info> db.info.insert({"id":1,"name":"jack"}) //插入数据记录//WriteResult({ "nInserted" : 1 })> db.info.find() //查看数据信息//{ "_id" : ObjectId("5b4d59fb97ae83a938d0e8b3"), "id" : 1, "name" : "jack" }> db.info.update({"id":1},{$set:{"name":"tom"}}) //更改//WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.info.find(){ "_id" : ObjectId("5b4d59fb97ae83a938d0e8b3"), "id" : 1, "name" : "tom" }> db.info.drop() //删除集合//true> db.dropDatabase() //删除数据库//{ "dropped" : "school", "ok" : 1 }> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GB
Daily maintenance of MongoDB
Mainly include: Database backup recovery, security management and database status monitoring.
1) Backup and recovery management
Backup management in MongoDB includes operations such as import and export, backup and restore, copy databases, and clone collections.
导出操作[[email protected] ~]# mongoexport -d kgc -c users -o /opt/users.json //适用于只有一个实例// mongoexport -h 127.0.0.1:27018 -d school -c test -o /opt/test.json //适用于有两个实列//导入操作[[email protected] opt]# mongoimport -d kgc -c user1 --file users.json //适用于只有一个实例// mongoimport -h=127.0.0.1:27018 -d school -c txt --file test.json //适用于有两个实列//条件操作[[email protected] opt]# mongoexport -d kgc -c user1 -q ‘{"id":{"$eq":10}}‘ -o /opt/top10.json
2) Backup and restore, copy database.
[[email protected] opt]# mkdir /backup //创建备份目录//[[email protected] opt]# mongodump -d kgc -o /backup/ #备份[[email protected] backup]# mongorestore -d kgc2 --dir=/backup/kgc #恢复>db.copyDatabase("kgc","kgc2") //复制数据库//
3) Clone Collection
mongo --port 27018 db.runCommand({"cloneCollection":"kgc.users","from":"192.168.235.190:27017"}
4) Create an administrative user
> use admin> db.createUser({"user":"root","pwd":"123","roles":["root"]})> db.auth("root","123")
Deploying the MongoDB database on CentOS