MongoDB是个非关系型数据库 但操作跟关系型数据库很类似,我来为大家科普一下关于mongodb原理和架构?以下内容希望对你有帮助!

mongodb原理和架构(Mongodb从入门到精通)

mongodb原理和架构

MongoDB介绍

MongoDB是个非关系型数据库 但操作跟关系型数据库很类似

MongoDB是面向文档存储的非关系型数据库 数据以json的格式进行存储

mongodb可用来永久存储 也可以用来缓存数据

MongoDB提供副本集合集群功能 操作简单

MongoDB的下载链接 直接使用MongoDB的二进制文件 面编译安装

https://www/mongodb.com/download-center?jmp=nav#community

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.2.tgz

mongodb的二进制解压安装

cd /usr/local/src

wget https://fastdl.MongoDB.org/linux/mongodb-linux-x86_64-4.0.2.tgz

tar -zxvf mongodb-linux-x86_64-4.0.2.tgz

mv mongodb-linux-x86_64-4.0.2 /usr/local/mongodb

[root@c7-node01 mongodb]# ll -h /usr/local/mongodb/

total 120K

drwxr-xr-x 2 root root 231 Mar 9 14:37 bin

-rw-r--r-- 1 root root 34K Aug 28 2018 GNU-AGPL-3.0

-rw-r--r-- 1 root root 2.1K Aug 28 2018 LICENSE-Community.txt

-rw-r--r-- 1 root root 17K Aug 28 2018 MPL-2

-rw-r--r-- 1 root root 2.2K Aug 28 2018 README

-rw-r--r-- 1 root root 56K Aug 28 2018 THIRD-PARTY-NOTICES

[root@c7-node01 mongodb]# ./bin/mongod --help

Options:

General options:

-v [ --verbose ] [=arg(=v)] be more verbose (include multiple times

for more verbosity e.g. -vvvvv)

--quiet quieter output

--port arg specify port number - 27017 by default

--logpath arg log File to send write to instead of

stdout - has to be a file, not

directory

--syslog log to system's syslog facility instead

of file or stdout

--syslogFacility arg syslog facility used for mongodb syslog

message

--logappend append to logpath instead of

over-writing

--logRotate arg set the log rotation behavior

(rename|reopen)

--timeStampFormat arg Desired format for timestamps in log

messages. One of ctime, iso8601-utc or

iso8601-local

--setParameter arg Set a configurable parameter

-h [ --help ] show this usage information

--version show version information

-f [ --config ] arg configuration file specifying

additional options

--bind_ip arg comma separated list of ip addresses to

listen on - localhost by default

....

查看版本号

[root@c7-node01 mongodb]# ./bin/mongod --version

db version v4.0.2

git version: fc1573ba18aee42f97a3bb13b67af7d837826b47

allocator: tcmalloc

modules: none

#########MongoDB单实例启动

mkdir -pv /data/mongodb/27017/

[root@c7-node01 logs]# cat /data/mongodb/27017/conf/mongodb.conf

systemLog:

destination: file

logAppend: true

path: /data/mongodb/27017/logs/mongodb.log

storage:

dbPath: /data/mongodb/27017/data/

journal:

enabled: true

processManagement:

fork: true

pidFilePath: /data/mongodb/27017/conf/mongodb.pid

net:

port: 27017

bindIp: 127.0.0.1

启动MongoDB

./bin/mongod -f /data/mongodb/27017/conf/mongodb.conf

[root@c7-node01 27017]# ps -ef|grep -v grep|grep mongo

root 9592 1 6 15:25 ? 00:00:30 ./bin/mongod -f /data/mongodb/27017/conf/mongodb.conf

[root@c7-node01 27017]#

[root@c7-node01 27017]# tree

├── conf

│ ├── mongodb.conf

│ └── mongodb.pid

├── data

│ ├── collection-0--6966875933417581368.wt

│ ├── collection-2--6966875933417581368.wt

│ ├── collection-4--6966875933417581368.wt

│ ├── diagnostic.data

│ │ ├── metrics.2019-03-09T07-15-22Z-00000

│ │ ├── metrics.2019-03-09T07-25-40Z-00000

│ │ └── metrics.interim

│ ├── index-1--6966875933417581368.wt

│ ├── index-3--6966875933417581368.wt

│ ├── index-5--6966875933417581368.wt

│ ├── index-6--6966875933417581368.wt

│ ├── journal

│ │ ├── WiredTigerLog.0000000002

│ │ ├── WiredTigerPreplog.0000000001

│ │ └── WiredTigerPreplog.0000000002

│ ├── _mdb_catalog.wt

│ ├── mongod.lock

│ ├── sizeStorer.wt

│ ├── storage.bson

│ ├── WiredTiger

│ ├── WiredTigerLAS.wt

│ ├── WiredTiger.lock

│ ├── WiredTiger.turtle

│ └── WiredTiger.wt

└── logs

└── mongodb.log

5 directories, 25 files

关闭

不建议使用kill,kill -9 和突然断电会导致MongoDB数据丢失

MongoDB 客户端 mongo 类似于mysql 的 mysql 连接客户端

[root@c7-node01 27017]# /usr/local/mongodb/bin/mongo 127.0.0.1:27017

MongoDB shell version v4.0.2

connecting to: mongodb://127.0.0.1:27017/test

MongoDB server version: 4.0.2

Server has startup warnings:

2019-03-09T15:25:39.221 0800 I CONTROL [initandlisten]

2019-03-09T15:25:39.222 0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.

2019-03-09T15:25:39.225 0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.

2019-03-09T15:25:39.225 0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

2019-03-09T15:25:39.225 0800 I CONTROL [initandlisten]

2019-03-09T15:25:39.235 0800 I CONTROL [initandlisten]

2019-03-09T15:25:39.235 0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

2019-03-09T15:25:39.235 0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'

2019-03-09T15:25:39.235 0800 I CONTROL [initandlisten]

2019-03-09T15:25:39.235 0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

2019-03-09T15:25:39.235 0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'

2019-03-09T15:25:39.235 0800 I CONTROL [initandlisten]

>

优化启动说明

1.调整用户进程数

[root@c7-node01 ~]# tail /etc/security/limits.conf

#* hard rss 10000

#@student hard nproc 20

#@faculty soft nproc 20

#@faculty hard nproc 50

#ftp hard nproc 0

#@student - maxlogins 4

# End of file

#

* - nofile 65536

* - nproc 65536

2.禁止内存巨大页

[root@c7-node01 ~]# echo 'never' >/sys/kernel/mm/transparent_hugepage/enabled

[root@c7-node01 ~]# echo 'never' >/sys/kernel/mm/transparent_hugepage/defrag

[root@c7-node01 ~]# tail -3 /etc/rc.local

#

echo 'never' >/sys/kernel/mm/transparent_hugepage/enabled

echo 'never' >/sys/kernel/mm/transparent_hugepage/defrag

3.使用普通用户登录

[root@c7-node01 ~]# useradd mongodb -s /sbin/nologin

[root@c7-node01 ~]# chown -R mongodb. /data/mongodb #要先停止MongoDB再授权 否则普通用户启动会提示没有权限错误

[root@c7-node01 ~]# chown -R mongodb. /usr/local/mongodb

[mongodb@c7-node01 ~]$ /usr/local/mongodb/bin/mongod -f /data/mongodb/27017/conf/mongodb.conf

about to fork child process, waiting until server is ready for connections.

forked process: 13008

child process started successfully, parent exiting

[mongodb@c7-node01 ~]$

[root@c7-node01 logs]# /usr/local/mongodb/bin/mongo 127.0.0.1:27017

MongoDB shell version v4.0.2

connecting to: mongodb://127.0.0.1:27017/test

MongoDB server version: 4.0.2

Server has startup warnings:

2019-03-09T16:22:35.916 0800 I CONTROL [initandlisten]

2019-03-09T16:22:35.916 0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.

2019-03-09T16:22:35.916 0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.

2019-03-09T16:22:35.916 0800 I CONTROL [initandlisten]

2019-03-09T16:22:35.917 0800 I CONTROL [initandlisten]

2019-03-09T16:22:35.918 0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 65536 files. Number of processes should be at least 32768 : 0.5 times number of files.

>

3.调整rlimits参数

[root@c7-node01 logs]# cat /etc/security/limits.d/20-nproc.conf

# Default limit for number of user's processes to prevent

# accidental fork bombs.

# See rhbz #432903 for reasoning.

* soft nproc 65536

root soft nproc unlimited

重启MongoDB,在从新登录

[root@c7-node01 ~]# /usr/local/mongodb/bin/mongo 127.0.0.1:27017

MongoDB shell version v4.0.2

connecting to: mongodb://127.0.0.1:27017/test

MongoDB server version: 4.0.2

Server has startup warnings:

2019-03-09T16:33:48.768 0800 I CONTROL [initandlisten]

2019-03-09T16:33:48.768 0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.

2019-03-09T16:33:48.768 0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.

2019-03-09T16:33:48.768 0800 I CONTROL [initandlisten]

>

MongoDB的基础概念介绍

database 数据库

collection 集合 类似于sql中的表

filed 类似于mysql中的字段

document 每行的记录

mongo客户端的自动提示功能

使用tab键 提示补全

查询所有的库 默认自带三个数据库

show dbs;

show databases;

> show dbs;

admin 0.000GB

config 0.000GB

local 0.000GB

> show databases;

admin 0.000GB

config 0.000GB

local 0.000GB

mongo客户端提供一个正确的关闭MongoDB服务器的方法

use admin

db.shutdownServer()

MongoDB创建库 创建集合 插入数据(key value的字典方式插入)

use mgtest #use 即可 不用显示创建

db.myuser.insert({'name':'mgtest1',age:22}) #无需显示创建collection。 name age为filed, mgtest1 22为值

show dbs;

show collections;

show tables;

db.myuser.insert({'name':'mgtest2',age:23})

db.myuser.insert({'name':'mgtest3',age:33})

> use mgtest

switched to db mgtest

> db.myuser.insert({'name':'mgtest1','age':23}) #往mgtest的集合myuser里插入数据

WriteResult({ "nInserted" : 1 })

> db.myuser.insert({'name':'mgtest2','age':24})

WriteResult({ "nInserted" : 1 })

> db.myuser.insert({'name':'mgtest3','age':25})

WriteResult({ "nInserted" : 1 })

> show dbs;

admin 0.000GB

config 0.000GB

local 0.000GB

mgtest 0.000GB

> show collections;

myuser

> show tables;

myuser

查询集合数据 默认有个_id

use mgtest

db.myuser.find() #查询所有数据

db.myuser.find({name:'mgtest1'})

db.myuser.find({age:23})

> db.myuser.find({age: 27})

{ "_id" : ObjectId("5c83a90c8c5abee480a1bd78"), "name" : "mgtest6", "age" : 27 }

> db.myuser.find({age: 26})

{ "_id" : ObjectId("5c83a8cd8c5abee480a1bd77"), "name" : "mgtest5", "age" : 26 }

>

#查询有多少条记录

> db.myuser.count();

5217

删除集合中的数据

use mgtest;

db.myuser.remove({name: 'mgtest1'}) #有条件的删除

db.myuser.find()

db.myuser.remove({}) #删除所有数据

db.myuser.drop() #删除集合

删除数据库

use mgtest

db.dropDatabase()

MongoDB默认的三个数据库无需动它,MongoDB系统管理库

> show dbs;

admin 0.000GB

config 0.000GB

local 0.000GB

集合的field不用固定 但一般来说是要一样的

db.myuser.insert({age: 28})

db.myuser.insert({'location':'hangzhou'})

db.myuser.find()

更新集合数据

use.mgtest;

db.myuser.update({location: 'hangzhou'},{$set:{'location':'shanghai'}})

db.myuser.update({age: 28},{$set: {age: 35}})

##MongoDB集合的多种查询方式

1.易读方式查询

db.myuser.find().pretty()

> db.myuser.find().pretty()

{

"_id" : ObjectId("5c83a5b08c5abee480a1bd74"),

"name" : "mgtest1",

"age" : 23

}

{

"_id" : ObjectId("5c83a5ba8c5abee480a1bd75"),

"name" : "mgtest2",

"age" : 24

}

{

"_id" : ObjectId("5c83a5bf8c5abee480a1bd76"),

"name" : "mgtest3",

"age" : 25

}

{

"_id" : ObjectId("5c83a8cd8c5abee480a1bd77"),

"name" : "mgtest5",

"age" : 26

}

2.limit限制条数查询

db.myuser.find().limit(2)

> db.myuser.find().limit(2);

{ "_id" : ObjectId("5c83a5b08c5abee480a1bd74"), "name" : "mgtest1", "age" : 23 }

{ "_id" : ObjectId("5c83a5ba8c5abee480a1bd75"), "name" : "mgtest2", "age" : 24 }

> db.myuser.find().skip(2).limit(5)

3.使用skip跳过记录查询

db.myuser.find().skip(2).limit(2)

> db.myuser.find().skip(2).limit(5)

{ "_id" : ObjectId("5c83a5bf8c5abee480a1bd76"), "name" : "mgtest3", "age" : 25 }

{ "_id" : ObjectId("5c83a8cd8c5abee480a1bd77"), "name" : "mgtest5", "age" : 26 }

{ "_id" : ObjectId("5c83a90c8c5abee480a1bd78"), "name" : "mgtest6", "age" : 27 }

{ "_id" : ObjectId("5c83aea38c5abee480a1bd79"), "name" : "shanshan", "age" : 23 }

{ "_id" : ObjectId("5c83aeac8c5abee480a1bd7a"), "name" : "nana", "age" : 22 }

>

4.MongoDB分页查询

db.myuser.find().skip(0).limit(2)

db.myuser.find().skip(2).limit(2)

db.myuser.find().skip(4).limit(2)

5.使用sort进行排序

db.myuser.find().sort({"age": 1}) #按age升序

db.myuser.find().sort({"age": -1}) #按age降序

6.根据字段进行数字比较查询

db.myuser.find({"age": {$lt: 30}})

$gt 大于

$lt 小于

$gte 大于等于

$lte 小于等于

7.与 或 查询

db.myuser.find({name: 'mgtest1'})

db.myuser.find({name: 'mgtest2'})

