Installation and basic operation of MongoDB 3.6 under CentOS 7

Source: Internet
Author: User
Tags install mongodb yum install mongodb mongodump mongorestore

First, MongoDB Introduction
    1. MongoDB is a cross-platform, document-oriented database that enables high performance, high availability, and easy scaling. MongoDB is written by the C + + language and is an open source database system based on distributed file storage.
      In the case of high load, adding more nodes can guarantee the performance of the server. MongoDB can provide scalable, high-performance data storage solutions for Web applications.

    2. MongoDB is the most versatile non-relational database, most like relational database. Instead of using relational models primarily for better extensibility, MongoDB no longer has the concept of "line", which operates primarily on two concepts: Collections (collection) and documents.
    3. MongoDB is designed to provide scalable, high-performance data storage solutions for Web applications.

    4. MongoDB stores data as a document and data structures consist of key-value (key=>value) pairs. A MongoDB document is similar to a JSON object. Field values can contain other documents, arrays, and array of documents.

Main features of MongoDB

    1. MongoDB is simple to install and provides a document-oriented storage function, which is simple and easy to operate.
    2. MONGODB provides replication, high availability, and automatic sharding capabilities. If the load increases, it can be distributed across other nodes in the computer network, which is called sharding.
    3. The MONGO supports rich query expressions. Query directives use a JSON-style tag to easily query objects and arrays embedded in the document.
    4. MongoDB supports a variety of programming languages: Ruby, Python, Java, C + +, PHP, C #, and more. Second, the installation of MongoDB 3.6 experimental Steps (1) Deploy the Yum source repository
      vim /etc/yum.repos.d/mongod-org.repo

[Mongodb-org]
Name=mongodb Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
Gpgcheck=1
Enabled=1
Gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

### (2)Yum安装

Yum Install Mongodb-org-y

### (3).编辑MongoDB配置文件并启动其服务

Vim/etc/mongod.conf
bindip:0.0.0.0 #监听地址
port:27017 #监听端口

Systemctl Start Mongod.service #开启服务
NETSTAT-ANPT | grep 27017 #检查是否启动

### (4)连接并访问MongoDB

/usr/bin/mongo

Db.version () #查看版本
3.6.6
Show DBS; #查看数据库
Admin 0.000GB
Config 0.000GB
Local 0.000GB

----------## 三、MongoDB 3.6的基本操作
1. Turn on multi-instance (1) Copy a configuration file to a second instance
cp -p /etc/mongod.conf /etc/mongod2.conf## 复制一份配置文件给第二个实例
(2) Editing the second instance configuration file and startup parameters
vim /etc/mongod2.conf  ##配置实例   path: /data/mongodb/mongod2.log  ##日志文件位置   dbPath: /data/mongodb/mongo    ##数据位置   port: 27018      ##不同实例的端口不同
mkdir -p  /data/mongodb/  ##创建数据文件夹cd /data/mongodb/mkdir mongo   touch mongod2.log     ##创建日志文件chmod 777 mongod2.log    
(3) Start a second instance
mongod -f /etc/mongod2.conf   ##开启第二份实例mongo --port 27018     ##进入数据库
2. Basic operation
## 创建数据库 ,不存在会创建,不建立集合又会删除> use mydb;  switched to db mydb ##创建集合> db.createCollection(‘a‘) { "ok" : 1 } ## 在集合中插入数据> db.a.insert({"id":1,"name":"zhangsan"}) WriteResult({ "nInserted" : 1 })## 查看集合中的数据> db.a.find()  { "_id" : ObjectId("5b4c54bc8a4352592ecc288f"), "id" : 1, "name" : "zhangsan" }##查找指定记录并赋予别名a,查看属性类型> b=db.a.findOne({"id":1}){    "_id" : ObjectId("5b4c54bc8a4352592ecc288f"),    "id" : 1,    "name" : "zhangsan"}> typeof(b.id)number   ##更改数据> db.a.update({"id":1},{$set:{"name":"tom"}})  WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.a.find(){ "_id" : ObjectId("5b4c54bc8a4352592ecc288f"), "id" : 1, "name" : "tom" }##查看集合> show collections  a##删除集合> db.a.drop()   true##删除数据库> db.dropDatabase() { "dropped" : "mydb", "ok" : 1 }##复制数据库> db.copyDatabase("mydb","mydb1")  { "ok" : 1 }> show dbs;admin   0.000GBconfig  0.000GBlocal   0.000GBmydb    0.000GBmydb1   0.000GB
3. Import and Export data
    • The Mongoexport command can export a collection to a file in JSON or CSV format, which can be used to specify the exported data item, or to export the data according to the specified criteria.
    • The Mongoimport command can import the contents of a particular format file into the specified collection. The tool can import files in son or CSV format.
(1) Creation of 100 data
> use kgc;switched to db kgc> for(var i=1;i<=100;i++)db.users.insert({"id":i,"name":"jack"+i})WriteResult({ "nInserted" : 1 })> db.users.count()100
(2) Import and export operations
#导出操作[[email protected] ~]# mongoexport -d kgc -c users -o /opt/users.json2018-07-16T16:36:30.395+0800    connected to: localhost2018-07-16T16:36:30.407+0800    exported 100 records#查看导出的文件[[email protected] ~]# head -n 3 /opt/users.json {"_id":{"$oid":"5b4c589f43705395b9afe284"},"id":1.0,"name":"jack1"}{"_id":{"$oid":"5b4c589f43705395b9afe285"},"id":2.0,"name":"jack2"}{"_id":{"$oid":"5b4c589f43705395b9afe286"},"id":3.0,"name":"jack3"}```
#导入操作[email protected] ~]# mongoimport -d kgc -c user1 --file /opt/users.json2018-07-16T16:38:13.615+0800    connected to: localhost2018-07-16T16:38:13.635+0800    imported 100 documents#查看导入的数据集合> use kgc;switched to db kgc> show collectionsuser1users> exitbye
#条件导出操作[[email protected] ~]# mongoexport -d kgc -c user1 -q ‘{"id":{"$eq":10}}‘ -o /opt/top10.json2018-07-16T16:40:28.912+0800    connected to: localhost2018-07-16T16:40:28.915+0800    exported 1 record
  • -D: Indicates the name of the database
  • -C: Indicates the name of the collection
  • -F: Indicates which columns to export
  • -O: Indicates the file name to export
  • -Q: Indicates the filtering criteria for the exported data
4. Backup and restore (1) Backup

In MongoDB, you can use the Mongodump command to back up data, which exports all the data to the specified directory.

[[email protected] ~]# mkdir /opt/backup #创建备份文件夹[[email protected] ~]# mongodump -d kgc -o /opt/backup/2018-07-16T16:44:47.254+0800    writing kgc.user1 to 2018-07-16T16:44:47.254+0800    writing kgc.users to 2018-07-16T16:44:47.256+0800    done dumping kgc.user1 (100 documents)2018-07-16T16:44:47.256+0800    done dumping kgc.users (100 documents)
(2) Recovery

MongoDB uses the Mongorestore command to restore the backed up data

mongorestore -d kgc2 --dir=/backup/kgc  ##恢复
5. Cloning a Collection

In MongoDB, you can clone a collection in a database. This will clone the User1 collection from the KGC database to another instance.

(1) First look at MongoDB open instances
[[email protected] ~]# netstat -ntap | grep mongodtcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      1121/mongod         tcp        0      0 0.0.0.0:27018           0.0.0.0:*               LISTEN      10145/mongod  
(2) Enter the second instance of Port 27018
[[email protected] ~]# mongo --port 27018    ##进入另一个实例> db.runCommand({"cloneCollection":"kgc.user1","from":"192.168.113.175:27017"}){ "ok" : 1 }> show dbsadmin   0.000GBconfig  0.000GBkgc     0.000GBlocal   0.000GB> use kgcswitched to db kgc> show collections   #查看27018端口实例的集合user1## 完成克隆
7. Process Management

The MongoDB process can be managed and controlled by the administrator.
The command to view the currently running process is: Db.currentop ().
The process command that terminates a running high-resource process management is: Db.killop (opid).

Installation and basic operation of MongoDB 3.6 under CentOS 7

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.