GVKun编程网logo

mongodb3.2复制集和shard集群搭建(mongodb复制集的作用)

15

在这里,我们将给大家分享关于mongodb3.2复制集和shard集群搭建的知识,让您更了解mongodb复制集的作用的本质,同时也会涉及到如何更有效地mongodb3.4集群搭建升级版五台集群、mo

在这里,我们将给大家分享关于mongodb3.2复制集和shard集群搭建的知识,让您更了解mongodb复制集的作用的本质,同时也会涉及到如何更有效地mongodb 3.4 集群搭建升级版 五台集群、mongodb 4.0.5 集群搭建五台集群、MongoDB Sharding(二) -- 搭建分片集群、mongodb shard集群扩容和收缩的内容。

本文目录一览:

mongodb3.2复制集和shard集群搭建(mongodb复制集的作用)

mongodb3.2复制集和shard集群搭建(mongodb复制集的作用)

三台机器操作系统环境如下:

1

2

3

4

5

6

7

[mongodb@node1 ~]$ cat /etc/issue

CentOS release 6.4 (Final)

Kernel \r on an \m

[mongodb@node1 ~]$ uname -r

2.6.32-358.el6.x86_64

[mongodb@node1 ~]$ uname -m

x86_64

 

架构如下图,之前的架构图找不到了,就凑合看下面的表格吧。。

wKiom1bfhbagjVaTAAGCoOPjNbU972.jpg

 

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

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

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

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

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

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

node1:192.168.75.128

node2:192.168.75.129

node3:192.168.75.130

 

创建mongodb用户

1

2

3

4

5

[root@node1 ~]# groupadd  mongodb

[root@node1 ~]# useradd  -g mongodb mongodb

[root@node1 ~]# mkdir /data

[root@node1 ~]# chown mongodb.mongodb /data -R

[root@node1 ~]# su - mongodb

创建目录和文件

1

2

3

4

5

6

[mongodb@node1 ~]$ mkdir /data/{config,shard1,shard2,shard3,mongos,logs,configsvr,keyfile} -pv

[mongodb@node1 ~]$ touch /data/keyfile/zxl

[mongodb@node1 ~]$ touch /data/logs/shard{1..3}.log

[mongodb@node1 ~]$ touch /data/logs/{configsvr,mongos}.log

[mongodb@node1 ~]$ touch /data/config/shard{1..3}.conf

[mongodb@node1 ~]$ touch /data/config/{configsvr,mongos}.conf

下载mongodb

1

2

3

[mongodb@node1 ~]$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.3.tgz

[mongodb@node3 ~]$ tar fxz mongodb-linux-x86_64-rhel62-3.2.3.tgz -C /data

[mongodb@node3 ~]$ ln -s /data/mongodb-linux-x86_64-rhel62-3.2.3 /data/mongodb

配置mongodb环境变量

1

2

[mongodb@node1 ~]$ echo "export PATH=$PATH:/data/mongodb/bin" >> ~/.bash_profile

[mongodb@node1 data]$ source ~/.bash_profile

 

shard1.conf配置文件内容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

[mongodb@node1 ~]$ cat /data/config/shard1.conf 

systemLog:

  destination: file

  path: /data/logs/shard1.log

  logAppend: true

processManagement:

  fork: true

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

net:

  port: 10001

storage:

  dbPath: "/data/shard1"

  engine: wiredTiger

  journal:

    enabled: true

  directoryPerDB: true

operationProfiling:

  slowOpThresholdMs: 10

  mode: "slowOp"

#security:

#  keyFile: "/data/keyfile/zxl"

#  clusterAuthMode: "keyFile"

replication:

  oplogSizeMB: 50

  replSetName: "shard1_zxl"

  secondaryIndexPrefetch: "all"

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

[mongodb@node1 ~]$ cat /data/config/shard2.conf 

systemLog:

  destination: file

  path: /data/logs/shard2.log

  logAppend: true

processManagement:

  fork: true

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

net:

  port: 10002

storage:

  dbPath: "/data/shard2"

  engine: wiredTiger

  journal:

    enabled: true

  directoryPerDB: true

operationProfiling:

  slowOpThresholdMs: 10

  mode: "slowOp"

#security:

#  keyFile: "/data/keyfile/zxl"

#  clusterAuthMode: "keyFile"

replication:

  oplogSizeMB: 50

  replSetName: "shard2_zxl"

  secondaryIndexPrefetch: "all"

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

[mongodb@node1 ~]$ cat /data/config/shard3.conf 

systemLog:

  destination: file

  path: /data/logs/shard3.log

  logAppend: true

processManagement:

  fork: true

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

net:

  port: 10003

storage:

  dbPath: "/data/shard3"

  engine: wiredTiger

  journal:

    enabled: true

  directoryPerDB: true

operationProfiling:

  slowOpThresholdMs: 10

  mode: "slowOp"

#security:

#  keyFile: "/data/keyfile/zxl"

#  clusterAuthMode: "keyFile"

replication:

  oplogSizeMB: 50

  replSetName: "shard3_zxl"

  secondaryIndexPrefetch: "all"

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

[mongodb@node1 ~]$ cat /data/config/configsvr.conf 

systemLog:

  destination: file

  path: /data/logs/configsvr.log

  logAppend: true

processManagement:

  fork: true

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

net:

  port: 10004

storage:

  dbPath: "/data/configsvr"

  engine: wiredTiger

  journal:

    enabled: true

#security:

#  keyFile: "/data/keyfile/zxl"

#  clusterAuthMode: "keyFile"

sharding:

  clusterRole: configsvr

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

[mongodb@node3 ~]$ cat /data/config/mongos.conf 

systemLog:

  destination: file

  path: /data/logs/mongos.log

  logAppend: true

processManagement:

  fork: true

  pidFilePath: /data/mongos/mongos.pid

net:

  port: 10005

sharding:

  configDB: 192.168.75.128:10004,192.168.75.129:10004,192.168.75.130:10004

#security:

#  keyFile: "/data/keyfile/zxl"

#  clusterAuthMode: "keyFile"

注:以上操作只是在node1机器上操作,请把上面这些操作步骤在另外2台机器操作一下,包括创建用户创建目录文件以及安装mongodb等,以及文件拷贝到node2、node3对应的目录下,拷贝之后查看一下文件的属主属组是否为mongodb。关于configsvr的问题,官方建议1台或者3台,最好为奇数,你懂得。不懂的话自行搜索mongodb官方就知道答案了,链接找不到了,自己找找吧。

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

1

2

3

4

5

[mongodb@node1 ~]$ mongod -f /data/config/shard1.conf

mongod: /usr/lib64/libcrypto.so.10: no version information available (required by m

mongod: /usr/lib64/libcrypto.so.10: no version information available (required by m

mongod: /usr/lib64/libssl.so.10: no version information available (required by mong

mongod: relocation error: mongod: symbol TLSv1_1_client_method, version libssl.so.1n file libssl.so.10 with link time reference

注:无法启动,看到相应的提示后

解决:安装openssl即可,三台机器均安装openssl-devel

1

2

3

[mongodb@node1 ~]$ su - root

Password: 

[root@node1 ~]# yum install openssl-devel -y

再次切换mongodb用户启动三台机器上的mongod,shard1、shard2、shard3

1

2

3

4

5

6

7

8

9

10

11

12

[mongodb@node1 ~]$ mongod -f /data/config/shard1.conf

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

forked process: 1737

child process started successfully, parent exiting

[mongodb@node1 ~]$ mongod -f /data/config/shard2.conf

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

forked process: 1760

child process started successfully, parent exiting

[mongodb@node1 ~]$ mongod -f /data/config/shard3.conf

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

forked process: 1783

child process started successfully, parent exiting

进入node1机器上的mongod:10001登录

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

[mongodb@node1 ~]$ mongo --port 10001

MongoDB shell version: 3.2.3

connecting to: 127.0.0.1:10001/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

Server has startup warnings: 

2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten] 

2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/epage/enabled is ''always''.

2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten] **        We suggest settin

2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten] 

2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/epage/defrag is ''always''.

2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten] **        We suggest settin

2016-03-08T13:28:18.508+0800 I CONTROL  [initandlisten]

注:提示warning......

解决:在三台机器上均操作一下内容即可

1

2

3

4

[mongodb@node2 config]$ su - root

Password

[root@node1 ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled

[root@node1 ~]# echo never > /sys/kernel/mm/transparent_hugepage/defrag

关闭三台机器上的mongod实例,然后再次启动三台机器上mongod实例即可。

1

2

3

4

[mongodb@node1 ~]$ netstat -ntpl|grep mongo|awk ''{print $NF}''|awk -F''/'' ''{print $1}''|xargs kill 

[mongodb@node1 ~]$ mongod -f /data/config/shard1.conf

[mongodb@node1 ~]$ mongod -f /data/config/shard2.conf

[mongodb@node1 ~]$ mongod -f /data/config/shard3.conf

配置复制集

node1机器上操作配置复制集

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

[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.75.128:10001"},

... ... {_id:1,host:"192.168.75.129:10001"},

... ... {_id:2,host:"192.168.75.130:10001",arbiterOnly:true}

... ... ]

... ... }

{

"_id" "shard1_zxl",

"members" : [

{

"_id" : 0,

"host" "192.168.75.128:10001"

},

{

"_id" : 1,

"host" "192.168.75.129:10001"

},

{

"_id" : 2,

"host" "192.168.75.130:10001",

"arbiterOnly" true

}

]

}

> rs.initiate(con

config                 connect(               connectionURLTheSame(  constructor

> rs.initiate(config)

"ok" : 1 }

node2机器上操作配置复制集

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

[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.75.129:10002"},

... ... {_id:1,host:"192.168.75.130:10002"},

... ... {_id:2,host:"192.168.75.128:10002",arbiterOnly:true}

... ... ]

... ... }

{

"_id" "shard2_zxl",

"members" : [

{

"_id" : 0,

"host" "192.168.75.129:10002"

},

{

"_id" : 1,

"host" "192.168.75.130:10002"

},

{

"_id" : 2,

"host" "192.168.75.128:10002",

"arbiterOnly" true

}

]

}

> rs.initiate(config)

"ok" : 1 }

node3机器上操作配置复制集

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

[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.75.130:10003"},

... ... {_id:1,host:"192.168.75.128:10003"},

... ... {_id:2,host:"192.168.75.129:10003",arbiterOnly:true}

... ... ]

... ... }

{

"_id" "shard3_zxl",

"members" : [

{

"_id" : 0,

"host" "192.168.75.130:10003"

},

{

"_id" : 1,

"host" "192.168.75.128:10003"

},

{

"_id" : 2,

"host" "192.168.75.129:10003",

"arbiterOnly" true

}

]

}

> rs.initiate(config)

"ok" : 1 }

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

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

1

2

3

4

5

6

7

8

[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

配置shard分片

在node1机器上配置shard分片

1

2

3

4

5

6

7

8

9

10

11

[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.75.128:10001,192.168.75.129:10001,192.168.75.130:10001"});

"shardAdded" "shard1_zxl""ok" : 1 }

mongos> db.runCommand({addshard:"shard2_zxl/192.168.75.128:10002,192.168.75.129:10002,192.168.75.130:10002"});

"shardAdded" "shard2_zxl""ok" : 1 }

mongos> db.runCommand({addshard:"shard3_zxl/192.168.75.128:10003,192.168.75.129:10003,192.168.75.130:10003"});

"shardAdded" "shard3_zxl""ok" : 1 }

1

2

3

4

#db.runCommand({addshard:"shard1_zxl/192.168.33.131:10001,192.168.33.132:10001,192.168.33.136:10001"});

#db.runCommand({addshard:"shard2_zxl/192.168.33.131:10002,192.168.33.132:10002,192.168.33.136:10002"});

#db.runCommand({addshard:"shard3_zxl/192.168.33.131:10003,192.168.33.132:10003,192.168.33.136:10003"});

注:根据自己的实际情况,修改上面内容,快速执行。。你懂得。。

查看shard信息

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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.75.128:10001,192.168.75.129:10001" }

{  "_id" "shard2_zxl",  "host" "shard2_zxl/192.168.75.129:10002,192.168.75.130:10002" }

{  "_id" "shard3_zxl",  "host" "shard3_zxl/192.168.75.128:10003,192.168.75.130: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:

查看分片状态

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

mongos> db.runCommand( {listshards : 1 } )

{

"shards" : [

{

"_id" "shard1_zxl",

"host" "shard1_zxl/192.168.75.128:10001,192.168.75.129:10001"

},

{

"_id" "shard2_zxl",

"host" "shard2_zxl/192.168.75.129:10002,192.168.75.130:10002"

},

{

"_id" "shard3_zxl",

"host" "shard3_zxl/192.168.75.128:10003,192.168.75.130:10003"

}

],

"ok" : 1

}

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

1

2

mongos> sh.enableSharding("zxl")

"ok" : 1 }

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

1

2

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

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

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

1

2

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

WriteResult({ "nInserted" : 1 })

可以使用上面mongos> sh.status()命令查看各个shard分片情况,以上就是复制集和shard分片搭建完成,主要还是需要理解rs和shard原理。还是把结果发出来吧,如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

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.75.128:10001,192.168.75.129:10001" }

{  "_id" "shard2_zxl",  "host" "shard2_zxl/192.168.75.129:10002,192.168.75.130:10002" }

{  "_id" "shard3_zxl",  "host" "shard3_zxl/192.168.75.128:10003,192.168.75.130: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)

以上就是mongodb3.2复制集和shard分片搭建就此完成,还是多多看看各个角色是什么概念以及原理性的东东吧。。

mongodb 3.4 集群搭建升级版 五台集群

mongodb 3.4 集群搭建升级版 五台集群

mongodb 3.4 集群搭建升级版 五台集群

纯洁的微笑 2017-09-21 原文

 

最新版mongodb推荐使用yaml语法来做配置,另外一些旧的配置在最新版本中已经不在生效,所以我们在生产实际搭建mongodb集群的时候做了一些改进。如果大家不熟悉什么是分片、副本集、仲裁者的话请先移步查看上一篇文章:mongodb 3.4 集群搭建:分片+副本集

和前一个版本相比,改动点有:

  • 配置文件采用yaml方式来配置
  • 生产中取消了仲裁者的角色,因为仲裁者也不会存储数据,只是起到选举的作用,线上为了保证数据安全,每份数据都会配置两个副本集,也就是每份数据存储了三份。
  • 优化配置,采用五台集群
  • 使用非root账户搭建mongodb集群。

环境准备

系统系统 centos6.9
五台服务器:192.168.0.31/32/33/34/35
安装包: mongodb-linux-x86_64-3.4.6.tgz

服务器规划

服务器31 服务器32 服务器33 服务器34 服务器35
mongos server mongos server config server config server config server
shard1 server shard2 server shard3 server shard4 server shard5 server
shard5 server shard1 server shard2 server shard3 server shard4 server
shard4 server shard5 server shard1 server shard2 server shard3 server

端口分配:

 
  1. mongos:20000
  2. config:21000
  3. shard1:27001
  4. shard2:27002
  5. shard3:27003
  6. shard4:27004
  7. shard5:27005

权限分配:

登录root账户,将安装目录和数据目录权限分配给日常操作(youknow)账户

 
  1. chown -R youknow:youknow /usr/local/
  2. chown -R youknow:youknow /data

mongodb安装

1、下载

下载 mongodb 3.4.6 安装包

 
  1. curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.6.tgz
 
  1. #解压
  2. tar -xzvf mongodb-linux-x86_64-3.4.6.tgz -C /usr/local/
  3. #改名
  4. mv mongodb-linux-x86_64-3.4.6 mongodb

2、创建相关目录

根据服务器的规范,分别在对应的服务器上建立conf、mongos、config、shard1、shard2、shard3、shard4、shard5等目录,因为mongos不存储数据,只需要建立日志文件目录即可。

 
  1. mkdir -p /usr/local/mongodb/conf
  2. mkdir -p /data/mongos/log
  3. mkdir -p /data/config/data
  4. mkdir -p /data/config/log
  5. mkdir -p /data/shard1/data
  6. mkdir -p /data/shard1/log
  7. mkdir -p /data/shard2/data
  8. mkdir -p /data/shard2/log
  9. mkdir -p /data/shard3/data
  10. mkdir -p /data/shard3/log
  11. mkdir -p /data/shard4/data
  12. mkdir -p /data/shard4/log
  13. mkdir -p /data/shard5/data
  14. mkdir -p /data/shard5/log

3、环境变量

为了后续方便操作,配置mongodb的环境变量,需要切到root用户下面

 
  1. vim /etc/profile
  2. # 内容
  3. export MONGODB_HOME=/usr/local/mongodb
  4. export PATH=$MONGODB_HOME/bin:$PATH
  5. # 使立即生效,在安装用户下(youknow)执行
  6. source /etc/profile

查看mongodb版本信息mongod -v 输出版本信息表明配置环境变量成功

集群配置

1、config server配置服务器

在服务器33、34、35上配置以下内容:

添加配置文件:

添加配置文件

 
  1. vi /usr/local/mongodb/conf/config.conf
  2.  
  3. ## content
  4. systemLog:
  5. destination: file
  6. logAppend: true
  7. path: /data/config/log/config.log
  8.  
  9. # Where and how to store data.
  10. storage:
  11. dbPath: /data/config/data
  12. journal:
  13. enabled: true
  14. # how the process runs
  15. processManagement:
  16. fork: true
  17. pidFilePath: /data/config/log/configsrv.pid
  18.  
  19. # network interfaces
  20. net:
  21. port: 21000
  22. bindIp: 192.168.0.33
  23.  
  24. #operationProfiling:
  25. replication:
  26. replSetName: config
  27.  
  28. sharding:
  29. clusterRole: configsvr

启动三台服务器的config server

 
  1. numactl --interleave=all mongod --config /usr/local/mongodb/conf/config.conf

登录任意一台配置服务器,初始化配置副本集

 
  1. #连接
  2. mongo 192.168.0.33:21000
  3. #config变量
  4. config = {
  5. ... _id : "config",
  6. ... members : [
  7. ... {_id : 0, host : "192.168.0.33:21000" },
  8. ... {_id : 1, host : "192.168.0.34:21000" },
  9. ... {_id : 2, host : "192.168.0.35:21000" }
  10. ... ]
  11. ... }
  12.  
  13. #初始化副本集
  14. rs.initiate(config)
  15.  
  16. #查看分区状态
  17. rs.status();

其中,"_id" : "configs"应与配置文件中配置的 replicaction.replSetName 一致,"members" 中的 "host" 为三个节点的ip和port

这样配置服务器就配置好了

2、配置分片、副本集

配置第一个分片副本集

在服务器 31、32、33上面做以下配置

配置文件

 
  1. vi /usr/local/mongodb/conf/shard1.conf
  2.  
  3. #配置文件内容
  4. # where to write logging data.
  5. systemLog:
  6. destination: file
  7. logAppend: true
  8. path: /data/shard1/log/shard1.log
  9.  
  10. # Where and how to store data.
  11. storage:
  12. dbPath: /data/shard1/data
  13. journal:
  14. enabled: true
  15. wiredTiger:
  16. engineConfig:
  17. cacheSizeGB: 20
  18.  
  19. # how the process runs
  20. processManagement:
  21. fork: true
  22. pidFilePath: /data/shard1/log/shard1.pid
  23.  
  24. # network interfaces
  25. net:
  26. port: 27001
  27. bindIp: 192.168.0.33
  28.  
  29. #operationProfiling:
  30. replication:
  31. replSetName: shard1
  32. sharding:
  33. clusterRole: shardsvr

启动三台服务器的shard1 server

 
  1. numactl --interleave=all mongod --config /usr/local/mongodb/conf/shard1.conf

登陆任意一台服务器,初始化副本集

 
  1. mongo 192.168.0.31:27001
  2. #使用admin数据库
  3. use admin
  4. #定义副本集配置
  5. config = {
  6. ... _id : "shard1",
  7. ... members : [
  8. ... {_id : 0, host : "192.168.0.31:27001" },
  9. ... {_id : 1, host : "192.168.0.32:27001" },
  10. ... {_id : 2, host : "192.168.0.33:27001" }
  11. ... ]
  12. ... }
  13.  
  14. #初始化副本集配置
  15. rs.initiate(config);
  16.  
  17. #查看分区状态
  18. rs.status();

配置第二个分片副本集

在服务器32、33、34上面做以下配置

配置文件

 
  1. vi /usr/local/mongodb/conf/shard2.conf
  2.  
  3. #配置文件内容
  4. # where to write logging data.
  5. systemLog:
  6. destination: file
  7. logAppend: true
  8. path: /data/shard2/log/shard2.log
  9.  
  10. # Where and how to store data.
  11. storage:
  12. dbPath: /data/shard2/data
  13. journal:
  14. enabled: true
  15. wiredTiger:
  16. engineConfig:
  17. cacheSizeGB: 20
  18.  
  19. # how the process runs
  20. processManagement:
  21. fork: true
  22. pidFilePath: /data/shard2/log/shard2.pid
  23.  
  24. # network interfaces
  25. net:
  26. port: 27002
  27. bindIp: 192.168.0.33
  28.  
  29. #operationProfiling:
  30. replication:
  31. replSetName: shard2
  32. sharding:
  33. clusterRole: shardsvr

启动三台服务器的shard2 server

 
  1. numactl --interleave=all mongod --config /usr/local/mongodb/conf/shard2.conf

登陆任意一台服务器,初始化副本集

 
  1. mongo 192.168.0.32:27002
  2. #使用admin数据库
  3. use admin
  4. #定义副本集配置
  5. config = {
  6. ... _id : "shard2",
  7. ... members : [
  8. ... {_id : 0, host : "192.168.0.32:27002" },
  9. ... {_id : 1, host : "192.168.0.33:27002" },
  10. ... {_id : 2, host : "192.168.0.34:27002" }
  11. ... ]
  12. ... }
  13.  
  14. #初始化副本集配置
  15. rs.initiate(config);
  16.  
  17. #查看分区状态
  18. rs.status();

配置第三个分片副本集

在服务器33、34、35上面做以下配置

配置文件

 
  1. vi /usr/local/mongodb/conf/shard3.conf
  2.  
  3. #配置文件内容
  4. # where to write logging data.
  5. systemLog:
  6. destination: file
  7. logAppend: true
  8. path: /data/shard3/log/shard3.log
  9.  
  10. # Where and how to store data.
  11. storage:
  12. dbPath: /data/shard3/data
  13. journal:
  14. enabled: true
  15. wiredTiger:
  16. engineConfig:
  17. cacheSizeGB: 20
  18. # how the process runs
  19. processManagement:
  20. fork: true
  21. pidFilePath: /data/shard3/log/shard3.pid
  22.  
  23. # network interfaces
  24. net:
  25. port: 27003
  26. bindIp: 192.168.0.33
  27.  
  28. #operationProfiling:
  29. replication:
  30. replSetName: shard3
  31. sharding:
  32. clusterRole: shardsvr

启动三台服务器的shard3 server

 
  1. numactl --interleave=all mongod --config /usr/local/mongodb/conf/shard3.conf

登陆任意一台服务器,初始化副本集

 
  1. mongo 192.168.0.33:27003
  2. #使用admin数据库
  3. use admin
  4. #定义副本集配置
  5. config = {
  6. ... _id : "shard3",
  7. ... members : [
  8. ... {_id : 0, host : "192.168.0.33:27003" },
  9. ... {_id : 1, host : "192.168.0.34:27003" },
  10. ... {_id : 2, host : "192.168.0.35:27003" }
  11. ... ]
  12. ... }
  13.  
  14. #初始化副本集配置
  15. rs.initiate(config);
  16.  
  17. #查看分区状态
  18. rs.status();

配置第四个分片副本集

在服务器34、35、31上面做以下配置

配置文件

 
  1. vi /usr/local/mongodb/conf/shard4.conf
  2.  
  3. #配置文件内容
  4. # where to write logging data.
  5. systemLog:
  6. destination: file
  7. logAppend: true
  8. path: /data/shard4/log/shard4.log
  9.  
  10. # Where and how to store data.
  11. storage:
  12. dbPath: /data/shard4/data
  13. journal:
  14. enabled: true
  15. wiredTiger:
  16. engineConfig:
  17. cacheSizeGB: 20
  18.  
  19. # how the process runs
  20. processManagement:
  21. fork: true
  22. pidFilePath: /data/shard4/log/shard4.pid
  23.  
  24. # network interfaces
  25. net:
  26. port: 27004
  27. bindIp: 192.168.0.35
  28.  
  29. #operationProfiling:
  30. replication:
  31. replSetName: shard4
  32. sharding:
  33. clusterRole: shardsvr

启动三台服务器的shard4 server

 
  1. numactl --interleave=all mongod --config /usr/local/mongodb/conf/shard4.conf

登陆任意一台服务器,初始化副本集

 
  1. mongo 192.168.0.34:27004
  2. #使用admin数据库
  3. use admin
  4. #定义副本集配置
  5. config = {
  6. ... _id : "shard4",
  7. ... members : [
  8. ... {_id : 0, host : "192.168.0.34:27004" },
  9. ... {_id : 1, host : "192.168.0.35:27004" },
  10. ... {_id : 2, host : "192.168.0.31:27004" }
  11. ... ]
  12. ... }
  13.  
  14. #初始化副本集配置
  15. rs.initiate(config);
  16.  
  17. #查看分区状态
  18. rs.status();

配置第五个分片副本集

在服务器35、31、32上面做以下配置

配置文件

 
  1. vi /usr/local/mongodb/conf/shard5.conf
  2.  
  3. #配置文件内容
  4. # where to write logging data.
  5. systemLog:
  6. destination: file
  7. logAppend: true
  8. path: /data/shard5/log/shard5.log
  9.  
  10. # Where and how to store data.
  11. storage:
  12. dbPath: /data/shard5/data
  13. journal:
  14. enabled: true
  15. wiredTiger:
  16. engineConfig:
  17. cacheSizeGB: 20
  18.  
  19. # how the process runs
  20. processManagement:
  21. fork: true
  22. pidFilePath: /data/shard5/log/shard5.pid
  23.  
  24. # network interfaces
  25. net:
  26. port: 27005
  27. bindIp: 192.168.0.35
  28.  
  29. #operationProfiling:
  30. replication:
  31. replSetName: shard5
  32. sharding:
  33. clusterRole: shardsvr

启动三台服务器的shard5 server

 
  1. numactl --interleave=all mongod --config /usr/local/mongodb/conf/shard5.conf

登陆任意一台服务器,初始化副本集

 
  1. mongo 192.168.0.35:27005
  2. #使用admin数据库
  3. use admin
  4. #定义副本集配置
  5. config = {
  6. ... _id : "shard5",
  7. ... members : [
  8. ... {_id : 0, host : "192.168.0.35:27005" },
  9. ... {_id : 1, host : "192.168.0.31:27005" },
  10. ... {_id : 2, host : "192.168.0.32:27005" }
  11. ... ]
  12. ... }
  13.  
  14. #初始化副本集配置
  15. rs.initiate(config);
  16.  
  17. #查看分区状态
  18. rs.status();

至此,五个分片和副本集搭建完毕

3、配置路由服务器 mongos

以下配置在服务器31、32上执行

注意:先启动配置服务器和分片服务器,后启动路由实例

 
  1. vi /usr/local/mongodb/conf/mongos.conf
  2.  
  3. systemLog:
  4. destination: file
  5. logAppend: true
  6. path: /data/mongos/log/mongos.log
  7. processManagement:
  8. fork: true
  9. # pidFilePath: /usr/local/mongodb/mongos.pid
  10.  
  11. # network interfaces
  12. net:
  13. port: 20000
  14. bindIp: 192.168.0.31
  15. #监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
  16. sharding:
  17. configDB: configs/192.168.0.33:21000,192.168.0.34:21000,192.168.0.35:21000

启动二台服务器的mongos server

 
  1. mongos --config /usr/local/mongodb/conf/mongos.conf

4、启用分片

目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。

登陆任意一台mongos

 
  1. mongo 192.168.0.31:20000
  2. #使用admin数据库
  3. use admin
  4. #串联路由服务器与分配副本集
  5. sh.addShard("shard1/192.168.0.31:27001,192.168.0.32:27001,192.168.0.33:27001")
  6. sh.addShard("shard2/192.168.0.32:27002,192.168.0.33:27002,192.168.0.34:27002")
  7. sh.addShard("shard3/192.168.0.33:27003,192.168.0.34:27003,192.168.0.35:27003")
  8. sh.addShard("shard4/192.168.0.34:27004,192.168.0.35:27004,192.168.0.31:27004")
  9. sh.addShard("shard5/192.168.0.35:27005,192.168.0.31:27005,192.168.0.32:27005")
  10. #查看集群状态
  11. sh.status()

这样mongodb的五台集群搭建就已经完成了,后期如何优化和运营请查看下一篇文章。

错误

rs.initiate报错

执行 rs.initiate(config); 报错:

 
  1. rs.initiate(config);
  2. {
  3. "ok" : 0,
  4. "errmsg" : "No host described in new configuration 1 for replica set shard1 maps to this node",
  5. "code" : 93,
  6. "codeName" : "InvalidReplicaSetConfig"
  7. }

最后发现是自己的一个端口号写错了。

启动mongos报错

启动mongos的时候报错:

 
  1. about to fork child process, waiting until server is ready for connections.
  2. forked process: 1436
  3. ERROR: child process failed, exited with error number 1

这个问题卡了我们半天,找了很多的资料,不是说清理lock文件,就是说清理log文件总无解,最后看到这个网站的提示

ERROR: child process failed, exited with error number 1

去掉了配置文件中 --fork,才将真正的错误日志打印了出来,是我们的配置文件中的路径写错了,本来是log写成了logs

原来:path: /data/logs/mongos.log

改为:path: /data/log/mongos.log

成功

为了方便大家拿取配置文件,我在github上面放置了一份:mongodb-five-cluster-conf

mongodb 4.0.5 集群搭建五台集群

mongodb 4.0.5 集群搭建五台集群

配置文件采用yaml方式来配置
 生产中取消了仲裁者的角色,因为仲裁者也不会存储数据,只是起到选举的作用,线上为了保证数据安全,每份数据都会配置两个副本集,也就是每份数据存储了三份。
 优化配置,采用五台集群
 使用非root账户搭建mongodb集群。
 
环境准备
系统系统 centos7.4
五台服务器:192.168.0.31/32/33/34/35
安装包:

 

服务器规划

 


  端口分配:
mongos:20000
config:21000
shard1:27001
shard2:27002
shard3:27003
shard4:27004
shard5:27005
权限分配:
登录root账户,将安装目录和数据目录权限分配给日常操作(youknow)账户
chown -R youknow:youknow /usr/local/
chown -R youknow:youknow /data
 
mongodb安装
 
1、下载
下载 mongodb  安装包
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.5.tgz
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.5.tgz
#解压tar -xzvf mongodb-linux-x86_64-4.0.5.tgz -C /usr/local/#改名mv mongodb-linux-x86_64-4.0.5 mongodb
 
2、创建相关目录
根据服务器的规范,分别在对应的服务器上建立conf、mongos、config、shard1、shard2、shard3、shard4、shard5等目录,因为mongos不存储数据,只需要建立日志文件目录即可。
mkdir -p /usr/local/mongodb/conf
mkdir -p /data/mongos/log
mkdir -p /data/config/data
mkdir -p /data/config/log
mkdir -p /data/shard1/data
mkdir -p /data/shard1/log
mkdir -p /data/shard2/data
mkdir -p /data/shard2/log
mkdir -p /data/shard3/data
mkdir -p /data/shard3/log
mkdir -p /data/shard4/data
mkdir -p /data/shard4/log
mkdir -p /data/shard5/data
mkdir -p /data/shard5/log
 
3、环境变量
vim /etc/profile
# 内容
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
# 使立即生效,在安装用户下(youknow)执行
source /etc/profile
查看mongodb版本信息 mongod -v 输出版本信息表明配置环境变量成功
 
集群配置

MongoDB Sharding(二) -- 搭建分片集群

MongoDB Sharding(二) -- 搭建分片集群

在上一篇文章中,我们基本了解了分片的概念,本文将着手实践,进行分片集群的搭建

 

首先我们再来了解一下分片集群的架构,分片集群由三部分构成:

  • mongos:查询路由,在客户端程序和分片之间提供接口。本次实验部署2个mongos实例
  • config:配置服务器存储集群的元数据,元数据反映分片集群的内所有数据和组件的状态和组织方式,元数据包含每个分片上的块列表以及定义块的范围。从3.4版本开始,已弃用镜像服务器用作配置服务器(SCCC),config Server必须部署为副本集架构(CSRS)。本次实验配置一个3节点的副本集作为配置服务器
  • shard:每个shard包含集合的一部分数据,从3.6版本开始,每个shard必须部署为副本集(replica set)架构。本次实验部署3个分片存储数据。

 

(一)主机信息

 

(二)配置服务器副本集搭建

配置服务器三个实例的基础规划如下:

member0 192.168.10.80:27017
member1 192.168.10.80:27018
member2 192.168.10.80:27019

其参数规划如下:

 

 接下来,我们一步一步搭建config server的副本集。

STEP1:解压mongodb安装包到/mongo目录

[root@mongosserver mongo]# pwd
/mongo
[root@mongosserver mongo]# ls
bin LICENSE-Community.txt MPL-2 README THIRD-PARTY-NOTICES THIRD-PARTY-NOTICES.gotools

 STEP2:根据上面参数规划,创建数据存放相关路径

# 创建文件路径
mkdir -p /replset/repset1/data
log
mkdir -p /replset/repset2/mkdir -p /replset/repset3/log

[root@mongosserver repset1]# tree /replset/
/replset/
├── repset1
│   ├── data
│   ├── log
│   └── mongodb.conf
├── repset2
│   ├── data
│   └── log
└── repset3
    ├── data
    └── log

 STEP3:为3个实例创建参数文件

实例1的参数文件  /replset/repset1/mongodb.conf  :

systemLog:
   destination: file
   logAppend: true
   path: /replset/repset1/log/mongodb.log

storage:
   dbPath: /replset/repset1/data
   journal:
     enabled: 

processManagement:
   fork: true  # fork and run in background
   pidFilePath: /replset/repset1/mongod.pid  # location of pidfile
   timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
   port: 27017
   bindIp: 0.0.0.0  

# shard
sharding:
  clusterRole: configsvr
  
# repliuca set
replication:
  replSetName: conf
View Code

 实例2的参数文件  /replset/repset2/mongodb.conf :

systemLog:
   destination: file
   logAppend: 
   path: /replset/repset2/log/mongodb.log

storage:
   dbPath: /replset/repset2/data
   journal:
     enabled:  background
   pidFilePath: /replset/repset2/mongod.pid  # location of pidfile
   timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
   port: 27018
   bindIp: 0.0.0.0  

# shard
sharding:
  clusterRole: configsvr
  
# repliuca set
replication:
  replSetName: conf
 
View Code

 实例3的参数文件  /replset/repset3/mongodb.conf :


   path: /replset/repset3/log/mongodb.log

storage:
   dbPath: /replset/repset3/ background
   pidFilePath: /replset/repset3/27019  

# shard
sharding:
  clusterRole: configsvr
  
# repliuca set
replication:
  replSetName: conf
View Code

 STEP4:启动三个mongod实例

mongod -f /replset/repset1/mongodb.conf
mongod -f /replset/repset2/mongodb.conf
mongod -f /replset/repset3/mongodb.conf


# 查看是成功否启动
[root@mongosserver mongo]# netstat -nltp |grep mongod
tcp 0 0.0:27019 0.0:* LISTEN 28009/mongod 
tcp 27017 27928/27018 27970/mongod

 STEP5:进入任意一个实例,初始化配置服务器的副本集

rs.initiate(
  {
    _id: "conf",configsvr:  },{ _id : 1,host : "192.168.10.80:27018" }
    ]
  }
)

  STEP6:[可选] 调整节点优先级,以便于确定主节点

cfg = rs.conf()
cfg.members[0].priority = 3
cfg.members[1].priority = 2
cfg.members[2].priority = 1
rs.reconfig(cfg)

 对于members[n]的定义:n是members数组中的数组位置,数组以0开始,千万不能将其理解为“members[n]._id”的_id值。

查看节点优先级:

conf:PRIMARY> rs.config()

 

(三)分片副本集搭建

分片1副本集成员:
member0 192.168.10.81:27017
member1 192.168.10.81:27018
member2 192.168.10.81:27019

分片2副本集成员:
member0 192.168.10.82:27017
member1 192.168.10.82:27018
member2 192.168.10.82:27019

分片3副本集成员:
member0 192.168.10.83:27017
member1 192.168.10.83:27018
member2 192.168.10.83:27019

 

其参数规划如下:

 这里一共有3个分片,每个分片都是3个节点的副本集,副本集的搭建过程与上面config server副本集搭建过程相似,这里不再重复赘述,唯一不同的是副本集的初始化。shard副本集的初始化与配置副本集初始化过程相比,少了 configsvr: true 的参数配置。

三个shard副本集的初始化:

# shard001
rs.initiate( { _id:
"shard001" } ] } )
# shard002
rs.initiate( { _id: "shard002" } ] } ) # shard003 rs.initiate( { _id: "shard003" } ] } )

 

(四)配置并启动mongos

本次试验在192.168.10.100服务器上启动2个mongos进程,分别使用端口27000和28000。

STEP1:配置mongos实例的参数

端口27000参数配置,特别注意,需要先创建涉及到的路径:


   path: /mongo/log/mongos-27000.log
   
processManagement:
   fork:  background
   pidFilePath: /mongo/mongod-27000.pid  # location of pidfile
   timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
   port: 27000  

sharding:
  configDB: conf/192.168.10.80:27017,192.168.10.80:27018,192.168.10.80:27019

 端口28000参数配置,特别注意,需要先创建涉及到的路径:


   path: /mongo/log/mongos-28000.log
 background
   pidFilePath: /mongo/mongod-28000
# network interfaces
net:
   port: 28000STEP2:启动mongos实例

# 启动mongos实例
[root@mongosserver mongo]# mongos -f /mongo/mongos-27000.conf 
[root@mongosserver mongo]# mongos -f /mongo/mongos-28000.conf 

# 查看实例信息
[root@mongosserver mongo]# netstat -nltp| mongos
tcp 27000 2209/mongos 
tcp 28000 2241/mongos

 

(五)添加分片到集群配置服务器

STEP1:使用mongo连接到mongos

mongo --host 192.168.10.100 --port 
# 或者
mongo --host 28000

 STEP2:添加分片到集群

sh.addShard( "shard001/192.168.10.81:27017,192.168.10.81:27018,192.168.10.81:27019")
shard002/192.168.10.82:27017,192.168.10.82:27018,192.168.10.82:27019shard003/192.168.10.83:27017,192.168.10.83:27018,192.168.10.83:27019)

 STEP3:查看分片信息

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
      "_id" : 1Leversion" : 5)
  }
  shards:
        {  "_id" : "shard001","host" : "shard001/192.168.10.81:27017,192.168.10.81:27019","state" : 1 }
        {  "_id" : "shard002","host" : "shard002/192.168.10.82:27017,192.168.10.82:27019",1)"> }
        {  "_id" : "shard003","host" : "shard003/192.168.10.83:27017,192.168.10.83:27019",1)"> }
  active mongoses:
        "4.2.10" : 2
  autosplit:
        Currently enabled: yes
  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:
        {  "_id" : "config","primary" : "config","partitioned" :  }

mongos> 

 

 

(六)启用分片

(6.1)对数据库启用分片
分片是以集合为单位进行的,在对一个集合进行分片之前,需要先对其数据库启用分片,对数据库启用分片并不会重新分发数据,只是说明该数据库上的集合可以进行分片操作。

sh.enableSharding("lijiamandb");

 

(6.2)对集合启用分片

如果集合已经存在数据,必须手动创建在分片键上创建索引,然后再对集合进行分片,如果集合为空,MongoDB会在分片的时候自动在分片键上创建索引。
mongodb提供了2种策略来对集合进行分片:

  • 哈希(hash)分片,对单列使用hash索引作为分片键
sh.shardCollection("<database>.<collection>",{shard key field : "hashed"}) 
  • 范围(range)分片,可以使用多个字段作为分片键,并将数据划分为由分片键确定的连续范围
sh.shardCollection("<database>.<collection>",{<shard key field>:1,...} )

 

例子:对集合user进行hash分片

// 连接到mongos,进入lijiamandb数据库,对新集合users插入10万条数据
use lijiamandb

for (i=1;i<100000;i++){
  db.user.insert({
  "id" : i,"name" : "name"+i,"age" : Math.floor(Math.random()*120),"created" : new Date()
  });
}


 使用mongostat可以看到,所有数据都写入到了主节点(shard2),每个数据库的主节点可能不同,可以使用sh.status()查看。
[root@mongosserver ~]# mongostat --port 27000 5 --discover
           host insert query update delete getmore command dirty used flushes mapped vsize   res faults qrw arw net_in net_out conn set repl                time
localhost:27000    352    *0     *0     *0       0   704|0                  0     0B  356M 32.0M      0 0|0 0|0   224k    140k   10      RTR Jan 15 10:52:32.046

               host insert query update  getmore command dirty  used flushes mapped vsize   res faults qrw arw net_in net_out conn      set repl                time
192.168.10.81:27017     *0    *0     *0     *0       0     2|0  0.3%  0.8%       0        1.90G  133M    n/a 0|0 1|0   417b   9.67k   23 shard001  SEC Jan 15 10:52:32.061
192.168.10.81:27018     *0    *0     *0     *0       0     3|0  0.3%  0.8%       1        1.93G  132M    n/a 0|0 1|0  1.39k   11.0k   28 shard001  PRI Jan 15 10:52:32.067
192.168.10.81:27019     *0    *0     *0     *0       0     2|0  0.3%  0.8%       0        1.95G  148M    n/a 0|0 1|0   942b   10.2k   26 shard001  SEC Jan 15 10:52:32.070
192.168.10.82:27017    352    *0     *0     *0     407  1192|0  2.5% 11.7%       1        1.99G  180M    n/a 0|0 1|0  1.52m   1.15m   29 shard002  PRI Jan 15 10:52:32.075
192.168.10.82:27018   *352    *0     *0     *0     409   441|0  4.5%  8.9%       0        1.96G  163M    n/a 0|0 1|0   566k    650k   25 shard002  SEC Jan 15 10:52:32.085
192.168.10.82:27019   *352    *0     *0     *0       0     2|0  4.4%  9.7%       0        1.92G  168M    n/a 0|0 1|0   406b   9.51k   24 shard002  SEC Jan 15 10:52:32.093
192.168.10.83:27017     *0    *0     *0     *0       0     1|0  0.2%  0.6%       1        1.89G  130M    n/a 0|0 1|0   342b   9.17k   22 shard003  SEC Jan 15 10:52:32.099
192.168.10.83:27018     *0    *0     *0     *0       0     2|0  0.2%  0.6%       0        1.95G  139M    n/a 0|0 1|0   877b   9.92k   28 shard003  PRI Jan 15 10:52:32.107
192.168.10.83:27019     *0    *0     *0     *0       0     1|0  0.2%  0.6%       0        1.90G  133M    n/a 0|0 1|0   342b   9.17k   21 shard003  SEC Jan 15 10:52:32.113
    localhost:27000    365    *0     *0     *0       0   731|0                   0     0B  356M 32.0M      0 0|0 0|0   233k    145k   10           RTR Jan 15 10:52:37.047



 使用分片键id创建hash分片,因为id上没有hash索引,会报错
sh.shardCollection("lijiamandb.user",{"id":"hashed"})
/* 1 */
{
    "ok" : 0.0 : {
        "clusterTime" : Timestamp(1610679762,"signature" : {
            "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=","$type" : "00")
        }
    }
}

 需要手动创建hash索引
db.user.ensureIndex()

 查看索引
/*
[
    {
        "v" : 2 : {
            "_id" : 1
        },"name" : "_id_"
    },{
        "v" : 2 : {
            "id" : "hashed"
    }
]

# 最后再重新分片即可
sh.shardCollection("lijiamandb".user,{"id":"hashed"})

 

到这里,我们分片集群环境已经搭建完成,接下来我们将会学习分片键的选择机制。

 

【完】

 

 

相关文档合集:

1. MongoDB Sharding(一) -- 分片的概念
2. MongoDB Sharding(二) -- 搭建分片集群
@L_301_3@
4. MongoDB Sharding(四) -- 分片集群的维护管理

总结

以上是小编为你收集整理的MongoDB Sharding(二) -- 搭建分片集群全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

mongodb shard集群扩容和收缩

mongodb shard集群扩容和收缩

集群搭建参考http://my.oschina.net/u/1791060/blog/652600

在这个集群的基础上再加个shard-4 mongodb-shard-4:

192.168.10.91:10003

192.168.10.92:10003

192.168.10.93:10003

###shard集群水平扩展 ####在上述三台服务器上启动shard4的mongodb实例 # mkdir -p /data/mongodb/shard4

# mongod --shardsvr --replSet shard4 --port 10003 --dbpath /data/mongodb/shard4 --oplogSize 100 --logpath /data/mongodb/shard4/shard4.log --logappend --fork

####建立shard关系

# mongo --port 10003
MongoDB shell version: 2.4.14
connecting to: 127.0.0.1:10003/test
> config={_id:''shard4'',members:[{_id:0,host:''192.168.10.91:10003''},{_id:1,host:''192.168.10.92:10003''},{_id:2,host:''192.168.10.93:100023''}]}
{
        "_id" : "shard4",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "192.168.10.91:10003"
                },
                {
                        "_id" : 1,
                        "host" : "192.168.10.92:10003"
                },
                {
                        "_id" : 2,
                        "host" : "192.168.10.93:10003"
                }
        ]
}
> rs.initiate(config)
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
> 
shard4:PRIMARY> 

####mongos上添加shard4

]# mongo --port 30000
MongoDB shell version: 2.4.14
connecting to: 127.0.0.1:30000/test
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:''shard4/192.168.10.91:10003,192.168.10.92:10003,192.168.10.93:10003''})
{ "shardAdded" : "shard4", "ok" : 1 }

####查看集群状态

  • 查看集群状态
mongos> printShardingStatus()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "version" : 3,
        "minCompatibleVersion" : 3,
        "currentVersion" : 4,
        "clusterId" : ObjectId("570394b40d1b67f7f38c434a")
}
  shards:
        {  "_id" : "shard1",  "host" : "shard1/192.168.10.91:10000,192.168.10.92:10000,192.168.10.93:10000" }
        {  "_id" : "shard2",  "host" : "shard2/192.168.10.91:10001,192.168.10.92:10001,192.168.10.93:10001" }
        {  "_id" : "shard3",  "host" : "shard3/192.168.10.91:10002,192.168.10.92:10002,192.168.10.93:10002" }
        {  "_id" : "shard4",  "host" : "shard4/192.168.10.91:10003,192.168.10.92:10003,192.168.10.93:10003" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "test",  "partitioned" : false,  "primary" : "shard3" }
        {  "_id" : "testdb",  "partitioned" : true,  "primary" : "shard1" }
                testdb.table1
                        shard key: { "_id" : 1 }
                        chunks:
                                shard4  1
                                shard1  1
                                shard2  2
                                shard3  2
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("57039cd208bca3c16785e081") } on : shard4 Timestamp(5, 0) 
                        { "_id" : ObjectId("57039cd208bca3c16785e081") } -->> { "_id" : ObjectId("57039d9708bca3c167860949") } on : shard1 Timestamp(5, 1) 
                        { "_id" : ObjectId("57039d9708bca3c167860949") } -->> { "_id" : ObjectId("57039d9e08bca3c167865f9e") } on : shard2 Timestamp(3, 1) 
                        { "_id" : ObjectId("57039d9e08bca3c167865f9e") } -->> { "_id" : ObjectId("57039da408bca3c16786aadb") } on : shard2 Timestamp(4, 0) 
                        { "_id" : ObjectId("57039da408bca3c16786aadb") } -->> { "_id" : ObjectId("57039da508bca3c16786f705") } on : shard3 Timestamp(4, 1) 
                        { "_id" : ObjectId("57039da508bca3c16786f705") } -->> { "_id" : { "$maxKey" : 1 } } on : shard3 Timestamp(3, 5) 

###shard集群收缩 ####缩减shard1

  • mongos上剔除shard1
mongos> db.runCommand({removeshard:"shard1/192.168.10.91:10000,192.168.10.92:10000,192.168.10.93:10000"})
{
        "msg" : "draining started successfully",
        "state" : "started",
        "shard" : "shard1",
        "note" : "you need to drop or movePrimary these databases",
        "dbsToMove" : [
                "testdb"
        ],
        "ok" : 1
}

  • 查看shard1的状态,显示draining,正在移除
mongos> printShardingStatus()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "version" : 3,
        "minCompatibleVersion" : 3,
        "currentVersion" : 4,
        "clusterId" : ObjectId("570394b40d1b67f7f38c434a")
}
  shards:
        {  "_id" : "shard1",  "draining" : true,  "host" : "shard1/192.168.10.91:10000,192.168.10.92:10000,192.168.10.93:10000" }
        {  "_id" : "shard2",  "host" : "shard2/192.168.10.91:10001,192.168.10.92:10001,192.168.10.93:10001" }
        {  "_id" : "shard3",  "host" : "shard3/192.168.10.91:10002,192.168.10.92:10002,192.168.10.93:10002" }
        {  "_id" : "shard4",  "host" : "shard4/192.168.10.91:10003,192.168.10.92:10003,192.168.10.93:10003" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "test",  "partitioned" : false,  "primary" : "shard3" }
        {  "_id" : "testdb",  "partitioned" : true,  "primary" : "shard1" }
                testdb.table1
                        shard key: { "_id" : 1 }
                        chunks:
                                shard4  2
                                shard2  2
                                shard3  2
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("57039cd208bca3c16785e081") } on : shard4 Timestamp(5, 0) 
                        { "_id" : ObjectId("57039cd208bca3c16785e081") } -->> { "_id" : ObjectId("57039d9708bca3c167860949") } on : shard4 Timestamp(6, 0) 
                        { "_id" : ObjectId("57039d9708bca3c167860949") } -->> { "_id" : ObjectId("57039d9e08bca3c167865f9e") } on : shard2 Timestamp(3, 1) 
                        { "_id" : ObjectId("57039d9e08bca3c167865f9e") } -->> { "_id" : ObjectId("57039da408bca3c16786aadb") } on : shard2 Timestamp(4, 0) 
                        { "_id" : ObjectId("57039da408bca3c16786aadb") } -->> { "_id" : ObjectId("57039da508bca3c16786f705") } on : shard3 Timestamp(4, 1) 
                        { "_id" : ObjectId("57039da508bca3c16786f705") } -->> { "_id" : { "$maxKey" : 1 } } on : shard3 Timestamp(3, 5) 
  • 再次执行removed可查看进度,chunks为0即为完成,但是dbs需要执行movePrimary
mongos> db.runCommand({removeshard:"shard1/192.168.10.91:10000,192.168.10.92:10000,192.168.10.93:10000"})
{
        "msg" : "draining ongoing",
        "state" : "ongoing",
        "remaining" : {
                "chunks" : NumberLong(0),
                "dbs" : NumberLong(1)
        },
        "note" : "you need to drop or movePrimary these databases",
        "dbsToMove" : [
                "testdb"
        ],
        "ok" : 1
}
  • 移动数据库 #可查看config.databases发现原来primary在shard1上,现在挪至shard2
mongos> db.runCommand({movePrimary:''testdb'',to:''shard2''})
{
        "primary " : "shard2:shard2/192.168.10.91:10001,192.168.10.92:10001,192.168.10.93:10001",
        "ok" : 1
}
mongos> db.runCommand({removeshard:''shard1''})
{
        "msg" : "removeshard completed successfully",
        "state" : "completed",
        "shard" : "shard1",
        "ok" : 1
}
  • 再次查看分片信息发现shard已移除
mongos> printShardingStatus()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "version" : 3,
        "minCompatibleVersion" : 3,
        "currentVersion" : 4,
        "clusterId" : ObjectId("570394b40d1b67f7f38c434a")
}
  shards:
        {  "_id" : "shard2",  "host" : "shard2/192.168.10.91:10001,192.168.10.92:10001,192.168.10.93:10001" }
        {  "_id" : "shard3",  "host" : "shard3/192.168.10.91:10002,192.168.10.92:10002,192.168.10.93:10002" }
        {  "_id" : "shard4",  "host" : "shard4/192.168.10.91:10003,192.168.10.92:10003,192.168.10.93:10003" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "test",  "partitioned" : false,  "primary" : "shard3" }
        {  "_id" : "testdb",  "partitioned" : true,  "primary" : "shard2" }
                testdb.table1
                        shard key: { "_id" : 1 }
                        chunks:
                                shard4  2
                                shard2  2
                                shard3  2
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("57039cd208bca3c16785e081") } on : shard4 Timestamp(5, 0) 
                        { "_id" : ObjectId("57039cd208bca3c16785e081") } -->> { "_id" : ObjectId("57039d9708bca3c167860949") } on : shard4 Timestamp(6, 0) 
                        { "_id" : ObjectId("57039d9708bca3c167860949") } -->> { "_id" : ObjectId("57039d9e08bca3c167865f9e") } on : shard2 Timestamp(3, 1) 
                        { "_id" : ObjectId("57039d9e08bca3c167865f9e") } -->> { "_id" : ObjectId("57039da408bca3c16786aadb") } on : shard2 Timestamp(4, 0) 
                        { "_id" : ObjectId("57039da408bca3c16786aadb") } -->> { "_id" : ObjectId("57039da508bca3c16786f705") } on : shard3 Timestamp(4, 1) 
                        { "_id" : ObjectId("57039da508bca3c16786f705") } -->> { "_id" : { "$maxKey" : 1 } } on : shard3 Timestamp(3, 5) 
        {  "_id" : "testdb1",  "partitioned" : false,  "primary" : "shard2" }

关于mongodb3.2复制集和shard集群搭建mongodb复制集的作用的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于mongodb 3.4 集群搭建升级版 五台集群、mongodb 4.0.5 集群搭建五台集群、MongoDB Sharding(二) -- 搭建分片集群、mongodb shard集群扩容和收缩的相关知识,请在本站寻找。

本文标签: