您的位置:首页 > 数据库 > Mongodb

部署MongoDB分片集群步骤

2016-12-10 16:46 836 查看

准备工作

1.开放27017端口、开放防火墙,Sharded Cluster中所有成员之间必须能互相访问

2.在部署集群的各机器上新建存放数据和日志的目录,如

数据目录:/data/db
 /data/configdb
 /data/arbiterdb1
 /data/arbiterdb2
log目录: /data/log/mongodb.log

3.在部署集群的各机器上设置好MongoDB路径相关的环境变量,MongoDB 的可执行文件位于 bin 目录下,所以可以将其添加到 PATH 路径中,修改/etc/profile文件,在该文件最后添加:

export PATH=<mongodb-install-directory>/bin:$PATH
export PATH=/usr/local/mongodb/bin:$PATH

<mongodb-install-directory> 为你 MongoDB 的安装路径。如本文的 /usr/local/mongodb 

保存退出后,执行以下命令使更改立即生效:

source /etc/profile

4.创建keyFile,在linux中使用如下命令创建keyFile文件

openssl rand -base64 755 > <path-to-keyfile>
chmod 400 <path-to-keyfile>

将keyFile文件分别复制到所有部署MongoDB分片集群成员的物理机上

一、部署Config Server

必要条件:

1、The replica set config servers must run the WiredTiger storage engine.

defaults to wiredTiger

2、The following restrictions apply to a replica set configuration when used for config servers:

Must have zero arbiters.

Must have no delayed members.

Must build indexes (i.e. no member should have buildIndexes setting set to false).

部署一个3成员replica set作为Config Server的步骤:

1、使用如下命令启动所有的Config Server;

mongod --configsvr --replSet configReplSet --port 27019 --dbpath /data/configdb

或使用如下配置文件方式启动

mongod -f /usr/local/mongodb/bin/configsvr.conf

2、通过mongo shell连接到其中一台config server并运行rs.initiate()来初始化replica set

rs.initiate( {

   _id: "configReplSet",

   configsvr: true,

   members: [

      { _id: 0, host: "15.62.32.106:27019" },

      { _id: 1, host: "15.62.32.107:27019" },

      { _id: 2, host: "15.62.32.108:27019" }

   ]

} )

至此,Config Server部署完成。

二、部署Router

必要条件:

1、所有的config server必须处于运行状态并且可以访问

2、设置防火墙,开放所有config server的27019端口

部署Router的步骤:

1、采用如下命令启动mongos实例;

mongos --configdb configReplSet/<cfgsvr1:27019>,<cfgsvr2:27019>,<cfgsvr3:27019> --port 27017

mongos -f /usr/local/mongodb/bin/mongos.conf

三、添加Shards到Cluster

必要条件:

1、所有的config server和Router都处于运行状态并且可以访问

2、设置防火墙,开放mongos的27017端口

3、所有的分片都已部署好,如

mongod -f /usr/local/mongodb/bin/arbiter1.conf

mongod -f /usr/local/mongodb/bin/rs1_member.conf

对于NUMA硬件服务器,使用如下命令进行启动:

numactl --interleave=all mongod -f /usr/local/mongodb/bin/rs1_member.conf

部署shard时的配置:

rs.initiate( {

   _id: "rs1",

   members: [

      { _id: 0, host: "15.62.32.112:27018", priority: 100 },

      { _id: 1, host: "15.62.32.111:27018", priority: 1 },

      { _id: 2, host: "15.62.32.109:30001", arbiterOnly: true}

   ]

} )

mongod -f /usr/local/mongodb/bin/arbiter2.conf
mongod -f /usr/local/mongodb/bin/rs2_member.conf

rs.initiate( {

   _id: "rs2",

   members: [

      { _id: 0, host: "15.62.32.122:27018", priority: 100 },

      { _id: 1, host: "15.62.32.110:27018", priority: 1 },

      { _id: 2, host: "15.62.32.109:30002", arbiterOnly: true}

   ]

} )

mongod -f /usr/local/mongodb/bin/arbiter3.conf

mongod -f /usr/local/mongodb/bin/rs3_member.conf

rs.initiate( {

   _id: "rs3",

   members: [

      { _id: 0, host: "15.62.32.121:27018", priority: 100 },

      { _id: 1, host: "15.62.32.120:27018", priority: 1 },

      { _id: 2, host: "15.62.32.109:30003", arbiterOnly: true}

   ]

} )

添加Shards到Cluster的步骤:

1、使用如下命令从一个mongo shell连接到mongos实例

