内容转载,原文连接:https://yq.aliyun.com/articles/422597?spm=a2c4e.11155472.0.0.ad0d55d8PCpSNN
mongodb下载连接: https://bbcbackup.oss-cn-shenzhen.aliyuncs.com/run/linux/mongodb-linux-x86_64-rhel70-3.4.2.tgz
先是文字描述,告诉你这个东西该怎么搭建哈
先呢准备三台服务器192.168.9.85//192.168.9.90//192.168.9.91,下载mongodb3.4版本以上(这个版本以下的我没试过)然后呢下载,解压,文件移动,文件名修改再进去这个文件,现在要新建文件及文件夹,用来放启动文件和日志等文件
mkdir -p /usr/local/mongodb/conf \
mkdir -p /usr/local/mongodb/mongos/log \
mkdir -p /usr/local/mongodb/config/data \
mkdir -p /usr/local/mongodb/config/log \
mkdir -p /usr/local/mongodb/shard1/data \
mkdir -p /usr/local/mongodb/shard1/log \
mkdir -p /usr/local/mongodb/shard2/data \
mkdir -p /usr/local/mongodb/shard2/log \
mkdir -p /usr/local/mongodb/shard3/data \
mkdir -p /usr/local/mongodb/shard3/log
我的mongodb文件放在/usr/local/里面,文件名为mongodb
可以在这里添加环境变量,这样以后操作方便一些
配置环境变量
vi /etc/profile
# MongoDB 环境变量内容
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
使立即生效
source /etc/profile
现在来搭建配置服务器,一个mongodb副本集,mongodb3.4以后要求配置服务器也创建副本集,不然集群搭建不成功。
(三台机器)添加配置文件
vi /usr/local/mongodb/conf/config.conf
## 配置文件内容
pidfilepath = /usr/local/mongodb/config/log/configsrv.pid
dbpath = /usr/local/mongodb/config/data
logpath = /usr/local/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 21000
fork = true
#declare this is a config db of a cluster;
configsvr = true
#副本集名称
replSet = configs
#设置最大连接数
maxConns = 20000
启动三台服务器的config server
mongod -f /usr/local/mongodb/conf/config.conf
登录任意一台配置服务器,初始化配置副本集
连接 MongoDB
mongo --port 21000
config 变量
config = {
_id : "configs",
members : [
{_id : 0, host : "192.168.9.85:21000" },
{_id : 1, host : "192.168.9.90:21000" },
{_id : 2, host : "192.168.9.91:21000" }
]
}
初始化副本集
rs.initiate(config)
查询状态
configs:SECONDARY> rs.status()
一主两副_id : 0的服务器为主机
配置服务器搭建好了之后再来进行第一个分片副本集搭建
1 设置第一个分片副本集
(三台机器)设置第一个分片副本集
配置文件
vi /usr/local/mongodb/conf/shard1.conf
#配置文件内容
#——————————————–
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid
dbpath = /usr/local/mongodb/shard1/data
logpath = /usr/local/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
#副本集名称
replSet = shard1
#declare this is a shard db of a cluster;
shardsvr = true
#设置最大连接数
maxConns = 20000
启动三台服务器的shard1 server
mongod -f /usr/local/mongodb/conf/shard1.conf
登陆任意一台服务器,初始化副本集(除了已经进行分片的服务器以外,假设192.168.9.85已经进行分片,那么,再次登录192.168.9.85服务器初始化副本集进行分片会报错【id:X不可用】)
连接 MongoDB
mongo --port 27001
使用admin数据库
use admin
定义副本集配置
config = {
_id : "shard1",
members : [
{_id : 0, host : "192.168.9.85:27001" },
{_id : 1, host : "192.168.9.90:27001" },
{_id : 2, host : "192.168.9.91:27001" , arbiterOnly: true }
]
}
初始化副本集配置
rs.initiate(config)
所有分片配置基本一样,但是的修改端口号和文件名,防止冲突
在定义副本集的时候一定要注意【arbiterOnly:true】,这是定义哪台服务器为仲裁者的参数,在进行分片副本集配置的时候最好是这样配置
192.168.9.85 | 192.168.9.90 | 192.168.9.91 |
---|---|---|
mongos | mongos | mongos |
config server | config server | config server |
shard server1 主节点 | shard server1 副节点 | shard server1 仲裁 |
shard server2 仲裁 | shard server2 主节点 | shard server2 副节点 |
shard server3 副节点 | shard server3 仲裁 | shard server3 主节点 |
端口分配:
mongos:20000 路由服务器
config:21000 配置服务器
shard1:27001 分片服务器 1
shard2:27002 分片服务器 2
shard3:27003 分片服务器 3
上面写了一个分片副本集的教程,但因为三个副本集的操作差不多我就不写了,现在进行路由mongodb的配置及启动
配置路由服务器 mongos
(三台机器)先启动配置服务器和分片服务器,后启动路由实例启动路由实例:
vi /usr/local/mongodb/conf/mongos.conf
#内容
pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid
logpath = /usr/local/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
configdb = configs/192.168.9.85:21000,192.168.9.90:21000,192.168.9.91:21000
#设置最大连接数
maxConns = 20000
启动三台服务器的mongos server
mongos -f /usr/local/mongodb/conf/mongos.conf
都搭建好且启动之后,登录任意服务器,进行分片串联,连接到路由上,这样外部直接在路由上操作就行了
串联路由服务器
目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。
登陆任意一台mongos
mongo --port 20000
使用admin数据库
use admin
串联路由服务器与分配副本集
sh.addShard("shard1/192.168.9.85:27001,192.168.9.90:27001,192.168.9.91:27001");
sh.addShard("shard2/192.168.9.85:27002,192.168.9.90:27002,192.168.9.91:27002");
sh.addShard("shard3/192.168.9.85:27003,192.168.9.90:27003,192.168.9.91:27003");
查看集群状态
sh.status()
响应内容如下
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5a713a37d56e076f3eb47acf")
}
shards:
{ "_id" : "shard1", "host" : "shard1/192.168.252.121:27001,192.168.252.122:27001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/192.168.252.122:27002,192.168.252.123:27002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/192.168.252.121:27003,192.168.252.123:27003", "state" : 1 }
active mongoses:
"3.6.2" : 3
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
mongos>
启用集合分片生效
目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片。连接在mongos上,准备让指定的数据库、指定的集合分片生效。
登陆任意一台mongos
mongo --port 20000
使用admin数据库
use admin
指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
指定数据库里需要分片的集合和片键,哈希id 分片
db.runCommand( { shardcollection : "testdb.table1",key : {"id": "hashed"} } );
我们设置testdb的 table1 表需要分片,根据 id 自动分片到 shard1 ,shard2,shard3 上面去。要这样设置是因为不是所有mongodb 的数据库和表 都需要分片!
测试分片配置结果
连接 MongoDB 路由服务
mongo 127.0.0.1:20000
切换到 testdb 数据库
use testdb;
插入测试数据
for(i=1;i<=100000;i++){db.table1.insert({"id":i,"name":"penglei"})}; #循环插入10万条数据,需要点时间
总条数
db.table1.aggregate([{$group : {_id : "$name", totle : {$sum : 1}}}])
查看分片情况如下
- shard1: "count": 33755
- shard2: "count": 33143,
- shard3: "count": 33102
结论数据基本均匀
db.table1.stats();
mongos> db.table1.stats();
{
"sharded": true,
"capped": false,
"ns": "testdb.table1",
"count": 100000,
"size": 5200000,
"storageSize": 1519616,
"totalIndexSize": 3530752,
"indexSizes": {
"_id_": 892928,
"id_hashed": 2637824
},
"avgObjSize": 52,
"nindexes": 2,
"nchunks": 6,
"shards": {
"shard1": {
"ns": "testdb.table1",
"size": 1755260,
"count": 33755,
"avgObjSize": 52,
"storageSize": 532480,
"capped": false,
"wiredTiger": {
...省略很多
}
},
"shard2": {
"ns": "testdb.table1",
"size": 1723436,
"count": 33143,
"avgObjSize": 52,
"storageSize": 479232,
"capped": false,
"wiredTiger": {
...省略很多
}
},
"shard3": {
"ns": "testdb.table1",
"size": 1721304,
"count": 33102,
"avgObjSize": 52,
"storageSize": 507904,
"capped": false,
"wiredTiger": {
...省略很多
}
}
},
"ok": 1,
"$clusterTime": {
"clusterTime": Timestamp(1517488062, 350),
"signature": {
"hash": BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId": NumberLong(0)
}
},
"operationTime": Timestamp(1517488062, 350)
}
mongos>
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5a73053f896ba937d7beb807")
}
shards:
{ "_id" : "shard1", "host" : "shard1/192.168.252.121:27001,192.168.252.122:27001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/192.168.252.122:27002,192.168.252.123:27002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/192.168.252.121:27003,192.168.252.123:27003", "state" : 1 }
active mongoses:
"3.6.2" : 3
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
2 : Success
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard1 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
{ "_id" : "testdb", "primary" : "shard1", "partitioned" : true }
testdb.table1
shard key: { "id" : "hashed" }
unique: false
balancing: true
chunks:
shard1 2
shard2 2
shard3 2
{ "id" : { "$minKey" : 1 } } -->> { "id" : NumberLong("-6148914691236517204") } on : shard1 Timestamp(3, 2)
{ "id" : NumberLong("-6148914691236517204") } -->> { "id" : NumberLong("-3074457345618258602") } on : shard1 Timestamp(3, 3)
{ "id" : NumberLong("-3074457345618258602") } -->> { "id" : NumberLong(0) } on : shard2 Timestamp(3, 4)
{ "id" : NumberLong(0) } -->> { "id" : NumberLong("3074457345618258602") } on : shard2 Timestamp(3, 5)
{ "id" : NumberLong("3074457345618258602") } -->> { "id" : NumberLong("6148914691236517204") } on : shard3 Timestamp(3, 6)
{ "id" : NumberLong("6148914691236517204") } -->> { "id" : { "$maxKey" : 1 } } on : shard3 Timestamp(3, 7)
mongos>
分组查看总数量是:100000
mongos> db.table1.aggregate([{$group : {_id : "$name", totle : {$sum : 1}}}])
{ "_id" : "penglei", "totle" : 100000 }
mongos>
至此,mongodb分片副本集集群就搭建好了,再见。