MongoDB Installation and common operation

Source: Internet
Author: User
Tags findone mongodb

Mongodbmongodb Introduction
    • 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 data system, in the case of high load, add more nodes, can guarantee the performance of the server.
    • MongoDB is also a product between relational database and non-relational database, which is the richest and most like relational database in non-relational database. MongoDB does not have a "line" concept, which operates in two main concepts: Collections (collection) and documents.
Features of MongoDB
    • MongoDB features include collection-oriented storage, schema freedom, rich query statements, multi-level indexing, replication 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 require more storage space and more processing power, it can be distributed across other nodes in the computer network, which is called sharding.
    • MongoDB supports rich query expressions, and Query Directives use table-level JSON as a way to easily query objects and arrays embedded in a document.
MongoDB areas of Use
    • MongoDB provides scalable, high-performance data storage solutions for Web applications, where MongoDB is primarily used for Web site data, distributed scenarios, data caching, and JSON document format storage. It is suitable for Internet applications with large amount of data, high concurrency and weak transaction, and its built-in horizontal scaling mechanism provides data processing capability from millions of to a billion of levels, which can meet the requirements of we2.0 and mobile Internet application.
The installation of MongoDB
    • MongoDB provides the installation package on the Linux platform, which can be downloaded from the official website Http://www.mongodb.org/downloads.
    • MongoDB Another simple installation option is to select the Yum installation form
Configuring the Yum Source
    • New Yum source file

Vim/etc/yum.repos.d/mongodb-org.repo

[mongodb-org]name=MongoDB Repositorybaseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/     //选择3.6安装版本         gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc    //官方验证密匙网址

Yum List
Yum Install mongodn-org

Modifying a configuration file

Vim/etc/mongodb.conf

# network interfacesnet:  port: 27017            //服务监听端口  bindIp: 0.0.0.0        //修改服务监听地址,0.0.0.0表示任意网段
Start the service

Systemctl Stop Firewalld.service
Setenforce 0
Mongod-f/etc/mongod.conf//start-up service
Mongod-f/etc/mongod.conf--shutdown//Stop service
MONGO--port27017//or MONGO directly into the database

MongoDB basic operations and additions and deletions to check
> show dbs     //查看数据库> use school        //不存在会创建数据库> db.dropDatabase() //删除数据库,在哪一个数据库下就删除哪一个数据库> use info; //不存在会创建,不建立集合又会删除> db.createCollection(‘a‘)          //创建集合a> db.a.insert({"id":1,"name":"zhangsan"})   //插入数据,json键值对格式,如果一个键对应多个值,那么{"hobby":["sport","run"]}> db.a.find()    //查看集合a中的是所有数据> for(var i=2;i<=100;i++)db.info.insert({"id":i,"name":"jack"+i})   //循环插入数据> db.info.findOne({"id":10})      //查看指定行> a=db.users.findOne({"id":2})    //查找指定记录并赋予别名a> typeof(a.id)   //查看属性类型> db.users.update({"id":10},{$set:{"name":"tom"}})  //  更改user集合中id=10中name> show collections //查看当前数据库下所有集合,或者使用show tables> db.a.drop()    //删除集合a
MongoDB Multi-Instance Open
    • Multi-instance opening requires creating a separate profile for another instance, as well as separate data file storage directories and log independent log files and different ports. The only thing to note in the configuration file is that the PID process file location cannot be changed.

    • To copy a standalone configuration file

Cp-p/etc/mongod.conf/etc/mongod2.conf

    • To modify a standalone configuration file

Vim/etc/mongod2.conf

# where to write logging data.systemLog:  destination: file  logAppend: true  path: /data/mongodb/logs/mongodb2.log    //日志文件存放位置,该位置目录需要手动创建,便于管理# Where and how to store data.storage:  dbPath: /data/mongodb/mongodb2    //数据文件存放位置,该位置目录需要手动创建,便于管理  journal:    enabled: true#  engine:#  mmapv1:#  wiredTiger:# how the process runsprocessManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile   //pid进程文件位置,在多实例配置文件中,此位置不可更改,需要跟主配置文件一致  timeZoneInfo: /usr/share/zoneinfo# network interfacesnet:  port: 27018       //修改端口号,每一个不同实例独立一个端口号  bindIp: 0.0.0.0   //监听服务IP地址
    • Creating data File hosting directories and stand-alone log files

Mkdir-p/DATA/MONGODB/MONGODB2//Create MONGODB2 data file to store directory
Cd/data/mongodb
mkdir logs//create log file directory
CD logs
Touch Mongodb2.logs//Create multi-instance standalone log file
chmod 777 Mongodb2.logs//privilege amplification allows MongoDB to write to the log

    • Start Multi-instance

Mongod-f/etc/mongod2.conf//Specify Configuration file Startup
Mongod--port 27018//Enter multi-instance standalone database

Import Export
[[email protected]]# mongoexport -d school -c info -o /opt/school.json//-d 指定数据库database名称;-c指定集合collection名称;-o指定导出output位置,并命名以json为结尾的文件格式[[email protected]]# mongoimport -d school -c info1 --file /opt/school.jso //-d 指定要还原的数据库;-c指定要还原出的表名,不能跟已有的同名;--file指定还原的文件位置条件操作[[email protected]]# mongoexport -d school -c info1 -q ‘{"id":{"$eq":10}}‘ -o /opt/school1.json//条件导出,将school数据库中info1集合中id等于10的行导出
Backup and Recovery
[[email protected]]# mkdir /backup    //创建备份文件存放目录[[email protected]]# mongodump -d school -o /backup/  //备份数据库[[email protected] backup]# mongorestore -d school2 --dir=/backup/school  //恢复数据库,注意不可与现有数据库同名
Copy Database
[[email protected]]# db.copyDatabase("school","school2")
Clone collection
[[email protected]]# mongo --port 27018     //进入实例2数据库  > db.runCommand({"cloneCollection":"school.info","from":"192.168.144.114:27017"})   //将27017实例数据库school中的info集合克隆到本实例中
Create an administrative user
[[email protected]]# mongo    //进入数据库  > db.createUser({"user":"root","pwd":"123","roles":["root"]})  > db.auth("root","123")
Process Management
> db.currentOp()     显示     "opid" : 337,> db.killOp(337)

MongoDB Installation and common operation

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.