在MongoDB中分片技术 也就是集群。需要1台配置服务器配置各个节点的配置信息,1台路由服务器来知道每一台节点都在哪个地方并给用户提供各个节点数据的访问功能,还有多台节点服务器,存储节点数据。
当前我有三台机器192.168.0.114,192.168.0.115,192.168.0.116,规划如下:
搭建配置服务器:192.168.0.114
搭建路由服务器:192.168.0.114
搭建3个节点:192.168.0.114,192.168.0.115,192.168.0.116
(192.168.0.114 既做配置服务器,又做路由服务器,同时还做一个节点)
1 搭建配置服务器
配置服务器参数
[root@dghost var]# cat /etc/mong_config1.conf
dbpath=/var/config1--指定配置路径
logpath=/var/log/mongodbs/log1.log--指定日志路径
port=28001--指定配置端口
logappend=true--指定打开节点开关
fork=true--指定后台运行
当前这里我们搭建3个配置,其他两个如下:
[root@dghost var]# cat /etc/mong_config2.conf
dbpath=/var/config2
logpath=/var/log/mongodbs/log2.log
port=28002
logappend=true
fork=true
configsvr=true
[root@dghost var]# cat /etc/mong_config3.conf
dbpath=/var/config3
logpath=/var/log/mongodbs/log3.log
port=28003
logappend=true
fork=true
configsvr=true
启动这3个配置
[root@dghost var]# mongod -f /etc/mong_config1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2360
child process started successfully, parent exiting
[root@dghost var]# mongod -f /etc/mong_config2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2374
child process started successfully, parent exiting
[root@dghost var]# mongod -f /etc/mong_config3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2388
child process started successfully, parent exiting
[root@dghost var]# ps -ef|grep mong
root 2360 1 0 15:19 ? 00:00:00 mongod -f /etc/mong_config1.conf
root 2374 1 1 15:19 ? 00:00:00 mongod -f /etc/mong_config2.conf
root 2388 1 2 15:19 ? 00:00:00 mongod -f /etc/mong_config3.conf
root 2401 2022 0 15:19 pts/0 00:00:00 grep mong
2 搭建路由服务器
路由服务器要配置所有的节点都在哪
[root@dghost var]# cat /etc/mong_rount.conf
port=28000--这个端口是路由端口
configdb=192.168.0.114:28001,192.168.0.114:28002,192.168.0.114:28003--指定所有节点的IP和配置端口
logpath=/var/log/mongodbs/mongos.log--指定路由的日志
logappend=true
fork=true
启动路由服务器
[root@dghost var]# mongos -f /etc/mong_rount.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2406
child process started successfully, parent exiting
[root@dghost var]# ps -ef|grep mong
root 2360 1 0 15:19 ? 00:00:01 mongod -f /etc/mong_config1.conf
root 2374 1 0 15:19 ? 00:00:01 mongod -f /etc/mong_config2.conf
root 2388 1 0 15:19 ? 00:00:01 mongod -f /etc/mong_config3.conf
root 2406 1 0 15:22 ? 00:00:00 mongos -f /etc/mong_rount.conf
root 2466 2022 0 15:22 pts/0 00:00:00 grep mong
到现在我们在192.168.0.114 上搭建了3个配置,1个路由。所有供又4个进程。
3 搭建所有的分片节点
[root@dghost var]# mkdir /data/mongodb -p
# 先建立一个节点存储数据的路径
[root@dghost var]# cat /data/mongodb/mongo1.conf
dbpath=/data/mongodb
logpath=/data/mongodb/mongo1.log
fork=true
port=27020
shardsvr=true
# 配置好节点启动参数
[root@dghost var]# mongod -f /data/mongodb/mongo1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2499
child process started successfully, parent exiting
#节点1 启动成功,启动的节点都这样做
节点搭建好之后做分片
[root@dghost var]# mongo 192.168.0.114:28000
MongoDB shell version: 2.6.4
connecting to: 192.168.0.114:28000/test
#登陆路由服务器
mongos> show dbs
admin (empty)
config 0.016GB
mongos> use admin
# 进入admin库
mongos> db.runCommand({addshard:"192.168.0.114:27020",allowLocal:true})
{ "shardAdded" : "shard0000", "ok" : 1 }
#添加节点成功
mongos> db.runCommand({addshard:"192.168.0.115:27020"})
{ "shardAdded" : "shard0001", "ok" : 1 }
#再加一个节点
mongos> use config
switched to db config
mongos> db.shards.find()
{ "_id" : "shard0000", "host" : "192.168.0.114:27020" }
{ "_id" : "shard0001", "host" : "192.168.0.115:27020" }
# 检查已经添加的连个节点
switched to db admin
mongos> db.runCommand({"enablesharding":"test_database"})
{ "ok" : 1 }
# 在admin 下需要指定哪个库需要分片
mongos> db.runCommand({"shardcollection":"test_database.test_table","key":{"_id":1}})
{ "collectionsharded" : "test_database.test_table", "ok" : 1 }
# 继续指定哪个表需要分片
mongos> db.databases.find()
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test_database", "partitioned" : true, "primary" : "shard0000" }
# 库test_database 已经被设置未分片成功了
mongos> db.chunks.find()
{ "_id" : "test_database.test_table-_id_MinKey", "lastmod" : Timestamp(1, 0), "lastmodEpoch" : ObjectId("58ff0371b44f90194e6a262e"), "ns" : "test_database.test_table", "min" : { "_id" : { "$minKey" : 1 } }, "max" : { "_id" : { "$maxKey" : 1 } }, "shard" : "shard0000" }
# 这个也可以检查
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 4,
"minCompatibleVersion" : 4,
"currentVersion" : 5,
"clusterId" : ObjectId("58fef935b44f90194e6a2496")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.0.114:27020" }
{ "_id" : "shard0001", "host" : "192.168.0.115:27020" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test_database", "partitioned" : true, "primary" : "shard0000" }
test_database.test_table
shard key: { "_id" : 1 }
chunks:
shard0000 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)
# 这个也可以检查
后面可以插入大量数据并检查,看是不是数据已经分片了