时间:2021-07-01 10:21:17 帮助过:2人阅读
Options:
...
Sharding options:
--configsvr declare this is a config db of a
cluster; default port 27019; default
dir /data/configdb
--configsvrMode arg Controls what config server protocol is
in use. When set to "sccc" keeps server
in legacy SyncClusterConnection mode
even when the service is running as a
replSet
--shardsvr declare this is a shard db of a
cluster; default port 27018
其中说的很明白了,如果要将机器配置为configserver只要在配置文件中添加上configsvr选项,下面是我们的配置文件:
启动mongod:[root@mongodb1 db]# cat /etc/mongod.conf
port = 27017
dbpath = /data/db
logpath = /data/log/config.log
logappend = true
fork = true
configsvr = true
#replSet = configreplset
这样config server就配置完成了,还是很简单的,如果要配置成复制集(一般是三节点),还需要在上面的再配置复制集的信息:[root@mongodb1 db]# mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3767
child process started successfully, parent exiting
这里就不详细描述复制集了.rs.initiate( {
_id: "configReplSet",
configsvr: true,
members: [
{ _id: 0, host: "<host1>:<port1>" },
{ _id: 1, host: "<host2>:<port2>" },
{ _id: 2, host: "<host3>:<port3>" }
]
} )
[root@mongodb2 ~]# mongos -h
Options:
Sharding options:
--configdb arg Connection string for communicating with config
servers. Acceptable forms:
CSRS: <config replset name>/<host1:port>,<host2:
port>,[...]
启动mongos服务,注意这里的mongos端口是27018,因为这台机器等会还要启动一个mongod,我设置mongod的端口为27017:[root@mongodb2 ~]# cat /etc/mongos.conf
logpath = /data/log/mongos.log
logappend = true
configdb = mongodb1:27017
port = 27018
fork = true
[root@mongodb2 ~]# mongos -f /etc/mongos.conf
2016-06-21T20:27:15.748+0800 W SHARDING [main] Running a sharded cluster with fewer than 3 config servers should only be done for testing purposes and is not recommended for production.
about to fork child process, waiting until server is ready for connections.
forked process: 3878
child process started successfully, parent exiting
分别启动mongodb2,mongodb3的mongod服务,如果是复制集先搭建好复制集,这里就赘述.[root@mongodb2 ~]# cat /etc/mongod.conf
port=27017
dbpath=/data/db
logpath=/data/log/mongod.log
logappend=true
fork = true
#replSet = rs0
oplogSize = 500
shardsvr = true
[root@mongodb2 db]# mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3944
child process started successfully, parent exiting
可以看到这里的提示符为mongos.使用sh.addShard("host:port")命令来添加mongod到集群,如果集群是复制集需要使用sh.adShard("replname/host:port")来添加:[root@mongodb2 db]# mongo 127.0.0.1:27018
MongoDB shell version: 3.2.6
connecting to: 127.0.0.1:27018/test
Server has startup warnings:
2016-06-21T20:27:15.759+0800 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended.
2016-06-21T20:27:15.760+0800 I CONTROL [main]
mongos>
mongos> sh.addShard("mongodb2:27017")
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("mongodb3:27017")
{ "shardAdded" : "shard0001", "ok" : 1 }
分片是集合级别的,所以还需要在集合上开启, 1.首先选择一个 shard key ,所选择的片键会影响集群的效率.参见 选择片键的注意事项. 获得注意事项. 2.如果集合中已经包含有数据,需要使用ensureIndex() 在片键上创建索引.如果集合是空的,MongoDB会在 sh.shardCollection() 过程中自动创建索引. 3.使用sh.shardCollection()方法来为一个集合启用分片,语法如下: sh.shardCollection("<database>.<collection>", shard-key-pattern) 示例如下: sh.shardCollection("records.people", { "zipcode": 1, "name": 1 } ) sh.shardCollection("people.addresses", { "state": 1, "_id": 1 } ) sh.shardCollection("assets.chairs", { "type": 1, "_id": 1 } ) sh.shardCollection("events.alerts", { "_id": "hashed" } ) 我们先来创建一张users表:mongos> show dbs config 0.000GB
mongos> sh.enableSharding("test");
{ "ok" : 1 }
假设我们以i和username为分片键,那么首先创建索引:mongos> use test
switched to db test
mongos> for(i=0;i<100;i++){
... db.users.insert(
... {
... "i":i,
... "username":"user"+i,
... "age":Math.floor(Math.random()*120),
... "created":new Date()
... }
... );
... }
WriteResult({ "nInserted" : 1 })
最后为集合users开启分片:mongos> db.users.createIndex({i:1,username:1})
{
"raw" : {
"mongodb2:27017" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
},
"ok" : 1
}
如果要使用hash分片,那么首先需要为片键创建hash索引使用db.colname.createIndex({"field":"hashed"}); 在使用sh.shardCollection("test.users",{"field":"hashed"})来创建hash分片. 到此mongodb的分片已经搭建完成.可以使用sh.status()查看集群的状态:mongos> sh.shardCollection("test.users",{"i":1,"username":1})
{ "collectionsharded" : "test.users", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId