MongoDB installed base

MongoDB installed base

MongoDB Overview

(1) MongoDB is a distributed file storage based database. Written by C ++ language. Designed to provide scalable, high-performance data storage solution for WEB applications.

(2) MongoDB is a product with function between relational databases and non-relational databases, non-relational database functions among the richest and most like a relational database. It supports data structure is very loose, the json bson similar format, it is possible to store more complex data types. Mongo biggest feature is that it supports very powerful query language, its syntax is somewhat similar to the object-oriented query language, most of the functionality can be achieved almost single-table queries similar to a relational database, but also support for data indexing.

(3) Features:

Set is stored for easy storage of data object type;

Free mode, support queries, support for dynamic queries;

Supports fully indexed, contain internal objects;

Supports replication and failover;

Using an efficient binary data storage, including large objects (e.g., video, etc.);

Automatic processing chips to support cloud computing scalability levels;

Support RUBY, PYTHON, JAVA, C ++, PHP, C # and other languages;

File storage format of the BSON (JSON extension of one kind);

It can be accessed through the network.

MongoDB installation process

1, test preparation

name Roles address
Centos7-1 Service carrier machine 192.168.142.212

2, the specific process

(1) Configuration Local YUM source (path location: /etc/yum.repos.d/)

[root@promote yum.repos.d]# vim MongoDB.repo
[mongodb-org]
name=mongodb
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) Installation and configuration of the body

//安装MongoDB
[root@promote yum.repos.d]# yum install mongodb-org -y

//修改配置文件
[root@promote yum.repos.d]# vim /etc/mongod.conf
net:
  port: 27017
//使服务能够被任意网络使用
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all 

//开启服务
[root@promote yum.repos.d]# systemctl start mongod.service
[root@promote yum.repos.d]# netstat -atnp | grep 27017
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      39695/mongod

//进入MongoDB数据库
[root@promote mongodb]# mongo --port 27017

In this case, MongoDB service has been installed, but the experiment does not this end, the following is extended to begin the process of its installation

(3) establish a multi-instance MongoDB

//在安装完MongoDB的基础上
[root@promote etc]# cp -p mongod.conf mongod2.conf
//修改第二个实例的配置文件
[root@promote yum.repos.d]# vim /etc/mongod2.conf
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/mongod2.log            //日志文件路径(需要区别于第一个)
storage:
  dbPath: /data/mongodb/mongo                //数据文件路径(同样)
  journal:
    enabled: true
net:
  port: 27018                     //设定一个与之前不同端口号
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all int

//建立二号实例站点
[root@promote etc]# mkdir -p /data/mongodb            //创建二号实例数据存放位置
[root@promote etc]# cd /data/mongodb/
[root@promote mongodb]# mkdir mongo
[root@promote mongodb]# touch mongod2.log          //创建二号实例日志文件存放位置
[root@promote mongodb]# chmod 777 mongod2.log

//启动二号实例
[root@promote mongodb]# mongod -f /etc//mongod2.conf
[root@promote mongodb]# mongo --port 27018         //通过不同的端口号进行不同的实例中

MongoDB basic operations

Carried out in MongoDB

> show dbs/databases                     #查看所有数据库
> show collections/tables            #查看数据库中所有集合
> db.info.find(条件)                 #查看集合中内容
    例:> db.info.find({"id":20})         #查看id为20的信息
> use school;                    #不存在数据库会创建并进入,而又不建立集合则并不进行显示
> db.createCollection('info')                   #建立集合(数据表)info
> db.info.insert({"id":10,"name":zhangsan,"键名":值})            #在集合中插入值
> db.info.insert({"hobby":["game","read","值"]})                #在集合中添加字符串数组           
> a=db.info.find()               #将查找结果定义别名
> for (var i=11;i<=100;i++)db.info.insert({"id":i,"name":"liuliu"+i})                #循环插入键值对
> db.info.update({"id":20},{$set:{"name":"kaili"}})                    #更改数据
##将id=20中的name更改为kaili
> db.info.count()               #统计集合中有多少数据
> db.test.drop()                #删除某个集合
> db.test.remove({"id":8})        #删除某个数据
> db.dropDatabase()        #删除整个数据库(在进入该数据库的前提下)
> db.copyDatabase("school","shell")        #将school数据库复制为shell数据库(改名字拷贝)

MongoDB import, export

Export

[root@promote mongodb]# mongoexport -d school -c info -o /opt/school.json

"-D" specify the database
"-c" to specify a set of
"-o" Specify Export Path

Introduced (into the database may not be present)

[root@promote mongodb]# mongoimport -d school -c info --file=/opt/school.json

"--File" designated import json file

Export conditions

[root@promote mongodb]# mongoexport -d school -c info -q '{"id":{"$eq":20}}' -o /opt/ttt.json

"-Q" Analyzing conditions (greater than: gt; less than: lt; equals:. Eq no greater than equal to, or less)

MongoDB backup, recovery

Backup

[root@promote mongodb]# mongodump -d school -o /opt/

restore

[root@promote mongodb]# mongorestore -d school --dir=/opt/school

"--Dir" specify a backup directory path

Examples of a set of clones

Under the multi-instance MongoDB: The premise

[root@promote mongodb]# mongo --port 27018             #进入第二个实例

#从本机的27017端口的MongoDB数据库中克隆其中school.info集合到本实例中
> db.runCommand({"cloneCollection":"school.info","from":"192.168.142.212:27017"})

Create an administrative user

> use admin               #该数据库默认存在
> db.createUser({"user":"root","pwd":"123123","roles":["root"]})     #建立管理用户
"user"     #管理用户名
"pwd"     #密码
"roles":["root"]      #权限为管理员身份
> db.auth("root","123123")                    #验证

Guess you like

Origin blog.51cto.com/14449528/2462182