Centos7 MongoDB(分片+副本)集群搭建

2019-11-18  本文已影响0人  增肥

相关概念

搭建集群之前需要了解几个概念:路由,分片、副本集、配置服务器等。


简单了解之后,我们可以这样总结一下,应用请求mongos来操作mongodb的增删改查,配置服务器存储数据库元信息,并且和mongos做同步,数据最终存入在shard(分片)上,为了防止数据丢失同步在副本集中存储了一份,仲裁在数据存储到分片的时候决定存储到哪个节点。

服务器规划

服务器(192.168.0.111) 服务器(192.168.0.112) 服务器(192.168.0.217) 端口
mongos mongos mongos 20000
config server config server config server 21000
shard server1 主节点 shard server1 仲裁 shard server1 副节点 27001
shard server2 副节点 shard server2 主节点 shard server2 仲裁 27002
shard server3 仲裁 shard server3 副节点 shard server3 主节点 27003

集群搭建

1、安装mongodb
#下载
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.6.tgz

#解压
tar -xzvf mongodb-linux-x86_64-3.4.6.tgz -C /usr/local/

#进去解压位置
cd /usr/local/

#改名
mv mongodb-linux-x86_64-3.4.6  mongodb

#分别在机器上建立conf、mongos、config、shard1、shard2、shard3六个目录,(每台服务器根据部署情况创建相关目录)
mkdir -p /usr/local/mongodb/conf
mkdir -p /usr/local/mongodb/mongos/log   #因为mongos不存储数据,只需要建立日志文件目录即可
mkdir -p /usr/local/mongodb/config/{data,log}
mkdir -p /usr/local/mongodb/shard{1,2,3}/{data,log}


#配置环境变量
vim /etc/profile
#内容
export PATH=$PATH:/usr/local/mongodb/bin

#使立即生效
source /etc/profile
2、config server配置服务器

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

登录任意一台配置服务器,初始化配置副本集

#连接
mongo --port 21000
#config变量
config = {
...    _id : "configs",
...     members : [
...         {_id : 0, host : "192.168.0.111:21000" },
...         {_id : 1, host : "192.168.0.112:21000" },
...         {_id : 2, host : "192.168.0.217:21000" }
...     ]
... }

#初始化副本集
rs.initiate(config)

其中,"_id" : "configs"应与配置文件中配置的 replSet(副本集名称) 一致,"members" 中的 "host" 为三个节点的 ip 和 port

3、配置分片副本集(三台机器)

3.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
 
#打开web监控
httpinterface=true
rest=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

登陆任意一台服务器,初始化副本集

#登录
mongo --port 27001

#使用admin数据库
use admin

#定义副本集配置,第三个节点的 "arbiterOnly":true 代表其为仲裁节点。
config = {
...    _id : "shard1",
...     members : [
...         {_id : 0, host : "192.168.0.111:27001" },
...         {_id : 1, host : "192.168.0.112:27001" , arbiterOnly: true },
...         {_id : 2, host : "192.168.0.217:27001" }
...     ]
... }

#初始化副本集配置
rs.initiate(config);

3.2、设置第二个分片副本集

#编写配置文件
vi /usr/local/mongodb/conf/shard2.conf

#配置文件内容
#——————————————–
pidfilepath = /usr/local/mongodb/shard2/log/shard2.pid
dbpath = /usr/local/mongodb/shard2/data
logpath = /usr/local/mongodb/shard2/log/shard2.log
logappend = true

bind_ip = 0.0.0.0
port = 27002
fork = true
 
#打开web监控
httpinterface=true
rest=true
 
#副本集名称
replSet=shard2
 
#declare this is a shard db of a cluster;
shardsvr = true
 
#设置最大连接数
maxConns=20000

启动三台服务器的shard2 server

mongod -f /usr/local/mongodb/conf/shard2.conf

登陆任意一台服务器,初始化副本集

#登录
mongo --port 27002

#使用admin数据库
use admin

#定义副本集配置
config = {
...    _id : "shard2",
...     members : [
...         {_id : 0, host : "192.168.0.111:27002" },
...         {_id : 1, host : "192.168.0.112:27002" },
...         {_id : 2, host : "192.168.0.217:27002" , arbiterOnly: true }
...     ]
... }

#初始化副本集配置
rs.initiate(config);

3.3、设置第三个分片副本集

#编写配置文件
vi /usr/local/mongodb/conf/shard3.conf

#配置文件内容
#——————————————–
pidfilepath = /usr/local/mongodb/shard3/log/shard3.pid
dbpath = /usr/local/mongodb/shard3/data
logpath = /usr/local/mongodb/shard3/log/shard3.log
logappend = true

bind_ip = 0.0.0.0
port = 27003
fork = true
 
#打开web监控
httpinterface=true
rest=true
 
#副本集名称
replSet=shard3
 
#declare this is a shard db of a cluster;
shardsvr = true
 
#设置最大连接数
maxConns=20000

启动三台服务器的shard3 server

mongod -f /usr/local/mongodb/conf/shard3.conf

登陆任意一台服务器,初始化副本集

#登录
mongo --port 27003

#使用admin数据库
use admin

