您的位置:首页 > 数据库 > Mongodb

Mongodb 3.4配置搭建高可用集群(2)

2017-08-26 14:44 399 查看
环境准备Centos6.5

三台服务器:

10.68.17.106、10.68.17.109、10.68.17.110

端口分配:

mongos:20000、config:21000、shard1:27001、shard2:27002、shard3:27003

分别为每台机器建立mongos、config、shard1、shard2、shard3这些目录,因为mongos不存储数据,只需要建立日志文件目录即可。

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
1、config server配置服务器
mongodb3.4以后版本要求配置服务器也创建副本集,不然集群搭建不成功。

添加配置文件:

[root@localhost ~]# vim /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
[root@localhost ~]# mongod -f /usr/local/mongodb/conf/config.conf
登录任意一台配置服务器,初始化配置副本集
[root@localhost ~]# mongo --port 21000
#config变量
config = {
...    _id : "configs",
...     members : [
...         {_id : 0, host : "10.68.17.106:21000" },
...         {_id : 1, host : "10.68.17.109:21000" },
...         {_id : 2, host : "10.68.17.110:21000" }
...     ]
... }

#初始化副本集
rs.initiate(config)
其中,"_id":"configs"应与配置文件中配置的replicaction.replSetName一致,"members"中的"host"为三个节点的ip和port。

2、配置分片副本集(三台机器)
设置第一个分片副本集

配置文件

[root@localhost ~]# /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
[root@localhost ~]# mongod -f /usr/local/mongodb/conf/shard1.conf登录任意一台服务器,初始化副本集
[root@localhost ~]# mongo --port 27001
#使用admin数据库
use admin
#定义副本集配置,第三个节点的 "arbiterOnly":true 代表其为仲裁节点。
config = {
...    _id : "shard1",
...     members : [
...         {_id : 0, host : "10.68.17.106:27001" },
...         {_id : 1, host : "10.68.17.109:27001" },
...         {_id : 2, host : "10.68.17.110:27001" , arbiterOnly: true }
...     ]
... }
#初始化副本集配置
rs.initiate(config);

设置第二个分片副本集
配置文件

[root@localhost ~]# vim /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
[root@localhost ~]# mongod -f /usr/local/mongodb/conf/shard2.conf登录任意一台服务器,初始化副本集
[root@localhost ~]# mongo --port 27002
#使用admin数据库
use admin
#定义副本集配置
config = {
...    _id : "shard2",
...     members : [
...         {_id : 0, host : "10.68.17.106:27002"  , arbiterOnly: true },
...         {_id : 1, host : "10.68.17.109:27002" },
...         {_id : 2, host : "10.68.17.110:27002" }
...     ]
... }

#初始化副本集配置
rs.initiate(config);
设置第三个分片副本集
配置文件

[root@localhost ~]# vim /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
[root@localhost ~]# mongod -f /usr/local/mongodb/conf/shard3.conf登录任意一台服务器,初始化副本集
[root@localhost ~]# mongo --port 27003
#使用admin数据库
use admin
#定义副本集配置
config = {
...    _id : "shard3",
...     members : [
...         {_id : 0, host : "10.68.17.106:27003" },
...         {_id : 1, host : "10.68.17.109:27003" , arbiterOnly: true},
...         {_id : 2, host : "10.68.17.110:27003" }
...     ]
... }

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


3、配置路由服务器mongos(三台机器)
先启动配置服务器和分片服务器,后启动路由实例。

[root@localhost ~]# vim /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/10.68.17.106:21000,10.68.17.109:21000,10.68.17.110:21000

#设置最大连接数
maxConns=20000

启动三台服务器的mongos server
[root@localhost ~]# mongos -f /usr/local/mongodb/conf/mongos.conf
4、启用分片
目前搭建了mongodb配置服务器、路由服务器、各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。

登录任意一台mongos

[root@localhost ~]# mongo --port 20000
#使用admin数据库
user  admin
#串联路由服务器与分配副本集
sh.addShard("shard1/10.68.17.106:27001,10.68.17.109:27001,10.68.17.110:27001")
sh.addShard("shard2/10.68.17.106:27002,10.68.17.109:27002,10.68.17.110:27002")
sh.addShard("shard3/10.68.17.106:27003,10.68.17.109:27003,10.68.17.110:27003")
#查看集群状态
sh.status()
目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但是我们的目的是希望插入数据,数据能够自动分片。连接在mongos上,准备让指定的数据库、指定的集合分片生效。
#指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
#指定数据库里需要分片的集合和片键
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: