CentOS 7.4 部署 MongoDB 4.0.2 分片集群

元气小坏坏 提交于 2020-05-01 01:10:06

如图应DEV同事要求,部署一套mongoDB DEV 环境,安装环境OS是 CentOS 7.4;
遂在网上查找些文档,因为使用的是4.0.2版本,有些步骤与网络文章稍有不同,自行尝试后均已解决。
本文只是一个安装演示,所以诸多命名和配置方法并不严谨,若是生产环境,请根据实际需求加入认证和连接参数,并建议将进程启动选项写入到配置文件持久化保存。


准备工作:
1.集群内设置时间同步
2.关闭防火墙,禁用SElinux
3.关闭 THP 特性


集群规划:
1.计划部署3个节点的分片集群,配置hosts如下:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.197.131 svr131
192.168.197.130 svr130
192.168.197.129 svr129

2.设置3个分片,每个分片为2个实例的副本集
分片 shardaaa 位于 node-1,node-2
分片 shardbbb 位于 node-2,node-3
分片 shardccc 位于 node-1,node-3

 

MongoDB 服务器角色描述:

配置服务器:是一个独立的mongod进程,保存集群和分片的元数据,即各分片包含了哪些数据的信息。最先开始建立,启用日志功能。像启动普通的mongod一样启动配置服务器,指定configsvr选项。

                     不需要太多的空间和资源,配置服务器的1KB空间相当于真是数据的200MB。保存的只是数据的分布表。当服务不可用,则变成只读,无法分块、迁移数据。

路由服务器:即mongos,起到一个路由的功能,供程序连接。本身不保存数据,在启动时从配置服务器加载集群信息,开启mongos进程需要知道配置服务器的地址,指定configdb选项。

分片服务器:是一个独立普通的mongod进程,保存数据信息。可以是一个副本集也可以是单独的一台服务器。

 

一,分片服务器部署:

在3个节点创建存放数据和日志的目录:
mkdir -p /data/mongodb/shardaaa/{data,logs}
mkdir -p /data/mongodb/shardbbb/{data,logs}
mkdir -p /data/mongodb/shardccc/{data,logs}