mongo localhost:27017 -u "clusterAdmin" -p "123" --authenticationDatabase "admin"

2、使用sh.addShard()方法将各个shard添加到Cluster

如有一个shard,其replica set的名字为rs1,rs1的其中一个成员运行在主机mongodb0.example.net的27018端口,

使用如下命令将该shard添加到Cluster:

sh.addShard( "rs1/mongodb0.example.net:27018" )

sh.addShard( "rs1/15.62.32.112:27018" )

sh.addShard( "rs2/15.62.32.122:27018" )

sh.addShard("rs3/15.62.32.121:27018")

四、创建各分片的管理员用户

如分片rs1

user:userAdmin

pwd:123

创建local用户管理员用户(shard-local user administrator)的步骤:

1、登陆到副本集的primary,执行如下命令:

use admin

db.createUser(

  {

    user: "userAdmin",

    pwd: "123",

    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]

  }

)

2、使用如下命令使创建的用户生效

db.auth("userAdmin","123")

创建local集群管理员用户(shard-local cluster administrator)的步骤:

如分片rs1

user:clusterAdmin

pwd:123

1、登陆到副本集的primary,执行如下命令:

use admin

db.createUser(

  {

    user: "clusterAdmin",

    pwd: "123",

    roles: [ { role: "clusterAdmin", db: "admin" } ]

  }

)

2、使用如下命令使创建的用户生效

db.auth("clusterAdmin","123")

五、创建整个集群的管理员用户

创建集群管理员用户方法同上,唯一的不同点是需要通过mongo shell连接到mongos,在mongos中创建

六、开启数据库分片

必要条件:

1、所有的config server、Router和shards都处于运行状态并且可以访问

2、对所有的数据库中要分片的集合设置索引

使能数据库分片的步骤:

1、使用如下命令从一个mongo shell连接到mongos实例;

mongo localhost:27017 -u "clusterAdmin" -p "123" --authenticationDatabase "admin"

2、使用sh.enableSharding()方法对各个需要分片的数据库进行sharding使能;

sh.enableSharding("<database>")

七、开启集合分片

必要条件:

1、所有的config server、Router和shards都处于运行状态并且可以访问

2、对所有的数据库中要分片的集合设置索引

3、集合所在的数据库已经开启了分片使能

使能数据库分片的步骤:

1、使用如下命令从一个mongo shell连接到mongos实例

mongo --host <hostname of machine running mongos> --port 27017

2、使用sh.shardCollection()方法对各个需要分片的集合进行分片

sh.shardCollection("<database>.<collection>", shard-key-pattern)

附部署集群配置文件

配置服务器节点配置文件

security:

   keyFile: /usr/local/mongodb/authentication/keyFile 

sharding:

   clusterRole: configsvr

replication:

   replSetName: configReplSet

net:

   port: 27019

storage:

   dbPath: /data/configdb

systemLog:

   path: /data/log/configsvr.log

   destination: file

   logAppend: true

processManagement:

   fork: true

仲裁节点配置文件

security:

   keyFile: /usr/local/mongodb/authentication/keyFile 

sharding:

   clusterRole: shardsvr

replication:

   replSetName: rs1

net:

   port: 30001

storage:

   dbPath: /data/arbiterdb1

   journal:

      enabled: false

systemLog:

   path: /data/log/arbiterdb1.log

   destination: file

   logAppend: true

processManagement:

   fork: true

常规节点配置文件

security:

   keyFile: /usr/local/mongodb/authentication/keyFile 

sharding:

   clusterRole: shardsvr

replication:

   replSetName: rs1

net:

   port: 27018

storage:

   dbPath: /data/db

systemLog:

   path: /data/log/mongodb.log

   destination: file

   logAppend: true

processManagement:

   fork: true

延迟节点配置文件

security:

   keyFile: /usr/local/mongodb/authentication/keyFile 

sharding:

   clusterRole: shardsvr

replication:

   replSetName: rs1

net:

   port: 27021

storage:

   dbPath: /data/db_delay_rs1

systemLog:

   path: /data/log_delay_rs1/mongodb.log

   destination: file

   logAppend: true

processManagement:

   fork: true

mongos节点配置文件

sharding:

   autoSplit: true

   configDB: configReplSet/15.62.32.106:27019,15.62.32.107:27019,15.62.32.108:27019

   chunkSize: 96

net:

   port: 27017

systemLog:

   path: /data/log_mongos/mongos.log

   destination: file

   logAppend: true

processManagement:

   fork: true
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息