#定义副本集配置
config = {
...    _id : "shard3",
...     members : [
...         {_id : 0, host : "192.168.0.111:27003" , arbiterOnly: true },
...         {_id : 1, host : "192.168.0.112:27003" },
...         {_id : 2, host : "192.168.0.217:27003" }
...     ]
... }

#初始化副本集配置
rs.initiate(config);

如初始化副本集出现如下报错:
解决方案:仲裁不能再同一台服务器上换下就可以。

> config = { _id : "shard3", members : [{_id : 0, host : "192.168.0.111:27003" , arbiterOnly: true },{_id : 1, host : "192.168.0.112:27003" },{_id : 2, host : "192.168.0.217:27003" }]}
{
        "_id" : "shard3",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "192.168.0.111:27003",
                        "arbiterOnly" : true
                },
                {
                        "_id" : 1,
                        "host" : "192.168.0.112:27003"
                },
                {
                        "_id" : 2,
                        "host" : "192.168.0.217:27003"
                }
        ]
}
> rs.initiate(config);
{
        "ok" : 0,
        "errmsg" : "This node, 192.168.0.111:27003, with _id 0 is not electable under the new configuration version 1 for replica set shard3",
        "code" : 93,
        "codeName" : "InvalidReplicaSetConfig"
}
4、配置路由服务器 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.0.111:21000,192.168.0.112:21000,192.168.0.217:21000
 
#设置最大连接数
maxConns=20000

启动三台服务器的mongos server

mongos -f /usr/local/mongodb/conf/mongos.conf
5、启用分片

目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。

#登陆任意一台mongos
mongo --port 20000
#使用admin数据库
use  admin
#串联路由服务器与分配副本集
sh.addShard("shard1/192.168.0.111:27001,192.168.0.112:27001,192.168.0.217:27001")
sh.addShard("shard2/192.168.0.111:27002,192.168.0.112:27002,192.168.0.217:27002")
sh.addShard("shard3/192.168.0.111:27003,192.168.0.112:27003,192.168.0.217:27003")
#查看集群状态
sh.status()
6、分片测试

目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片。连接在mongos上,准备让指定的数据库、指定的集合分片生效。

#指定testdb库分片生效
db.runCommand( { enablesharding :"testdb"});
#指定数据库里需要分片的集合(testdb.table1)和片键(以索引作为分片的键值进行分片)
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
#登录
mongo --port 20000

#使用testdb
use  testdb;

#插入测试数据
for (var i = 1; i <= 100000; i++) db.table1.save({id:i,"test1":"testval1"});

#查看分片情况如下,部分无关信息省掉了
db.table1.stats();
                "shard1" : {
                        "ns" : "testdb.table1",
                        "size" : 54,
                        "count" : 1,
                        "avgObjSize" : 54,
                        "storageSize" : 16384,
                        "capped" : false,
                        "wiredTiger" : {
                                "metadata" : {
                                        "formatVersion" : 1
                                }
                "shard2" : {
                        "ns" : "testdb.table1",
                        "size" : 972,
                        "count" : 18,
                        "avgObjSize" : 54,
                        "storageSize" : 16384,
                        "capped" : false,
                        "wiredTiger" : {
                                "metadata" : {
                                        "formatVersion" : 1
                                }
                "shard3" : {
                        "ns" : "testdb.table1",
                        "size" : 5398974,
                        "count" : 99981,
                        "avgObjSize" : 54,
                        "storageSize" : 1716224,
                        "capped" : false,
                        "wiredTiger" : {
                                "metadata" : {
                                        "formatVersion" : 1
                                }


可以看到数据分到3个分片,各自分片数量为: shard1 “count” : 1,shard2 “count” : 18,shard3 “count” : 99981。已经成功了!
#查看集群的状态
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5dd20dcb4e1701b7accb3d02")
}
  shards:
        {  "_id" : "shard1",  "host" : "shard1/192.168.0.111:27001,192.168.0.217:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/192.168.0.111:27002,192.168.0.112:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/192.168.0.111:27003,192.168.0.217:27003",  "state" : 1 }
  active mongoses:
        "3.4.6" : 2
        "3.4.0" : 1
 autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
                Balancer lock taken at Mon Nov 18 2019 11:19:40 GMT+0800 (CST) by ConfigServer:Balancer
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                2 : Success
  databases:
        {  "_id" : "testdb",  "primary" : "shard3",  "partitioned" : true }
                testdb.table1
                        shard key: { "id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard1  1
                                shard2  1
                                shard3  1
                        { "id" : { "$minKey" : 1 } } -->> { "id" : 2 } on : shard1 Timestamp(2, 0) 
                        { "id" : 2 } -->> { "id" : 20 } on : shard2 Timestamp(3, 0) 
                        { "id" : 20 } -->> { "id" : { "$maxKey" : 1 } } on : shard3 Timestamp(3, 1) 
7、添加分片成员
#创建好分片副本节点后,随机登录一台mongos
mongo --port 20000

#添加分片
sh.addShard("shard1/192.168.3.23:10001,192.168.3.24:10002")

参考:https://www.cnblogs.com/ityouknow/p/7344005.html

上一篇 下一篇

猜你喜欢

热点阅读