如果是目录已经存在,请将之前的数据清空,此步骤谨慎操作,我这里是开发测试环境:
rm -rf /data/mongodb/shardaaa/data/*
rm -rf /data/mongodb/shardbbb/data/*
rm -rf /data/mongodb/shardccc/data/*


在node-1节点上启动分片 shardaaa 和 shardccc 进程:
mongod --dbpath=/data/mongodb/shardaaa/data --shardsvr --replSet shardaaa --bind_ip_all --port 30100 --directoryperdb --logpath=/data/mongodb/shardaaa/logs/mongo.log --logappend --fork

mongod --dbpath=/data/mongodb/shardccc/data --shardsvr --replSet shardccc --bind_ip_all --port 30300 --directoryperdb --logpath=/data/mongodb/shardccc/logs/mongo.log --logappend --fork


在node-2 节点上启动分片 shardaaa 和 shardbbb 进程:

mongod --dbpath=/data/mongodb/shardaaa/data --shardsvr --replSet shardaaa --bind_ip_all --port 30100 --directoryperdb --logpath=/data/mongodb/shardaaa/logs/mongo.log --logappend --fork

mongod --dbpath=/data/mongodb/shardbbb/data --shardsvr --replSet shardbbb --bind_ip_all --port 30200 --directoryperdb --logpath=/data/mongodb/shardbbb/logs/mongo.log --logappend --fork


在node-3 节点上启动分片 shardbbb 和 shardccc 进程:

mongod --dbpath=/data/mongodb/shardbbb/data --shardsvr --replSet shardbbb --bind_ip_all --port 30200 --directoryperdb --logpath=/data/mongodb/shardbbb/logs/mongo.log --logappend --fork

mongod --dbpath=/data/mongodb/shardccc/data --shardsvr --replSet shardccc --bind_ip_all --port 30300 --directoryperdb --logpath=/data/mongodb/shardccc/logs/mongo.log --logappend --fork

初始化分片副本集:
在node-1 或 node-2上:
mongo --port 30100
config = {_id: 'shardaaa', members: [{_id: 0, host: '192.168.197.131:30100'},{_id: 1, host: '192.168.197.130:30100'}]}
rs.initiate(config)
rs.status()

在node-2 或 node-3上:
mongo --port 30200
config = {_id: 'shardbbb', members: [{_id: 0, host: '192.168.197.130:30200'},{_id: 1, host: '192.168.197.129:30200'}]}
rs.initiate(config)
rs.status()

在node-1 或 node-3上:
mongo --port 30300
config = {_id: 'shardccc', members: [{_id: 0, host: '192.168.197.131:30300'},{_id: 1, host: '192.168.197.129:30300'}]}
rs.initiate(config)
rs.status()

 

二,然后在每个节点启动一个configsvr 进程,并建立一个3节点的配置副本集:
在3个节点上创建配置服务目录
mkdir -p /data/mongodb/configsvr/logs

然后在每个节点启动配置服务 config server 进程:
mongod --configsvr --replSet conf --bind_ip_all --port 40000 --dbpath /data/mongodb/configsvr --logpath=/data/mongodb/configsvr/logs/config.log --directoryperdb --logappend --fork


在任一节点上初始化配置服务器副本集:
mongo --port 40000

rs.initiate(
{
_id: "conf",
configsvr: true,
members: [
{ _id : 0, host : "192.168.197.131:40000" },
{ _id : 1, host : "192.168.197.130:40000" },
{ _id : 2, host : "192.168.197.129:40000" }
]
}
)

 

三,在每个(并非必须每个节点)节点上创建启动Route server:
mongos --configdb "conf/192.168.197.131:40000,192.168.197.130:40000,192.168.197.129:40000" --bind_ip_all --port 50000 --logpath=/data/mongodb/route.log --logappend --fork

--configdb "conf/192.168.197.131:40000,192.168.197.130:40000,192.168.197.129:40000" #此参数较之前的版本有了变化,仅指定IP和端口将会报错


其中最重要的参数是configdb,不能在其后面带的配置服务器的地址写成localhost或则127.0.0.1,需要设置成其他分片也能访问的地址,即192.168.197.131:40000/,否则在addshard的时候会报错。

 


四,登陆路由服务器mongos ,添加分片

[root@svr131 ~]# mongo --port 50000
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:50000/
MongoDB server version: 4.0.2
Server has startup warnings:
2018-10-09T19:02:05.489+0800 I CONTROL [main]
2018-10-09T19:02:05.489+0800 I CONTROL [main] ** WARNING: Access control is not enabled for the database.
2018-10-09T19:02:05.489+0800 I CONTROL [main] ** Read and write access to data and configuration is unrestricted.
2018-10-09T19:02:05.489+0800 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended.
2018-10-09T19:02:05.489+0800 I CONTROL [main]

#查看集群的信息
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5bc555a968034bbce0f63e2a")
}
shards:
active mongoses:
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> sh.addShard("shardaaa/192.168.197.131:30100,192.168.197.130:30100")
{
"shardAdded" : "shardaaa",
"ok" : 1,
"operationTime" : Timestamp(1539661132, 6),
"$clusterTime" : {
"clusterTime" : Timestamp(1539661132, 6),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}


mongos> sh.addShard("shardbbb/192.168.197.130:30200,192.168.197.129:30200")
{
"shardAdded" : "shardbbb",
"ok" : 1,
"operationTime" : Timestamp(1539661179, 6),
"$clusterTime" : {
"clusterTime" : Timestamp(1539661179, 6),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}


mongos> sh.addShard("shardccc/192.168.197.129:30300,192.168.197.131:30300")
{
"shardAdded" : "shardccc",
"ok" : 1,
"operationTime" : Timestamp(1539661206, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1539661206, 3),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

 

#验证设置
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5bc555a968034bbce0f63e2a")
}
shards:
{ "_id" : "shardaaa", "host" : "shardaaa/192.168.197.130:30100,192.168.197.131:30100", "state" : 1 }
{ "_id" : "shardbbb", "host" : "shardbbb/192.168.197.129:30200,192.168.197.130:30200", "state" : 1 }
{ "_id" : "shardccc", "host" : "shardccc/192.168.197.129:30300,192.168.197.131:30300", "state" : 1 }
active mongoses:
"4.0.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> use testdb
switched to db testdb

mongos> db.coll001.insert({"name":"alice"})
WriteResult({ "nInserted" : 1 })


#开启分片功能:sh.enableSharding("库名")、
sh.shardCollection("库名.集合名",{"key":1})

#首先对数据库启用分片
mongos> sh.enableSharding("testdb")
{
"ok" : 1,
"operationTime" : Timestamp(1539661677, 6),
"$clusterTime" : {
"clusterTime" : Timestamp(1539661677, 6),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

 

#查看分片信息
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5bc555a968034bbce0f63e2a")
}
shards:
{ "_id" : "shardaaa", "host" : "shardaaa/192.168.197.130:30100,192.168.197.131:30100", "state" : 1 }
{ "_id" : "shardbbb", "host" : "shardbbb/192.168.197.129:30200,192.168.197.130:30200", "state" : 1 }
{ "_id" : "shardccc", "host" : "shardccc/192.168.197.129:30300,192.168.197.131:30300", "state" : 1 }
active mongoses:
"4.0.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 }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shardaaa 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shardaaa Timestamp(1, 0)
{ "_id" : "testdb", "primary" : "shardbbb", "partitioned" : true, "version" : { "uuid" : UUID("21bf70b0-6f62-48cd-8a36-95dbb852d134"), "lastMod" : 1 } }

 

参考:

https://cloud.tencent.com/developer/article/1116318

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!