部署一个集群¶
On this page
- 如果你使用”localhost”或者 127.0.0.1 在任意一处地方作为主机标识,比如使用 addShard 命令时的 host 参数或者 --configdb <mongos --configdb>`作为启动参数,你必须保证在所有地方使用其中一个,如果混用了"localhost"和 ``127.0.0.1` ,MongoDB会报错.
- 启动三台配置服务器,每台都通过一下命令启动:
- Deploy Sharded Cluster
- Shard a Collection
如果你使用”localhost”或者 127.0.0.1 在任意一处地方作为主机标识,比如使用 addShard 命令时的 host 参数或者 --configdb <mongos --configdb>`作为启动参数,你必须保证在所有地方使用其中一个,如果混用了"localhost"和 ``127.0.0.1` ,MongoDB会报错.¶
集群中任意一个节点都必须能够与 所有 节点进行连通,包括所有的分片和所有的配置服务器.确保网络和安全系统,防火墙允许节点之间的连通.
为三个配置服务器创建数据目录,默认情况下,配置服务器将数据文件存储在 /data/configdb 目录下.你也可以自己指定不同的位置用来存储数据文件.通过简单的命令创建数据目录:
启动三台配置服务器,每台都通过一下命令启动:¶
CloudManager and OpsManager¶
初始化 sharded cluster 时所有配置服务器必须正常运行并且可以访问.
See Deploy a Sharded Cluster in the Cloud Manager manual or in the Ops Manager manual.
Operating System¶
This tutorial uses the mongod and mongos programs. Windows users should use the mongod.exe and mongos.exe programs instead.
mongos 实例是轻量服务,并且不需要数据目录,你可以将 mongos 运行在已经部署了其他服务的系统中,比如应用服务器或者 运行了 mongod 的机器上.:program:mongos 默认运行在 27017 端口上.¶
This tutorial does not include the required steps for configuring Internal Authentication or Role-Based Access Control. See Deploy Sharded Cluster with Keyfile Access Control for a tutorial on deploying a sharded cluster with a keyfile.
使用以下语法启动 mongos 实例:
示例:使用以下配置服务器,在默认端口上启动 mongos :
For details on using x.509 for client authentication, see Use x.509 Certificates to Authenticate Clients.
注解
Enabling internal authentication also enables Role-Based Access Control.
Deploy Sharded Cluster¶
一个 shard 可以是一个单独的:program:mongod 或者一个 replica set.在生产环境中,每个分片都应该是一个复制集.参见 部署复制集 将每个分片部署为复制集.¶
示例:如果 mongos 部署在 mongos0.example.net``的 ``27017 端口上,使用以下命令进行连接:
Start each member of the config server replica set.¶
Start each mongod in the config server replica set.
You can specify the mongod settings either via a configuration file or the command line.
Configuration File
If using a configuration file, set sharding.clusterRole to configsvr, and replication.replSetName to the desired name of the config server replica set.
sharding:
clusterRole: configsvr
replication:
replSetName: <setname>
Include additional settings as appropriate to your deployment. For more information on the configuration file, see configuration options.
Start the mongod specifying the --config option and the path to the configuration file.
mongod --config <path-to-config-file>
Command Line
If using the command line parameters, start the mongod with the --configsvr, and --replSet parameters.
mongod --configsvr --replSet <setname> --dbpath <path>
Include additional settings as appropriate to your deployment. For more information on startup parameters, see the mongod reference page.
The rs.initiate() method initiates the replica set and can take an optional replica set configuration document. In the replica set configuration document, include:
- The _id. The _id must match the --replSet parameter passed to the mongod.
- The members field. The members field is an array and requires a document per each member of the replica set.
- The configsvr field. The configsvr field must be set to true for the config server replica set.
See Replica Set Configuration for more information on replica set configuration documents.
Initiate the replica set using the rs.initiate() method and a configuration document:
rs.initiate(
{
_id: "<replSetName>",
configsvr: true,
members: [
{ _id : 0, host : "cfg1.example.net:27017" },
{ _id : 1, host : "cfg2.example.net:27017" },
{ _id : 2, host : "cfg3.example.net:27017" }
]
}
)
正如下面的示例,使用 sh.addShard() 在集群中添加分片.每次使用 sh.addShard() 添加一个分片.如果分片是复制集,需要指定复制集的名字与一个成员名字.在生产环境中,所有分片都应该是复制集.
Create the Shard Replica Sets¶
以下是使用 sh.addShard() 添加分片的例子:
Start each member of the shard replica set.¶
Start each mongod in the replica set using either a configuration file or the command line.
Configuration File
If using a configuration file, set the replication.replSetName to the desired name of the replica set, and the sharding.clusterRole option to shardsvr.
sharding:
clusterRole: shardsvr
replication:
replSetName: <replSetName>
Include any other options as appropriate for your deployment. See 配置文件选项 for settings available.
Start the mongod specifying the --config option and the path to the configuration file.
mongod --config <path-to-config-file>
Command Line
If using the command line option, when starting the component, specify the replSet, and --shardsvr parameters, as in the following example:
mongod --shardsvr --replSet <replSetname>
Include any other options as appropriate for your deployment.
For more information on startup parameters, see the mongod reference page.
Include additional settings as appropriate to your deployment.
Initiate the replica set.¶
The rs.initiate() method initiates the replica set and can take an optional replica set configuration document.
In the replica set configuration document, include:
- The _id field. The _id must match the --replSet parameter passed to the mongod.
- The members field. The members field is an array and requires a document per each member of the replica set.
See Replica Set Configuration for more information on replica set configuration documents.
The following example initates a three member replica set.
rs.initiate(
{
_id : <replicaSetName>,
members: [
{ _id : 0, host : "s1-mongo1.example.net:27017" },
{ _id : 1, host : "s1-mongo2.example.net:27017" },
{ _id : 2, host : "s1-mongo3.example.net:27017" }
]
}
)
rs.initiate() triggers an election and elects one of the members to be the primary.
Connect to the primary before continuing. Use rs.status() to locate the primary member.
假设一个分片使用了复制集,复制集名字为 rs1 ,有一个运行在 mongodb0.example.net 且端口为 27017 的成员,使用以下命令添加这个分片:¶
Connect a mongos to the cluster¶
Start a mongos specifying using either a configuration file or a command line parameter.
Configuration File
If using a configuration file, set the sharding.configDB to the config server replica set name and at least one member of the replica set in <replSetName>/<host:port> format.
sharding:
configDB: <configReplSetName>/cfg1.example.net:27017,cfg2.example.net:27017,...
Start the mongos specifying the --config option and the path to the configuration file.
mongos --config <path-to-config>
For more information on the configuration file, see configuration options.
Command Line
If using command line parameters start the mongos and specify the --configdb parameter.
mongos --configdb <configReplSetName>/cfg1.example.net:27017,cfg2.example.net:27017,...
Include any other options as appropriate for your deployment.
添加运行在 mongodb0.example.net 端口为 27017 的单机 mongod 分片,需要执行以下命令:¶
将 数据块 迁移到新的分片需要花费一些时间.
The following operation adds a single shard replica set to the cluster:
sh.addShard( "<replSetName>/s1-mongo1.example.net:27017")
在对集合进行分片之前,必须开启数据库的分片.对数据库开启分片不会导致数据的重新分配,但这是对这个数据库中集合进行分片的前提.
sh.addShard( "s1-mongo1.example.net:27017")
一旦为数据库开启了分片,MongoDB就会为这个数据库指定一个 primary shard,所有未分片的数据都会存储在这个分片上.
使用 sh.enableSharding() 需要指定要开启分片的数据库的名字,语法如下:¶
你也可以使用 enableSharding 命令对数据库开启分片,语法如下:
首先选择一个 shard key ,所选择的片键会影响集群的效率.参见 通常,一个经过计算的片键会有一定的”随机性”,比如一个包含了其他字段加密哈希(例如 MD5或者SHA1)的片键,会使集群具有较好的写扩展性能.不过,随机的片键通常不会提供 查询隔离 的特性,而查询隔离同样是片键一个很重要的特性. 获得注意事项.
如果集合中已经包含有数据,需要使用 ensureIndex() 在片键上创建索引.如果集合是空的,MongoDB会在 sh.shardCollection() 过程中自动创建索引.
将 <database>.<collection> 字符串换成你数据库的ns,由数据库的全名,一个点(即 . ),和集合的全名组成, shard-key-pattern 换成你的片键,名字为 创建索引 时指定的名字.
sh.enableSharding("<database>")
Shard a Collection¶
This section contains an overall description of the sharding process.
records 数据库中的 people 集合使用 { "zipcode": 1, "name": 1 } 片键开启分片.
这个集合使用 zipcode 字段重新分配数据.如果很多文档都有相同的 zipcode 值, chunk 会按照 name 的值进行 分裂.
你也可以使用 enableSharding 命令对数据库开启分片,语法如下:
people 数据库中的 addresses 集合使用片键 { "state": 1, "_id": 1 }.
这个片键使用 state 字段重新分配数据.如果很多文档都有相同的 state 值, chunk 会按照 _id 的值进行 分裂.
assets 数据库中的 chairs 集合使用 { "type": 1, "_id": 1 } 做片键.
这个片键使用 type 字段重新分配数据.如果很多文档都有相同的 type 值, chunk 会按照 _id 的值进行 分裂.
events 数据库中的 alerts 集合使用 { "_id": "hashed" } 做片键.
sh.shardCollection("<database>.<collection>", { <key> : <direction> } )