0、需要环境
安装包:mongodb-linux-x86_64-3.4.6.tgz
安装路径:/usr/mongodb
服务器: 192.168.177.131/132/133
mongos |
20000 |
config server |
21000 |
shard1 |
20001 |
shard2 |
20002 |
shard3 |
20003 |
1、解压到 /usr/local
解压后重命名为mongodb,方便以后操作
2、创建目录
mkdir -p mongodb/mongos/log mkdir -p mongodb/config-server/data mkdir -p mongodb/config-server/log mkdir -p mongodb/shard1/data mkdir -p mongodb/shard1/log mkdir -p mongodb/shard2/data mkdir -p mongodb/shard2/log mkdir -p mongodb/shard3/data mkdir -p mongodb/shard3/log |
其中data目录存放数据,log目录存放日志
mongos服务不存放数据,所以不需要数据目录,只需要log目录
3、配置服务器 config server
vim /usr/local/mongodb/config-server.conf
systemLog: destination: file path: "/usr/local/mongodb/config-server/log/config-server.log" logAppend: true storage: dbPath: "/usr/local/mongodb/config-server/data" journal: enabled: true directoryPerDB: true net: port: 21000 processManagement: fork: true pidFilePath: "/usr/local/mongodb/config-server/config-server.pid" sharding: clusterRole: configsvr replication: replSetName: configServer |
冒号(:)和配置之间需要有空格
启动三台服务器
cd mongodb
bin/mongod -f config-server.conf
登录任意一台配置服务器,初始化
#连接
mongodb/bin/mongo --port 21000
config = {
_id : "configServer",
members : [
{_id : 0, host : "192.168.177.131:21000" },
{_id : 1, host : "192.168.177.132:21000" },
{_id : 2, host : "192.168.177.133:21000" }
]
}
#初始化副本集
rs.initiate(config);
#查看分区状态
rs.status();
4、分片副本集 shard
vim /usr/local/mongodb/shard1.conf
systemLog: destination: file path: "/usr/local/mongodb/shard1/log/shard1.log" logAppend: true storage: dbPath: "/usr/local/mongodb/shard1/data" journal: enabled: true directoryPerDB: true net: port: 20001 processManagement: fork: true pidFilePath: "/usr/local/mongodb/shard1/shard1.pid" sharding: clusterRole: shardsvr replication: replSetName: shard1 |
启动三台服务器:
cd mongodb
bin/mongod -f shard1.conf
登陆任意一台服务器,初始化副本集
mongo --port 20001
#使用admin数据库
use admin
config = { _id : "shard1", members : [ {_id : 0, host : "192.168.177.131:20001" }, {_id : 1, host : "192.168.177.132:20001" }, {_id : 2, host : "192.168.177.133:20001" } ] } |
#初始化副本集
rs.initiate(config);
#查看分区状态
rs.status();
配置第二,三个分片副本集,方法同第六步。 注意端口改为20002 20003
systemLog: destination: file path: "/usr/local/mongodb/shard2/log/shard2.log" logAppend: true storage: dbPath: "/usr/local/mongodb/shard2/data" journal: enabled: true directoryPerDB: true net: port: 20002 processManagement: fork: true pidFilePath: "/usr/local/mongodb/shard2/shard2.pid" sharding: clusterRole: shardsvr replication: replSetName: shard2 |
systemLog: destination: file path: "/usr/local/mongodb/shard3/log/shard3.log" logAppend: true storage: dbPath: "/usr/local/mongodb/shard3/data" journal: enabled: true directoryPerDB: true net: port: 20003 processManagement: fork: true pidFilePath: "/usr/local/mongodb/shard3/shard3.pid" sharding: clusterRole: shardsvr replication: replSetName: shard3 |
启动三台服务器:
cd mongodb
bin/mongod -f shard2.conf
bin/mongod -f shard3.conf
登陆任意一台服务器,初始化副本集
mongo --port 20002
mongo --port 20003
#使用admin数据库
use admin
config = {
_id : "shard2",
members : [
{_id : 0, host : "192.168.177.131:20002" },
{_id : 1, host : "192.168.177.132:20002" },
{_id : 2, host : "192.168.177.133:20002" }
]
}
config = {
_id : "shard3",
members : [
{_id : 0, host : "192.168.177.131:20003" },
{_id : 1, host : "192.168.177.132:20003" },
{_id : 2, host : "192.168.177.133:20003" }
]
}
#初始化副本集
rs.initiate(config);
#查看分区状态
rs.status();
5、路由服务器 mongos
vim /usr/local/mongodb/mongos.conf
systemLog: destination: file path: "/usr/local/mongodb/mongos/log/mongos.log" logAppend: true net: port: 20000 bindIp: 0.0.0.0 processManagement: fork: true pidFilePath: "/usr/local/mongodb/mongos/mongos.pid" sharding: configDB: configReplSet/192.168.177.131:21000,192.168.177.132:21000,192.168.177.133:21000 |
启动三台服务器:
cd mongodb
bin/mongos -f mongos.conf
注意此处启动使用的是mongos
6、启用分片
目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。
#登陆任意一台mongos
mongo --port 20000
#使用admin数据库
use admin
#串联路由服务器与分配副本集
sh.addShard("shard1/192.168.177.131:20001,192.168.177.132:20001,192.168.177.133:20001");
sh.addShard("shard2/192.168.177.131:20002,192.168.177.132:20002,192.168.177.133:20002");
sh.addShard("shard3/192.168.177.131:20003,192.168.177.132:20003,192.168.177.133:20003");
#查看集群状态
sh.status()
至此集群已经搭建完成。
7、测试
连接mongos
bin/mongo --port 20000
首先启动某个Database(数据库)的Sharding(分片)功能
mongos> sh.enableSharding("test")
对testDB数据库里的collection logs进行shard,shard key是sn字段,shard策略是hashed。使用hashed策略旨在让mongodb将数据均匀分布,如果使用{"sn",1}进行range shard,数据会按照大小顺序分布,就会出现某个shard数据很多,其他的很少的现象。
mongos> sh.shardCollection("testDB.logs", { "sn" : "hashed" } )
查看shard信息
mongos> sh.status()
添加测试数据
mongos> use testDB
mongos> for(var i=1; i<=100000; i++){db.log.insert({sn:i, msg:'Message ' + i});}
等待一段时间
查看log的shard信息
mongos> db.logs.stats()
可以看到三个分片几乎平均的保存了数据。
33143 33755 33102
{
"sharded" : true,
"capped" : false,
"ns" : "testDB.logs",
"count" : 100000,
"size" : 5688895,
"storageSize" : 1773568,
"totalIndexSize" : 3944448,
"indexSizes" : {
"_id_" : 983040,
"sn_hashed" : 2961408
},
"avgObjSize" : 56,
"nindexes" : 2,
"nchunks" : 6,
"shards" : {
"shard1" : {
"ns" : "testDB.logs",
"size" : 1920351,
"count" : 33755,
"avgObjSize" : 56,
"storageSize" : 634880,
"capped" : false,
...
"ok" : 1
},
"shard2" : {
"ns" : "testDB.logs",
"size" : 1885397,
"count" : 33143,
"avgObjSize" : 56,
"storageSize" : 544768,
"capped" : false,
...
"ok" : 1
},
"shard3" : {
"ns" : "testDB.logs",
"size" : 1883147,
"count" : 33102,
"avgObjSize" : 56,
"storageSize" : 593920,
"capped" : false,
...
"ok" : 1
}
},
"ok" : 1
}
8、后期运维
mongodb的启动顺序是,先启动配置服务器,在启动分片,最后启动mongos.
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/config-server.conf
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/shard1.conf
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/shard2.conf
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/shard3.conf
/usr/local/mongodb/bin/mongos -f /usr/local/mongodb/mongos.conf
关闭时,直接killall杀掉所有进程
killall mongod
killall mongos
没有killall命令的使用