Learn the industry-leading NoSQL database = "MongoDB" in one article per day

Source: Internet
Author: User
Tags install mongodb mongodb mongodb server mongodump

About MongoDB:

MongoDB is a cross-platform, document-oriented database that enables high-performance, high-availability, and can be easily scaled.

MongoDB is also a product between a non-relational database and a relational database, and is the most versatile and most like relational database in an untyped database.

The operation is primarily based on two concepts: collections and documents

Features of MongoDB:

1, easy to install, provide the document-oriented storage function

2. Provides replication, high availability, and automatic sharding capabilities

3, support rich query expressions, query instructions using JSON-style markup

4. Support various programming languages: Ruby, Pathon, Java, C + +, PHP, C #, etc.

Below I install MongoDB in CENTOS7 and demonstrate a variety of basic operations related

First, install MongoDB (1) Configure the Yum source repository
[[email protected] ~]#cd /etc/yum.repos.d/[[email protected] yum.repos.d]#vim 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
(2) Installing MongoDB
[[email protected] yum.repos.d]#yum list   //测试[[email protected] ~]#yum  install -y mongodb-org[[email protected] ~]#vi /etc/mongod.confbindIp: 0.0.0.0             #监听地址port: 27017                 #监听端口[[email protected] ~]#systemctl start mongod.service[[email protected] ~]#netstat -anpt | grep 27017tcp          0          0 0.0.0.0:27017           0.0.0.0:*       LISTEN          44010/mongod[[email protected] ~]#/usr/bin/mongo>db.version()                //查看版本信息>show dbsadmin 0.0000GBlocal 0.0000GB> DB.getMongo()2018-07-16T16:10:27.899+0800 E QUERY    [thread1] TypeError: DB.getMongo is not a function :@(shell):1:1
Second, start MongoDB multi-instance
[[email protected] ~]# cp-p/etc/mongod.conf/etc/mongod2.conf[[email protected] ~]# vim/etc/                               Mongod2.confpath:/data/mongodb/mongod2.log//Log location DBPath:/data/mongodb/mongo//storage path port:27018 Port number [[email protected] ~]# mkdir-p/data/mongodb/[[email protected] ~]# cd/dat A/mongodb/[[email protected] mongodb]# mkdir mongo[[email protected] mongodb]# touch mongod2.log[[email  protected] mongodb]# chmod 777 mongod2.log[[email protected] mongodb]# mongod-f/etc/mongod2.confabout to  Fork child process, waiting until server was ready for connections.forked Process:51823child process started successfully, Parent exiting[[email protected] mongodb]# MONGO--port 27018MongoDB Shell version v3.6.6connecting TO:MONGODB://1 27.0.0.1:27018/mongodb Server Version:3.6.6server has startup warnings:2018-07-16t16:21:43.218+0800 I CONTROL [Initand Listen] > exitbye[[email protected] mongodb]# NETstat-antpactive Internet connections (servers and established) Proto recv-q send-q Local Address Foreign addres      S state pid/program name TCP 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 44010/mongod TCP 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 51823/mongod TC P 0 0 127.0.0.1:49076 127.0.0.1:27018 time_wait-tcp 0 0 192. 168.200.184:22 192.168.200.1:60220
Third, MongoDB basic operation
> Use MyDB//Create and open MyDB database switched to DB mydb> A=db.info.find ()/ /aliases > Db.createcollection (' a ')//Add a collection to the MyDB database a{"OK": 1}> Show Collections//Display number According to all collections in the library a> typeof (a.ID)//View property type 2018-07-16t17:51:21.735+0800 E QUERY [Thread1] Referenceerr OR:A isn't defined: @ (shell):1:1> Db.a.insert ({"id": 1, "name": "Zhangsan", "hobby": ["Game", "Talk", "read"]})                           Writeresult ({"ninserted": 1}) ["Game", "Talk", "read"]//String array object (object) > Db.a.find () Displays all the documents in the A collection {"_id": ObjectId ("5b4c58ddf7c9ed9a709175e9"), "id": 1, "name": "Zhangsan", "hobby": ["Game "," Talk "," read "]}> Db.a.insert ({" id ": 2," name ":" Lisi "," hobby ": [" Game "," Talk "," read "]}) Writeresult ({" ninserted ": 1}" > Db.a.insert ({"id": 3, "name": "Wangwu", "hobby": ["Game", "Talk", "read"]}) Writeresult ({"ninserted": 1}) > Db.a.find () {"_id": ObjectId ("5b4c58ddf7c9ed9a709175e9 ")," id ": 1," name ":" Zhangsan "," hobby ": [" Game "," Talk "," read "]} {" _id ": ObjectId (" 5b4c67c5bacc9efbe3a0d 477 ")," id ": 2," name ":" Lisi "," hobby ": [" Game "," Talk "," read "]} {" _id ": ObjectId (" 5b4c67ccbacc9efbe3a0d478 ")," I D ": 3," name ":" Wangwu "," hobby ": [" Game "," Talk "," read "]}> Db.a.findone ({" id ": 1})//find specified record {" _id ": ObjectId (" 5b4c58ddf7c9ed9a709175e9 ")," id ": 1," name ":" Zhangsan "," hobby ": [" Game "," ta LK "," read "]}> db.a.update ({" id ": 2},{$set: {" name ":" Xiaoqi "}})//Change Property Writeresult ({" nmatched ": 1," Nupser  Ted ": 0," nmodified ": 1}) > Db.a.find () {" _id ": ObjectId (" 5b4c58ddf7c9ed9a709175e9 ")," id ": 1," name ":" Zhangsan ", "Hobby": ["Game", "Talk", "read"]} {"_id": ObjectId ("5b4c67c5bacc9efbe3a0d477"), "id": 2, "name": "Xiaoqi", "hobby ": [" Game "," Talk "," read "]} {" _id ": ObjectId (" 5b4c67ccbacc9efbe3a0d478 ")," id ": 3," name ":" Wangwu "," hobby ": [" Game "," Talk "," read "]}> show Dbsadmin 0.000GBconfig 0.000GBlocal 0.000GBmydb 0.000gb> use mydbswitched to DB mydb> Db.a.drop () Delete Collection true> show collections> db.dropdatabase ()//delete database {"Dropped" : "MyDB", "OK": 1}> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GB
Iv. Daily maintenance of MongoDB

Parameter description:

-d:指明数据库的名字-c:指明集合的名字-f:指明要导出哪些列-o:指明要导出的文件名-q:指明导出数据的过滤条件
(1) Import and export

Import and export MongoDB data using the Mongoexport and Mongoimport commands

> Use schoolswitched to DB school> db.createcollection (' info ') {"OK": 1}> for (var i=1;i<=100;i++) db.info.ins ERT ({"id": I, "name": "Jack" +i}) Writeresult ({"ninserted": 1}) > Db.info.find () {"_id": ObjectId ("  5b4ca7864f78ab07f2b572da ")," id ": 1," name ":" Jack1 "} {" _id ": ObjectId (" 5b4ca7864f78ab07f2b572db ")," id ": 2," name ": "Jack2"} {"_id": ObjectId ("5B4CA7864F78AB07F2B572DC"), "id": 3, "name": "Jack3"} {"_id": ObjectId ("5b4ca7864f78ab07 F2B572DD ")," id ": 4," name ":" Jack4 "} {" _id ": ObjectId (" 5b4ca7864f78ab07f2b572de ")," id ": 5," name ":" Jack5 "} {" _id ": ObjectId (" 5b4ca7864f78ab07f2b572df ")," id ": 6," name ":" Jack6 "> Db.info.count ()//display number of rows 100[[ Email protected] ~]# mongoexport-d school-c info-o/opt/school.json//Note Here the exported file format is *.jsonc2018-07-16t22 : 13:22.668+0800 connected to:localhost2018-07-16t22:13:22.675+0800 exported records[[email protected] ~]# C D/opt[[email protected] opt]# LSRH school.json[[emAil protected] opt]# vim School.json [[email protected] opt]# mongoimport-d school-c test--file School.json20 18-07-16t22:15:22.523+0800 Connected to:localhost2018-07-16t22:15:22.700+0800 Imported DOCUMENTS[[EMAIL&NBSP;PR Otected] opt]# mongo> use schoolswitched to DB school> show Tablesinfotest//import file test, which was just exported Content > Db.test.find () {"_id": ObjectId ("5b4ca7864f78ab07f2b572da"), "id": 1, "name": "Jack1"} {"_id": ObjectId ("5b4c A7864f78ab07f2b572db ")," id ": 2," name ":" Jack2 "} {" _id ": ObjectId (" 5B4CA7864F78AB07F2B572DC ")," id ": 3," name ":" Ja Ck3 "} {" _id ": ObjectId (" 5B4CA7864F78AB07F2B572DD ")," id ": 4," name ":" Jack4 "} {" _id ": ObjectId (" 5b4ca7864f78ab07f2b5 72de ")," id ": 5," name ":" Jack5 "} {" _id ": ObjectId (" 5b4ca7864f78ab07f2b572df ")," id ": 6," name ":" Jack6 "[[email&nbsp ;p rotected] opt]# mongoexport-d school-c info-q ' {"id": {"$eq": Ten}} '-o/opt/top10.json//filter the number of rows to be exported,-Q There is a space behind 2018-07-16t22:20:20.610+0800 connected to:localhost2018-07-16t22:20:20.636+0800 exported 1 record[[email protected] opt]# cd/opt /[[email protected] opt]# lsrh school.json top10.json[[email protected] opt]# vim Top10.json {"_id": {"$oid": "5b4ca7864f78ab07f2b572e3"}, "id": 10.0, "name": "Jack10"}
(2) Backup and recovery

Use the Mongodump command to back up MongoDB data

-h:MongoDB所在服务器地址-d:需要备份的数据库实例-o:备份数据存放的位置,该目录需提前创建
[[email protected] opt]# mkdir /backup[[email protected] opt]# mongodump -d school -o /backup/       //备份2018-07-16T22:43:29.828+0800    writing school.info to 2018-07-16T22:43:29.830+0800    writing school.test to 2018-07-16T22:43:29.836+0800    done dumping school.info (100 documents)2018-07-16T22:43:29.838+0800    done dumping school.test (100 documents)[[email protected] opt]# cd /backup/[[email protected] backup]# lsschool[[email protected] backup]# cd school/[[email protected] school]# lsinfo.bson  info.metadata.json  ==test.bson==  test.metadata.json[[email protected] school]# mongorestore -d abc --dir=/backup/school/  //恢复  备注:这里的数据库abc会自动创建,所以可以不存在2018-07-16T22:46:02.801+0800    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
(3) Copy Database
> show dbsabc     0.000GBadmin   0.000GBconfig  0.000GBlocal   0.000GBschool  0.000GB> show dbsabc     0.000GBadmin   0.000GBconfig  0.000GBlocal   0.000GBschool  0.000GB> db.copyDatabase("abc","abc1")               //复制数据库        { "ok" : 1 }> show dbsabc     0.000GBabc1    0.000GBadmin   0.000GBconfig  0.000GBlocal   0.000GBschool  0.000GB
(4) Create an administrative user
> use admin> db.createUser({"user":"root","pwd":"123","roles":["root"]})> db.auth("root","123")
(5) Process Management
> db.currentOp()          //查看进程    "active" : true,            "currentOpTime" : "2018-07-16T23:28:31.188+0800",            "opid" : 19385,            "secs_running" : NumberLong(0),    > db.killOp(19385){ "info" : "attempting to kill op", "ok" : 1 }

Learn the industry-leading NoSQL database = "MongoDB" in one article per day

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.