db.myuser.find({$or: [{name: 'mgtest1'},{name: 'mgtest2'}]})

db.myuser.find({$and: [{name: 'mgtest2'},{age: 20}]})

8.mongodb正则查询,支持普通正则和扩展正则

db.myuser.find({"name":{$regex:"mgtest[0-9]"}}) #普通正则

db.myuser.find({name: {$regex:"(mgtest)"}}) #分组正则

db.myuser.find({name: {$regex: "(mgtest )"}})

db.myuser.find({name: {$regex: "(zhangsan)"}}) #支持分组正则

MongoDB索引查询与建立

生成点数据测试

use mgtest

for(i=1;i<=500000;i ){

db.myuser.insert({name: 'test' i,age:i})

}

MongoDB的慢查询概念 默认是超过100ms会记录慢日志mongo

db.getProfilingStatus()

查询age为9999的 查看扫描的行数

db.myuser.find({age: 9999})

db.myuser.find({age: 9999}).explain(true) #使用explain可以查看是否全表扫描

添加索引,加快查询

db.myuser.getIndexes() #获得当前索引 默认有_id的索引 索引用_id查是比较快的

db.myuser.ensureIndex({age: 1}) #增加age的升序索引

db.myuser.getIndexes()

db.myuser.find({age: 9999})

db.myuser.find(age: 9999).explain(true)

不加索引查询 耗时2182ms

> db.myuser.find({age: 9999})

{ "_id" : ObjectId("5c8f4c009fc9074914ce79f9"), "name" : "mytest9999", "age" : 9999 }

>

慢日志

[root@c7-node01 ~]# tailf /data/mongodb/27017/logs/mongodb.log 2019-03-18T16:22:50.611 0800 I COMMAND [conn9] command test.myuser appName: "MongoDB Shell" command: find { find: "myuser", filter: { age: 9999.0 }, lsid: { id: UUID("3c43c514-01e8-4313-8cc6-f482dde57d42") }, $db: "test" } planSummary: COLLSCAN keysExamined:0 docsExamined:500000 #COLLSCAN 全表扫描 cursorExhausted:1 numYields:3980 nreturned:1 reslen:159 locks:{ Global: { acquireCount: { r: 3981 } }, Database: { acquireCount: { r: 3981 } }, Collection: { acquireCount: { r: 3981 } } } protocol:op_msg 2182ms #加explain(true) > db.myuser.find({age: 9999}).explain(true) { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.myuser", "indexFilterSet" : false, "parsedQuery" : { "age" : { "$eq" : 9999 } }, "winningPlan" : { "stage" : "COLLSCAN", #全表扫描 "filter" : { "age" : { "$eq" : 9999 } }, "direction" : "forward" }, "rejectedPlans" : [ ] }, "executionStats" : { "executionSuccess" : true, "nReturned" : 1, "executionTimeMillis" : 1151, #耗时1151毫秒 "totalKeysExamined" : 0, "totalDocsExamined" : 500000, "executionStages" : { "stage" : "COLLSCAN", "filter" : { "age" : { "$eq" : 9999 } }, "nReturned" : 1, "executionTimeMillisEstimate" : 944, "works" : 500002, "advanced" : 1, "needTime" : 500000, "needYield" : 0, "saveState" : 3946, "restoreState" : 3946, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 500000 }, "allPlansExecution" : [ ] }, "serverInfo" : { "host" : "c7-node01", "port" : 27017, "version" : "4.0.2", "gitVersion" : "fc1573ba18aee42f97a3bb13b67af7d837826b47" }, "ok" : 1 } > #查看默认索引 > db.myuser.getIndexes(); [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.myuser" } ] >

根据索引查询 速度很快

> db.myuser.find().limit(5) { "_id" : ObjectId("5c8f4bcb9fc9074914ce52eb"), "name" : "mytest1", "age" : 1 } { "_id" : ObjectId("5c8f4bcb9fc9074914ce52ec"), "name" : "mytest2", "age" : 2 } { "_id" : ObjectId("5c8f4bcb9fc9074914ce52ed"), "name" : "mytest3", "age" : 3 } { "_id" : ObjectId("5c8f4bcb9fc9074914ce52ee"), "name" : "mytest4", "age" : 4 } { "_id" : ObjectId("5c8f4bcb9fc9074914ce52ef"), "name" : "mytest5", "age" : 5 } > db.myuser.find({"_id" : ObjectId("5c8f4bcb9fc9074914ce52eb")}) { "_id" : ObjectId("5c8f4bcb9fc9074914ce52eb"), "name" : "mytest1", "age" : 1 } > db.myuser.find({"_id" : ObjectId("5c8f4bcb9fc9074914ce52eb")}).explain(true) { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.myuser", "indexFilterSet" : false, "parsedQuery" : { "_id" : { "$eq" : ObjectId("5c8f4bcb9fc9074914ce52eb") } }, "winningPlan" : { "stage" : "IDHACK" }, "rejectedPlans" : [ ] }, "executionStats" : { "executionSuccess" : true, "nReturned" : 1, "executionTimeMillis" : 1, "totalKeysExamined" : 1, "totalDocsExamined" : 1, "executionStages" : { "stage" : "IDHACK", #没有扫描全表 "nReturned" : 1, "executionTimeMillisEstimate" : 0, "works" : 2, "advanced" : 1, "needTime" : 0, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "invalidates" : 0, "keysExamined" : 1, "docsExamined" : 1 }, "allPlansExecution" : [ ] }, "serverInfo" : { "host" : "c7-node01", "port" : 27017, "version" : "4.0.2", "gitVersion" : "fc1573ba18aee42f97a3bb13b67af7d837826b47" }, "ok" : 1 } >

##添加索引

> db.myuser.ensureIndex({age: 1}) #以age 为升序索引

{

"createdCollectionAutomatically" : false,

"numIndexesBefore" : 1,

"numIndexesAfter" : 2,

"ok" : 1

}

>

查看索引

> db.myuser.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.myuser" }, { "v" : 2, "key" : { "age" : 1 }, "name" : "age_1", "ns" : "test.myuser" } ] > > db.myuser.find({age:39999}).explain(true) { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.myuser", "indexFilterSet" : false, "parsedQuery" : { "age" : { "$eq" : 39999 } }, "winningPlan" : { "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "age" : 1 }, "indexName" : "age_1", "isMultiKey" : false, "multiKeyPaths" : { "age" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "age" : [ "[39999.0, 39999.0]" ] } } }, "rejectedPlans" : [ ] }, "executionStats" : { "executionSuccess" : true, "nReturned" : 1, "executionTimeMillis" : 8, "totalKeysExamined" : 1, "totalDocsExamined" : 1, "executionStages" : { "stage" : "FETCH", "nReturned" : 1, "executionTimeMillisEstimate" : 0, "works" : 2, "advanced" : 1, "needTime" : 0, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "invalidates" : 0, "docsExamined" : 1, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 1, "executionTimeMillisEstimate" : 0, "works" : 2, "advanced" : 1, "needTime" : 0, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "invalidates" : 0, "keyPattern" : { "age" : 1 }, "indexName" : "age_1", "isMultiKey" : false, "multiKeyPaths" : { "age" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "age" : [ "[39999.0, 39999.0]" ] }, "keysExamined" : 1, "seeks" : 1, "dupsTested" : 0, "dupsDropped" : 0, "seenInvalidated" : 0 } }, "allPlansExecution" : [ ] }, "serverInfo" : { "host" : "c7-node01", "port" : 27017, "version" : "4.0.2", "gitVersion" : "fc1573ba18aee42f97a3bb13b67af7d837826b47" }, "ok" : 1 } >

###使用正则的话 索引无效果

测试

db.myuser.find({name: 'mytest66'})

db.myuser.ensureIndex({name: 1})

db.myuser.find({name: "mytest6"}) #快

db.myuser.find({"name": /99999/}) #慢

db.myuser.find({"name": /99999/}).explain(true) #使用正则全表扫描 慢

keyExamined:500000 #全表扫描

####MongoDB建立唯一索引 唯一索引对应的值不能重复

use mgtest

db.myuser.insert({userid: 1})

db.myuser.insert({userid: 1}) #可以插入

db.myuser.remove({}) #清空数据

db.myuser.ensureIndex({userid: 1},{unique: true}) #创建唯一索引

db.myuser.insert({userid: 1})

db.myuser.insert({userid: 1}) #报错 ,因为索引是唯一的

######MongoDB数据库的监控命令

mongostat可以实时监控MongoDB的状态 一直刷新输出

mongostat --help

mongostat -h 127.0.0.1:27017

测试

for(i=1;i<=300000;i ){

db.myuser.insert({name: 'mytest' i,age:i})

}

[root@c7-node01 ~]# mongostat -h 127.0.0.1:27017 insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time 330 *0 *0 *0 0 2|0 0.1% 28.9% 0 1.09G 204M 0|0 1|0 60.6k 77.7k 2 Mar 18 17:21:46.161 289 *0 *0 *0 0 1|0 0.1% 28.9% 0 1.09G 204M 0|0 1|0 53.2k 68.7k 2 Mar 18 17:21:47.258 345 *0 *0 *0 0 2|0 0.1% 28.9% 0 1.09G 204M 0|0 1|0 63.4k 81.8k 2 Mar 18 17:21:48.179 303 *0 *0 *0 0 2|0 0.1% 28.9% 0 1.09G 204M 0|0 1|0 55.6k 80.7k 2 Mar 18 17:21:49.090 335 *0 *0 *0 0 2|0 0.2% 28.9% 0 1.09G 204M 0|0 1|0 61.6k 76.7k 2 Mar 18 17:21:50.082 353 *0 *0 *0 0 1|0 0.2% 29.0% 0 1.09G 204M 0|0 1|0 64.9k 76.4k 2 Mar 18 17:21:51.091 354 *0 *0 *0 0 2|0 0.2% 29.0% 0 1.09G 204M 0|0 1|0 65.0k 77.6k 2 Mar 18 17:21:52.081 277 *0 *0 *0 0 1|0 0.2% 29.0% 0 1.09G 204M 0|0 1|0 50.9k 71.7k 2 Mar 18 17:21:53.112 218 *0 *0 *0 0 2|0 0.2% 29.0% 0 1.09G 204M 0|0 1|0 40.1k 71.8k 2 Mar 18 17:21:54.096 MongoDB监控值serverStatus serverStatus可用来获取MongoDB的状态信息 db.serverStatus() #查看索引的监控信息 db.serverStatus().network #单独查看网络流浪信息 db.serverStatus().opcounters #统计增 删 改 查的次数 db.serverStatus().connections #连接 > db.serverStatus().opcounters { "insert" : 712976, "query" : 50, "update" : 43, "delete" : 9, "getmore" : 0, "command" : 1853 } > > db.serverStatus().connections { "current" : 2, "available" : 52426, "totalCreated" : 19 } > > db.serverStatus().mem { "bits" : 64, "resident" : 205, "virtual" : 1116, "supported" : true, "mapped" : 0, "mappedWithJournal" : 0 }

使用shell获取监控信息 非交互式获取

echo 'db.serverStatus()'|/usr/local/mongodb/bin/mongo 127.0.0.1:27017

echo 'db.serverStatus().opcounters'|/usr/local/mongodb/bin/mongo 127.0.0.1:27017

echo 'db.serverStatus().opcounters'|/usr/local/mongodb/bin/mongo 127.0.0.1:27017|grep insert

echo 'db.serverStatus().opcounters'|/usr/local/mongodb/bin/mongo 127.0.0.1:27017|grep insert|egrep -o '[0-9] '

MongoDB副本集的搭建

MongoDB单台服务器

数据会有丢失的风险,单台服务器无法做高可用性 MongoDB副本集能够预防数据丢失 多台MongoDB数据一致

MongoDB副本集能在有问题的时候自动切换

使用2台服务器实战MongoDB副本集 生产环境中建议至少三台服务器

两台服务器IP:192.168.128.162(2个实例27017/27019) 192.168.128.163(27018)

192.168.128.162

MongoDB副本集配置文件 /data/mongodb/27017/conf/mongodb.conf

systemLog: destination: file logAppend: true path: /data/mongodb/27017/logs/mongodb.log storage: dbPath: /data/mongodb/27017/data/ journal: enabled: true processMangement: fork: true net: port: 27017 bindIp: 127.0.0.1 replication: replSetName: mgtest mkdir /data/mongodb/27019/{data,conf,logs} vim /data/mongodb/27019/mongodb.conf systemLog: destination: file logAppend: true path: /data/mongodb/27019/logs/mongodb.log storage: dbPath: /data/mongodb/27019/data/ journal: enabled: true processMangement: fork: true net: port: 27019 bindIp: 127.0.0.1 replication: replSetName: mgtest

##192.168.128.163

mkdir /data/mongodb/27018/{data,conf,logs} vim /data/mongodb/27018/mongodb.conf systemLog: destination: file logAppend: true path: /data/mongodb/27018/logs/mongodb.log storage: dbPath: /data/mongodb/27018/data/ journal: enabled: true processMangement: fork: true net: port: 27018 bindIp: 127.0.0.1 replication: replSetName: mgtest

###启动三个MongoDB实例

/usr/local/mongodb/bin/mongod -f /data/mongodb/27017/conf/mongondb.conf

/usr/local/mongodb/bin/mongod -f /data/mongodb/27018/conf/mongondb.conf

/usr/local/mongodb/bin/mongod -f /data/mongodb/27019/conf/mongondb.conf

MongoDB副本集的初始化及状态查看

config = { _id:"mgtest",members:[ {_id:0,host: "192.168.128.162:27017"}, {_id:1,host: "192.168.128.163:27018"}, {_id:2,host: "192.168.128.162:27019"}] } > config = { _id:"mgtest",members:[ ... {_id:0,host: "192.168.128.162:27017"}, ... {_id:1,host: "192.168.128.163:27018"}, ... {_id:2,host: "192.168.128.162:27019"}] ... } { "_id" : "mgtest", "members" : [ { "_id" : 0, "host" : "192.168.128.162:27017" }, { "_id" : 1, "host" : "192.168.128.163:27018" }, { "_id" : 2, "host" : "192.168.128.162:27019" } ] } > use admin switched to db admin > rs.initiate( config ) { "ok" : 1, "operationTime" : Timestamp(1551206629, 1), "$clusterTime" : { "clusterTime" : Timestamp(1551206629, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } mgtest:SECONDARY> use admin rs.initiate( config );#副本集初始化 rs.status() #副本集状态 一个primary 其它secondary。primary是主 只有primary能写入 > use admin switched to db admin

验证和同步

/usr/local/mongodb/bin/mongo 192.168.128.163:27018

use mgtest

db.myuser.insert({userid: 1})

###坑坑

参数要一致

rs.slaveOk() #secondary 需要声明是slave才能查看数据

mgtest:SECONDARY> rs.slaveOk() mgtest:SECONDARY> show dbs; admin 0.000GB config 0.000GB local 0.000GB mgtest 0.000GB mgtest:SECONDARY> use mgtest switched to db mgtest mgtest:SECONDARY> db.myuser.find() { "_id" : ObjectId("5c758ce0f615a72df7261710"), "userid" : 1 } mgtest:SECONDARY> mgtest:SECONDARY> db.myuser.insert({userid:2}) WriteCommandError({ "operationTime" : Timestamp(1551209152, 1), "ok" : 0, "errmsg" : "not master", "code" : 10107, "codeName" : "NotMaster", "$clusterTime" : { "clusterTime" : Timestamp(1551209152, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }) mgtest:SECONDARY>

##查看延迟

mgtest:PRIMARY> rs.printSlaveReplicationInfo() source: 192.168.128.162:27017 syncedTo: Wed Feb 27 2019 03:27:52 GMT 0800 (CST) 0 secs (0 hrs) behind the primary source: 192.168.128.162:27019 syncedTo: Wed Feb 27 2019 03:27:52 GMT 0800 (CST) 0 secs (0 hrs) behind the primary mgtest:PRIMARY>

MongoDB的副本自动切换

MongoDB的副本集当primary挂了 会挑选其中的一台secondary升级为主

挑选其中一台secondary升级为primary的条件是剩下的集群台数>=2

如果集群只剩下一个实例的话 会有异常

MongoDB副本集自动切换演练

/usr/local/mongodb/bin/mongo 192.168.128.162:27018

use admin

db.shutdownServer() #关闭主MongoDB 会有其他的MongoDB提升为主 插入数据正常

/usr/local/mongodb/bin/mongo 192.168.128.162:27017

use admin

db.shutdownServer() #关闭两台MongoDB的话 剩下的一台不会提升为primary 插入数据将异常

启动27018和27017 没有固定的主 可通过优先级指定主

/usr/local/mongodb/bin/mongod -f /data/mongodb/27017/conf/mongodb.conf

/usr/local/mongodb/bin/mongod -f /data/mongodb/27018/conf/mongodb.conf

###MongoDB副本的优先级

MongoDB副本集的主的优先级

primary的选举依赖于各个实例的优先权重 默认权重都是1

副本集的主挑选权重最高的 权重一样的无法控制谁为主

设置各个实例的优先级权重 挑选自己想要的实例为主 只有primary可以更改权重配置

rs.config() #获取集群配置信息 必须在primary上执行

conf = rs.config() #赋值给conf 然后通过conf进行修改

conf.members[0].priority = 10 #索引从0开始 每次递增1 类似数组 优先级高的位主

conf.members[1].priority = 5

conf.members[2].priority = 2

rs.reconfig(config) #更新MongoDB副本集的配置 优先权重最高的提升为主 关闭启动后也为主

MongoDB副本集的伸缩

MongoDB副本集的扩展非常好 往副本集里添加实例和移除实例都非常方便

往MongoDB副本集添加实例数据能够自动同步 无需人工干预

往现有的MongoDB副本集中添加实例

创建27020实例 注意副本集名称要保持一致replSetName: mgtest

use admin

rs.add('192.168.128.163:27020') #所有数据自动同步

rs.add的优先权重默认为1

从MongoDB副本机制移除实例 不可移除primary

use admin

rs.remove('192.168.128.163:27020')

rs.remove('192.168.128.162:27017')

副本集经过添加删除后顺序会乱 设置权重需要注意

先rs.config()获取实例的位置 再修改优先级

rs.add('192.168.128.162:27017')

MongoDB的备份和恢复

单台服务器一定需要备份

MongoDB有提供mongodump工具用来备份数据

MongoDB也有提供mongorestore工具来恢复数据

MongoDB备份说明 指定ip和端口

单台服务器直接使用mongodump进行备份

副本集需要连接到primary上进行备份

数据清空 启动单台MongoDB

systenLog:

destination: file

logAppend: true

path: /data/mongodb/27017/logs/mongodb.log

storage:

dbPath: /data/mongodb/27017/data/

journal:

enabled: true

processManagement:

fork: true

net:

port: 27017

bindIp: 192.168.128.162

:%/27017/27018/g #vim全局替换

MongoDB数据库备份 备份所有库

/usr/local/mongodb/bin/mongodump -h 192.168.128.162 --port 27017 -o /data/backup/

也可以-h ip:port

ll -h /data/backup/

MongoDB数据库的恢复

创建个新MongoDB实例27018

/usr/local/mongodb/bin/mongorestore -h 192.168.128.162 --port 27018 /data/backup/

使用Python简单操作MongoDB

使用程序操作MongoDB

MongoDB可以使用java Python PHP等语言去操作

运维使用Python居多 介绍使用Python操作MongoDB数据库

使用Python操作MongoDB 需要使用pip安装pymongo模块

yum install -y epel-release

yum install -y python2-pip

#yum install -y python34-pip

pip install pymongo

连接代码 单实例的连接代码

import pymongo

client = pymongo.MongoClient('192.168.128.162',27017)

mgtest = client.mgtest

myuser = mgtest.myuser

for item in myuser.find():

print(item)

pymongo副本集的连接代码 代码支持自动切换

import pymongo

client = pymongo.MongoClient(['192.168.128.162:27017','192.168.128.162:27018','192.168.128.162:27019',192.168.128.162:27020'])

mgtest = client.mgtest

myuser = mgtest.myuser

myvar = {'age':28,'name':'mgtest1'} #down掉一个实例 测试insert 数据

myuser.insert(myvar)

for item in myuser.find():

print(item)

python获取MongoDB的状态信息

MongoDB监控

使用Python去监控MongoDB状态

获取MongoDB的serverStatus状态信息,一般状态信息需要每个实例都进行监控

import pymongo

client = pymongo.MongoClient('192.168.128.162:27017')

db = client.admin

serverStatus = db.command('serverStatus')

print(serverStatus)

每个状态信息单独打印

import pymongo

client = pymongo.MongoClient('192.168.128.162:27017')

db = client.admin

serverStatus = db.command('serverStatus')

for key,value in serverStatus.items():

print(key,value)

print()

MongoDB建议的监控方向

connection #连接信息

network #流量信息

opcounters #增删改查

状态信息获取

import pymongo

client = pymongo.MongoClient('192.168.128.162:27017')

db = client.admin

serverStatus = db.command('serverStatus')

print(serverStatus['connection'])

print(serverStatus['network'])

print(serverStatus['opcounters'])

print(serverStatus['opcounters']['query'])

print(serverStatus['opcounters']['insert'])

print(serverStatus['opcounters']['update'])

print(serverStatus['opcounters']['delete'])

MongoDB分片

不是大企业 一般用的比较少 需要较多的服务器 有三种角色

一般吧MongoDB的副本集应用的好就足够了 可搭建多套MongoDB副本集

MongoDB分片技术

MongoDB副本集可以解决数据备份 读性能的问题 但由于MongoDB副本是每份数据都一模一样的 无法解决数据量过大的问题

MongoDB分片技术能够把数据分成两份存储 假如mgtest.myuser里面有1亿条数据 分片能够实现5千万左右存储在mongodb1,5千万左右存储在MongoDB2里

MongoDB1,MongoDB2需要使用副本集的形式 预防数据丢失

生产环境中如果数据量不大 直接使用副本集即可 数据量大 使用分片 副本集

MongoDB分片集群角色介绍

route角色 #MongoDB的路由 提供入口 使得分片集群对外透明 router不存储数据

configsvr角色 #MongoDB的配置服务 存储元数据信息 分片集群后端有多分存储 读取数据改去哪个存储上读取 依赖于元数据 建议使用副本集

shardsvr角色 #MongoDB的存储服务 存储真正的数据 建议使用副本集

依赖关系

当用户通过route角色插入数据时 需要从configsvr知道这份数据插入到哪个节点 然后执行插入动作插入数据到shardsvr

当用户通过route角色获取数据时 需要从configsvr知道这份数据是存储在哪个节点 然后再去shardsvr获取数据

MongoDB分片集群的搭建说明

使用同一份MongoDB二进制包

修改对应的配置就能实现分片集群的搭建

MongoDB分片集群实战环境搭建说明

configsvr #使用28017,28018,28019三个端口来搭建

route #使用27017,27018,27019三个端口来搭建

shardsvr #使用29017,29018,29019,29020四个端口来搭建 生产环境使用三个端口一个集群

MongoDB配置角色configsvr的搭建 配置文件路径/data/mongodb/28017/conf/mongodb.conf

systemLog:

destination: file

logAppend: true

path: /data/mongodb/28017/logs/mongodb.log

storage:

dbPath: /data/mongodb/28017/data/

journal:

enabled: true

processManagement:

fork: true

net:

port: 28017

bindIp: 192.168.128.162

replication:

replSetName: mgtestconf

sharding:

clusterRole: configsvr

#28018 28019 ...

启动3个实例

分片集群的配置角色副本集搭建

config = {_id:"mgtestconf",

configsvr: true,

members:[

{_id:0,host:"192.168.128.162:28017"},

{_id:0,host:"192.168.128.162:28018"},

{_id:0,host:"192.168.128.162:28019"}

]

}

rs.initiate(config)

验证是否搭建成功

mongod 192.168.128.162:28017

rs.status()

####分片集群的route角色

route说明

MongoDB中的router角色只负责提供一个入口 不存储任何的数据

MongoDB分片集群之router角色配置/data/mongodb/27017/conf/mongodb.conf

systemLog:

destination: file

logAppend: true

path: /data/mongodb/27017/logs/mongodb.log

processManagement:

fork: true

net:

port: 27017

bindIp: 192.168.128.162

sharding:

configDB: mgtestconf/192.168.128.162:28017,192.168.128.162:28018,192.168.128.162:28019

router最重要的配置

指定configsvr的地址 使用副本集ip:port的方式指定

配置多个router 任何一个都能正常的获取数据

router的启动

mongos -f /data/mongodb/27017/conf/mongodb.conf

router验证

需要等到数据角色(sharedsvr)搭建完成才能验证

#####mongodb分片集群sharedsvr数据角色的实战部署

分片集群的数据角色里面存储着真正的数据 所以数据角色一定得使用副本集

MongoDB的数据服务配置文件/data/mongodb/29017/conf/mongodb.conf

systemLog:

destination: file

logAppend: true

path: /data/mongodb/29017/logs/mongodb.log

storage:

dbPath: /data/mongodb/29017/data/

journal:

enabled: true

precessManagement:

fork: true

net:

port: 29017

bindIp: 192.168.128.162

replication:

replSetName: mgtestdata1

sharding:

clusterRole: shardsvr

数据服务两个集群说明

29017 29018 29019 数据角色mgtestdata1

29020 29021 29022 数据角色mgtestdata2

.....................mgtestdata3

启动6个实例

mongod -f 29017/mongodb.conf

.....

29017集群数据角色mgtestdata1

config = {_id:"mgtestdata1",

members:[

{_id:0,host:"192.168.128.162:29017"},

{_id:0,host:"192.168.128.162:29018"},

{_id:0,host:"192.168.128.162:29019"}

]

}

rs.initiate(config)

29020...集群mgtestdata2

config = {_id:"mgtestdata1",

members:[

{_id:0,host:"192.168.128.162:29020"},

{_id:0,host:"192.168.128.162:29021"},

{_id:0,host:"192.168.128.162:29022"}

]

}

rs.initiate(config)

服务器分配

6台服务器

mgtestdata1 3台

mgtestdata2 3台 配置角色 路由角色

######MongoDB分片集群使用

分片集群添加数据角色 连接到路由角色里面配置 数据角色为副本集的方式

/usr/local/mongodb/bin/mongo 192.168.128.162:27017

sh.addShard("mgtestdata1/192.168.128.162:29017,192.168.128.162:29018")

sh.addShard("mgtestdata2/192.168.128.162:29019,192.168.128.162:29020")

sh.status()

默认添加数据没有分片存储 操作都是在路由角色里面

use mgtest

for(i=1;i<=500;i ){

db.myuser.insert({name: 'mytest' 1,age:i})

}

db.dropDatabase() #验证完后删除

针对某个数据库的某个表使用hash分片存储 分片存储就会同一个collection分配两个数据角色

use admin

db.runCommand({enablesharding:"mgtest"}); #启用分片

db.runCommand({shardcollection:"mgtest.myuser",key:{_id:"hashed"}})

插入数据校验 分布在两个数据集群上

use mgtest

for(i=1;i<=2000;i ){

db.myuser.insert({name: 'mytest' 1,age:i})

}

配置角色挂掉一个会不会有影响

验证mongos多个入口是否能够正常使用

-----副本集 sharing-------

架构规划

192.168.128.170 节点1 192.168.128.171 节点2 192.168.128.172 节点3

mongos:10005 mongos:10005 mongos:10005

configsvr:10004 configsvr:10004 configsvr:10004

shard1:10001(主节点) shard1:10001(副本) shard1:10001(仲裁)

shard1:10002(仲裁节点) shard1:10002(主节点) shard1:10002(副本)

shard1:10003(副本) shard1:10003(仲裁) shard1:10003(主节点)

目录规划

data目录 config 目录 logs 目录

/data/mongo_cluster/config/ /data/mongo_cluster/logs/

/data/mongo_cluster/shard1/ /data/mongo_cluster/shard1/shard1.conf /data/mongo_cluster/logs/shard1/shard1.log

/data/mongo_cluster/shard2/ /data/mongo_cluster/shard2/shard2.conf /data/mongo_cluster/logs/shard1/shard2.log

/data/mongo_cluster/shard3/ /data/mongo_cluster/shard3/shard3.conf /data/mongo_cluster/logs/shard1/shard3.log

/data/mongo_cluster/mongos/ /data/mongo_cluster/mongos.conf /data/mongo_cluster/logs/mongos/mongos.log

/data/mongo_cluster/logs/

/data/mongo_cluster/configsvr/ /data/mongo_cluster/configsvr/configsvr.conf /data/mongo_cluster/configsvr/logs/configsvr.log

/data/mongo_cluster/keyfile/

节点概述

192.168.128.170、shard1:10001、shard2:10002、shard3:10003、configsvr:10004、mongos:10005

注:shard1主节点,shard2仲裁,shard3副本

192.168.128.171、shard1:10001、shard2:10002、shard3:10003、configsvr:10004、mongos:10005

注:shard1副本,shard2主节点,shard3仲裁

192.168.128.172、shard1:10001、shard2:10002、shard3:10003、configsvr:10004、mongos:10005

注:shard1仲裁,shard2副本,shard3主节点

mgd-node1:192.168.128.170

mgd-node2:192.168.128.171

mgd-node3:192.168.128.172

用户创建

useradd mongodb

passwd mongodb

mongodb

mkdir -pv /data/mongo_cluster/ && chown -R mongodb. /data/mongo_cluster/

su - mongodb

mkdir /data/mongo_cluster/{config,shard1,shard2,shard3,mongos,logs,configsvr,keyfile} -pv

touch /data/mongo_cluster/keyfile/zxl

touch /data/mongo_cluster/logs/shard{1..3}.log

touch /data/mongo_cluster/logs/{configsvr,mongos}.log

touch /data/mongo_cluster/config/shard{1..3}.conf

touch /data/mongo_cluster/config/{configsvr,mongos}.conf

下载

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.13.tgz

tar xf mongodb-linux-x86_64-3.6.13.tgz -C /data/mongo_cluster/

cd /data/mongo_cluster/ && ln -s mongodb-linux-x86_64-3.6.13 mongodb

echo "export PATH=$PATH:/data/mongodb/bin" >> ~/.bash_profile

source ~/.bash_profile

shard1.conf配置文件内容

cat /data/mongo_cluster/config/shard1.conf

systemLog:

destination: file

path: /data/mongo_cluster/logs/shard1.log

logAppend: true

processManagement:

fork: true

pidFilePath: "/data/mongo_cluster/shard1/shard1.pid"

net:

port: 10001

storage:

dbPath: "/data/mongo_cluster/shard1"

engine: wiredTiger

journal:

enabled: true

directoryPerDB: true

operationProfiling:

slowOpThresholdMs: 10

mode: "slowOp"

#security:

# keyFile: "/data/mongo_cluster/keyfile/zxl"

# clusterAuthMode: "keyFile"

replication:

oplogSizeMB: 50

replSetName: "shard1_zxl"

secondaryIndexPrefetch: "all"

------shard2.conf配置文件内容如下------------

systemLog:

destination: file

path: /data/mongo_cluster/logs/shard2.log

logAppend: true

processManagement:

fork: true

pidFilePath: "/data/mongo_cluster/shard2/shard2.pid"

net:

port: 10002

storage:

dbPath: "/data/mongo_cluster/shard2"

engine: wiredTiger

journal:

enabled: true

directoryPerDB: true

operationProfiling:

slowOpThresholdMs: 10

mode: "slowOp"

#security:

# keyFile: "/data/mongo_cluster/keyfile/zxl"

# clusterAuthMode: "keyFile"

replication:

oplogSizeMB: 50

replSetName: "shard2_zxl"

secondaryIndexPrefetch: "all"

---------shard3.conf配置文件内容如下-------

systemLog:

destination: file

path: /data/mongo_cluster/logs/shard3.log

logAppend: true

processManagement:

fork: true

pidFilePath: "/data/mongo_cluster/shard3/shard3.pid"

net:

port: 10003

storage:

dbPath: "/data/mongo_cluster/shard3"

engine: wiredTiger

journal:

enabled: true

directoryPerDB: true

operationProfiling:

slowOpThresholdMs: 10

mode: "slowOp"

#security:

# keyFile: "/data/mongo_cluster/keyfile/zxl"

# clusterAuthMode: "keyFile"

replication:

oplogSizeMB: 50

replSetName: "shard3_zxl"

secondaryIndexPrefetch: "all"

------configsvr.conf配置文件内容如下---

systemLog:

destination: file

path: /data/mongo_cluster/logs/configsvr.log

logAppend: true

processManagement:

fork: true

pidFilePath: "/data/mongo_cluster/configsvr/configsvr.pid"

net:

port: 10004

storage:

dbPath: "/data/mongo_cluster/configsvr"

engine: wiredTiger

journal:

enabled: true

#security:

# keyFile: "/data/mongo_cluster/keyfile/zxl"

# clusterAuthMode: "keyFile"

sharding:

clusterRole: configsvr

---------mongos.conf配置文件内容如下----------

systemLog:

destination: file

path: /data/mongo_cluster/logs/mongos.log

logAppend: true

processManagement:

fork: true

pidFilePath: /data/mongo_cluster/mongos/mongos.pid

net:

port: 10005

sharding:

configDB: 192.168.42.41:10004,192.168.42.42:10004,192.168.42.43:10004

#security:

# keyFile: "/data/mongo_cluster/keyfile/zxl"

# clusterAuthMode: "keyFile"

注:以上操作只是在node1机器上操作,请把上面这些操作步骤在另外2台机器操作一下,

包括创建用户创建目录文件以及安装mongodb等,以及文件拷贝到node2、node3对应的目录下,

拷贝之后查看一下文件的属主属组是否为mongodb。

启动各个机器节点的mongod,shard1、shard2、shard3

mongod -f /data/mongo_cluster/config/shard1.conf

mongod -f /data/mongo_cluster/config/shard2.conf

mongod -f /data/mongo_cluster/config/shard3.conf

配置复制集

node1机器上操作配置复制集

[mongodb@node1 config]$ mongo --port 10001 MongoDB shell version: 3.2.3 connecting to: 127.0.0.1:10001/test > use admin switched to db admin > config = { _id:"shard1_zxl", members:[ ... ... {_id:0,host:"192.168.42.41:10001"}, ... ... {_id:1,host:"192.168.42.42:10001"}, ... ... {_id:2,host:"192.168.42.43:10001",arbiterOnly:true} ... ... ] ... ... } { "_id" : "shard1_zxl", "members" : [ { "_id" : 0, "host" : "192.168.42.41:10001" }, { "_id" : 1, "host" : "192.168.42.42:10001" }, { "_id" : 2, "host" : "192.168.42.43:10001", "arbiterOnly" : true } ] } > rs.initiate(con config connect( connectionURLTheSame( constructor > rs.initiate(config) { "ok" : 1 }

node2机器上操作配置复制集

[mongodb@node2 config]$ mongo --port 10002 MongoDB shell version: 3.2.3 connecting to: 127.0.0.1:10002/test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user > use admin switched to db admin > config = { _id:"shard2_zxl", members:[ ... ... {_id:0,host:"192.168.42.42:10002"}, ... ... {_id:1,host:"192.168.42.43:10002"}, ... ... {_id:2,host:"192.168.42.41:10002",arbiterOnly:true} ... ... ] ... ... } { "_id" : "shard2_zxl", "members" : [ { "_id" : 0, "host" : "192.168.42.42:10002" }, { "_id" : 1, "host" : "192.168.42.43:10002" }, { "_id" : 2, "host" : "192.168.42.41:10002", "arbiterOnly" : true } ] } > rs.initiate(config) { "ok" : 1 }

node3机器上操作配置复制集

[mongodb@node3 config]$ mongo --port 10003 MongoDB shell version: 3.2.3 connecting to: 127.0.0.1:10003/test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user > use admin switched to db admin > config = {_id:"shard3_zxl", members:[ ... ... {_id:0,host:"192.168.42.43:10003"}, ... ... {_id:1,host:"192.168.42.41:10003"}, ... ... {_id:2,host:"192.168.42.42:10003",arbiterOnly:true} ... ... ] ... ... } { "_id" : "shard3_zxl", "members" : [ { "_id" : 0, "host" : "192.168.42.43:10003" }, { "_id" : 1, "host" : "192.168.42.41:10003" }, { "_id" : 2, "host" : "192.168.42.42:10003", "arbiterOnly" : true } ] } > rs.initiate(config) { "ok" : 1 }

注:以上是配置rs复制集,相关命令如:rs.status(),查看各个复制集的状况

启动三台机器上的configsvr和mongos节点

[mongodb@node1 logs]$ mongod -f /data/config/configsvr.conf about to fork child process, waiting until server is ready for connections. forked process: 6317 child process started successfully, parent exiting [mongodb@node1 logs]$ mongos -f /data/config/mongos.conf about to fork child process, waiting until server is ready for connections. forked process: 6345 child process started successfully, parent exiting

查看端口是否启动:

[mongodb@elk_node1 ~]$ ss -atunlp|grep mong tcp LISTEN 0 128 *:10001 *:* users:(("mongod",2630,6)) tcp LISTEN 0 128 *:10002 *:* users:(("mongod",2654,6)) tcp LISTEN 0 128 *:10003 *:* users:(("mongod",2678,6)) tcp LISTEN 0 128 *:10004 *:* users:(("mongod",3053,6)) tcp LISTEN 0 128 *:10005 *:* users:(("mongos",3157,21))

配置shard分片

在node1机器上配置shard分片

[mongodb@node1 config]$ mongo --port 10005 MongoDB shell version: 3.2.3 connecting to: 127.0.0.1:10005/test mongos> use admin switched to db admin mongos> db.runCommand({addshard:"shard1_zxl/192.168.42.41:10001,192.168.42.42:10001,192.168.42.43:10001"}); { "shardAdded" : "shard1_zxl", "ok" : 1 } mongos> db.runCommand({addshard:"shard2_zxl/192.168.42.41:10002,192.168.42.42:10002,192.168.42.43:10002"}); { "shardAdded" : "shard2_zxl", "ok" : 1 } mongos> db.runCommand({addshard:"shard3_zxl/192.168.42.41:10003,192.168.42.42:10003,192.168.42.43:10003"}); { "shardAdded" : "shard3_zxl", "ok" : 1 }

查看shard信息

mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("56de6f4176b47beaa9c75e9d") } shards: { "_id" : "shard1_zxl", "host" : "shard1_zxl/192.168.42.41:10001,192.168.42.42:10001" } { "_id" : "shard2_zxl", "host" : "shard2_zxl/192.168.42.42:10002,192.168.42.43:10002" } { "_id" : "shard3_zxl", "host" : "shard3_zxl/192.168.42.41:10003,192.168.42.43:10003" } active mongoses: "3.2.3" : 3 balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases:

查看分片状态

mongos> db.runCommand( {listshards : 1 } ) { "shards" : [ { "_id" : "shard1_zxl", "host" : "shard1_zxl/192.168.42.41:10001,192.168.42.42:10001" }, { "_id" : "shard2_zxl", "host" : "shard2_zxl/192.168.42.42:10002,192.168.42.43:10002" }, { "_id" : "shard3_zxl", "host" : "shard3_zxl/192.168.42.41:10003,192.168.42.43:10003" } ], "ok" : 1 }

启用shard分片的库名字为'zxl',即为库

mongos> sh.enableSharding("zxl")

{ "ok" : 1 }

设置集合的名字以及字段,默认自动建立索引,zxl库,haha集合

mongos> sh.shardCollection("zxl.haha",{age: 1, name: 1})

{ "collectionsharded" : "zxl.haha", "ok" : 1 }

模拟在haha集合中插入10000数据

mongos> for (i=1;i<=10000;i ) db.haha.insert({name: "user" i, age: (i0)})

WriteResult({ "nInserted" : 1 })

可以使用上面mongos> sh.status()命令查看各个shard分片情况,以上就是复制集和shard分片搭建完成。

结果如下:

mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("56de6f4176b47beaa9c75e9d") } shards: { "_id" : "shard1_zxl", "host" : "shard1_zxl/192.168.42.41:10001,192.168.42.42:10001" } { "_id" : "shard2_zxl", "host" : "shard2_zxl/192.168.42.42:10002,192.168.42.43:10002" } { "_id" : "shard3_zxl", "host" : "shard3_zxl/192.168.42.41:10003,192.168.42.43:10003" } active mongoses: "3.2.3" : 3 balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: 2 : Success databases: { "_id" : "zxl", "primary" : "shard3_zxl", "partitioned" : true } zxl.haha shard key: { "age" : 1, "name" : 1 } unique: false balancing: true chunks: shard1_zxl1 shard2_zxl1 shard3_zxl1 { "age" : { "$minKey" : 1 }, "name" : { "$minKey" : 1 } } -->> { "age" : 2, "name" : "user2" } on : shard1_zxl Timestamp(2, 0) { "age" : 2, "name" : "user2" } -->> { "age" : 22, "name" : "user22" } on : shard2_zxl Timestamp(3, 0) { "age" : 22, "name" : "user22" } -->> { "age" : { "$maxKey" : 1 }, "name" : { "$maxKey" : 1 } } on : shard3_zxl Timestamp(3, 1)

--mongodb.conf---

systemLog:

destination: file #日志输出的目的地 指定一个file 或者syslog 如果指定file 必须指定logAppend

logAppend: true

path: /data/mongo_cluster/mongo_27017/logs/mongo.log

storage:

journal: #回滚日志

enabled: true

dbPath: /data/mongo_cluster/mongo_27017/data #数据目录

directoryPerDB: true #默认false 不适应inmemory engine

wiredTiger:

engineConfig:

cacheSizeGB: 1 #将用于所有数据缓存的最大值

directoryForIndexes: true #默认false 索引集合storage.dbPath存储在数据单独子目录

collectionConfig:

blockCompressor: zlib

indexConfig:

prefixCompression: true

processManagement: #使用出来系统守护进程的控制处理

fork: true

pidFilePath: /data/mongo_cluster/mongo_27017/pid/mongo_27017.pid

net:

port: 27017 #监听端口

bindIp: 192.168.128.171 #绑定 IP

,