配置参考文章:https://www.cnblogs.com/kevingrace/p/9365840.html
服务器划分:
11.11.141.37 从(只读)
11.11.141.38 主(读写-主)
11.11.141.39 主(读写-备)
服务器全部安装mysql
mycat安装在38、39上,进行负载均衡操作
11.11.141.38 主(读写-主)
11.11.141.39 主(读写-备)
1、mysql安装
docker run -d
-v /home/kygl/docker/mysql/conf:/etc/mysql/conf.d:Z
-v /home/kygl/docker/mysql/data:/var/lib/mysql:z
-v /home/kygl/docker/mysql/logs:/log:z
-p 3306:3306
--name mysql
-e MYSQL_ROOT_PASSWORD=MYSQLPTRkygl@37
10.27.213.66:5000/mysql:latest
docker run -d
-v /home/kygl/docker/mysql/conf:/etc/mysql/conf.d:Z
-v /home/kygl/docker/mysql/data:/var/lib/mysql:z
-v /home/kygl/docker/mysql/logs:/log:z
-p 3306:3306
--name mysql
-e MYSQL_ROOT_PASSWORD=MYSQLPTRkygl@38
10.27.213.66:5000/mysql:latest
docker run -d
-v /home/kygl/docker/mysql/conf:/etc/mysql/conf.d:Z
-v /home/kygl/docker/mysql/data:/var/lib/mysql:z
-v /home/kygl/docker/mysql/logs:/log:z
-p 3306:3306
--name mysql
-e MYSQL_ROOT_PASSWORD=MYSQLPTRkygl@39
10.27.213.66:5000/mysql:latest
2、 my.cnf配置
37
[mysql]
default-character-set = utf8
[mysqld]
#设置server-id 必须唯一
server-id=37
read_only=1
character_set_server = utf8
lower_case_table_names = 1
gtid-mode=on
enforce-gtid-consistency=true
#避免relay.info更新不及时,SLAVE 重启后导致的主从复制出错,,在从服务器上实现事故安全功能,增加配置:
master_info_repository=TABLE
relay_log_info_repository=TABLE
[mysql.server]
default-character-set = utf8
[mysqld_safe]
default-character-set = utf8
[client]
default-character-set=utf8
38
[mysql]
default-character-set = utf8
[mysqld]
log-bin=mysql-bin #开启二进制日志
server-id=38 #设置server-id
gtid-mode=on
enforce-gtid-consistency=true
#步进值auto_imcrement。一般有n台主MySQL就填n
auto_increment_increment=2
#起始值。一般填第n台主MySQL。此时为第一台主MySQL
auto_increment_offset=1
# 不同步那些数据库
binlog-ignore-db = mysql,information_schema
# 只同步那些数据库,除此之外,其他不同步
#binlog-do-db = test_mysql
#要同步的数据库,默认所有库
#replicate-do-db=aa
character_set_server = utf8
lower_case_table_names = 1
[mysql.server]
default-character-set = utf8
[mysqld_safe]
default-character-set = utf8
[client]
default-character-set=utf8
[mysql]
default-character-set = utf8
[mysqld]
log-bin=mysql-bin #开启二进制日志
server-id=39 #设置server-id
gtid-mode=on
enforce-gtid-consistency=true
#步进值auto_imcrement。一般有n台主MySQL就填n
auto_increment_increment=2
#起始值。一般填第n台主MySQL。此时为第一台主MySQL
auto_increment_offset=2
# 不同步那些数据库
binlog-ignore-db = mysql,information_schema
# 只同步那些数据库,除此之外,其他不同步
#binlog-do-db = test_mysql
#要同步的数据库,默认所有库
#replicate-do-db=aa
character_set_server = utf8
lower_case_table_names = 1
[mysql.server]
default-character-set = utf8
[mysqld_safe]
default-character-set = utf8
[client]
default-character-set=utf8
3、主服务器创建同步账号
38
用户: kyglmaster
密码: PTRkygl@master
创建用户:
CREATE USER 'kyglmaster'@'%' IDENTIFIED BY 'PTRkygl@master';
分配权限:
GRANT REPLICATION SLAVE ON . TO 'kyglmaster'@'%';
刷新权限:
flush privileges; #刷新权限
39
用户: kyglmaster
密码: PTRkygl@master
创建用户:
CREATE USER 'kyglmaster'@'%' IDENTIFIED BY 'PTRkygl@master';
分配权限:
GRANT REPLICATION SLAVE ON . TO 'kyglmaster'@'%';
刷新权限:
flush privileges; #刷新权限
4、配置主主连接
38
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000001 | 774 | | mysql,information_schema | |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)
CHANGE MASTER TO MASTER_HOST='11.11.141.39',MASTER_PORT=3306,MASTER_USER='kyglmaster',MASTER_PASSWORD='PTRkygl@master',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=774;
start slave;
39
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000001 | 774 | | mysql,information_schema | |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)
CHANGE MASTER TO MASTER_HOST='11.11.141.38',MASTER_PORT=3306,MASTER_USER='kyglmaster',MASTER_PASSWORD='PTRkygl@master',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=774;
start slave;
5、配置从服务器
37
CHANGE MASTER TO MASTER_HOST='11.11.141.38',MASTER_USER='kyglmaster', MASTER_PASSWORD='PTRkygl@master',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=774 FOR CHANNEL 'master38';
CHANGE MASTER TO MASTER_HOST='11.11.141.39',MASTER_USER='kyglmaster', MASTER_PASSWORD='PTRkygl@master',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=774 FOR CHANNEL 'master39';
6.配置38上mycat
vim /usr/local/mycat/conf/server.xml 编辑user配置
<user name="root" defaultAccount="true">
<property name="password">MYSQLPTRkygl@59</property>
<property name="schemas">esp_admin,esp_application,esp_food,esp_quartz,esp_config,esp_workflow</property>
</user>
编辑/usr/local/mycat/conf/schema.xml 编辑schema数据库实体对应的mycat表,例如
<schema name="esp_config" checkSQLschema="false" sqlMaxLimit="100">
<!-- esp_config -->
<table name="cloud_config" primaryKey="id" dataNode="esp_config" />
<table name="test" primaryKey="id" dataNode="esp_config" />
<!--esp_config视图-->
<table name="v_userinfo" primaryKey="id" dataNode="esp_config" />
<!-- esp_config -->
</schema>
然后编辑dataNode(分片信息,也就是分库相关配置)字段,添加数据库
<!-- 连接的数据库 -->
<dataNode name="esp_admin" dataHost="mysql_3306" database="esp_admin" />
<dataNode name="esp_application" dataHost="mysql_3306" database="esp_application" />
<dataNode name="esp_food" dataHost="mysql_3306" database="esp_food" />
<dataNode name="esp_quartz" dataHost="mysql_3306" database="esp_quartz" />
<dataNode name="esp_config" dataHost="mysql_3306" database="esp_config" />
<dataNode name="esp_workflow" dataHost="mysql_3306" database="esp_workflow" />
编辑dataHost(物理数据库)信息
<dataHost name="mysql_3306" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostm38" url="11.11.141.38:3306" user="root" password="MYSQLPTRkygl@38">
<readHost host="hostm38" url="11.11.141.38:3306" user="root" password="MYSQLPTRkygl@38" />
<readHost host="hosts37" url="11.11.141.37:3306" user="root" password="MYSQLPTRkygl@37" />
</writeHost>
<writeHost host="hostm39" url="11.11.141.39:3306" user="root" password="MYSQLPTRkygl@39">
<readHost host="hostm39" url="11.11.141.39:3306" user="root" password="MYSQLPTRkygl@39" />
<readHost host="hosts37" url="11.11.141.37:3306" user="root" password="MYSQLPTRkygl@37" />
</writeHost>
</dataHost>
- name属性唯一标识dataHost标签,供上层的标签使用。
- maxCon属性指定每个读写实例连接池的最大连接。也就是说,标签内嵌套的
- writeHost、readHost标签都会使用这个属性的值来实例化出连接池的最大连接数。
- minCon属性指定每个读写实例连接池的最小连接,初始化连接池的大小。
- writeType="0" 所有写操作发送到配置的第一个 writeHost,第一个挂了切到还生存的第二个writeHost,重新启动后已切换后的为准,切换记录在配置文件中:dnindex.properties .
- balance : 均衡负载的方式.balance="1": 读请求随机分发到当前writeHost对应的readHost和standby的writeHost上
- switchType="1" 第一个writehost失败后转向第二个writehost
最后启动mycat:/usr/local/mycat/bin/mycat start
39上配置和上面配置相同,用作负载均衡。
7.配置mycat高可用负载均衡
在keepalived配置文件中添加如下配置
virtual_server 11.11.141.59 8066 {
delay_loop 1 # 设置健康检查时间,单位是秒
lb_algo wlc # 负载均衡调度算法,一般用wrr、rr、wlc
lb_kind DR # 设置LVS实现负载的机制,有NAT、TUN、DR三个模式
persistence_timeout 1 # 会话保持时间。把用户请求请求间隔在未超过保持时间时,一直分发到某个服务节点
protocol TCP # 转发协议 有TCP和UDP两种
real_server 11.11.141.38 8066 { # 指定real server1的IP地址
weight 1 # 配置节点权值,数字越大权重越高
TCP_CHECK {
connect_timeout 5 #连接超时时间
nb_get_retry 3 # 重连次数
delay_before_retry 1 # 重连时间间隔
connect_port 8066 #检测端口
}
}
real_server 11.11.141.39 8066 { # 指定real server1的IP地址
weight 1 # 配置节点权值,数字越大权重越高
TCP_CHECK {
connect_timeout 5 #连接超时时间
nb_get_retry 3 # 重连次数
delay_before_retry 1 # 重连时间间隔
connect_port 8066 #检测端口
}
}
}
开启路由转发:echo "1" >/proc/sys/net/ipv4/ip_forward
脚本如下
在mycat服务器上添加lo:0路由并设置开机自启动
#!/bin/bash
VIP=11.11.141.59 #这个为你要设置的VIP
case $1 in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $IP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
添加执行权限并执行该脚本添加虚拟路由:
chmod +x /home/kygl/script/vip.sh
bash /home/kygl/script/vip.sh
设置脚本开机自启动
chmod +x /etc/rc.d/rc.local
echo "bash /home/kygl/script/vip.sh start" >>/etc/rc.d/rc.local
网友评论