上一篇簡單說了一下mysql主從複製的配置,可是僅僅只是作了主從複製,應用服務器還須要根據業務來判斷讀邏輯與寫邏輯,還須要作負載均衡...php
爲解決此問題,此篇文章主要來介紹一種中間件Mycatjava
主機 | 端口 | 容器名稱 | 容器名稱 |
---|---|---|---|
192.168.1.18 | 3306 | percona-master01 | master |
192.168.1.18 | 3307 | percona-slave01 | slave |
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
<system>
<property name="nonePasswordLogin">0</property>
<property name="useHandshakeV10">1</property>
<property name="useSqlStat">0</property>
<property name="useGlobleTableCheck">0</property>
<property name="sequnceHandlerType">2</property>
<property name="subqueryRelationshipCheck">false</property>
<property name="processorBufferPoolType">0</property>
<property name="handleDistributedTransactions">0</property>
<property name="useOffHeapForMerge">1</property>
<property name="memoryPageSize">64k</property>
<property name="spillsFileBufferSize">1k</property>
<property name="useStreamOutput">0</property>
<property name="systemReserveMemorySize">384m</property>
<property name="useZKSwitch">false</property>
</system>
<!--這裏是設置的itcast用戶和虛擬邏輯庫-->
<user name="itcast" defaultAccount="true">
<property name="password">yopurpasswor</property>
<property name="schemas">yourschemas</property>
</user>
</mycat:server>
複製代碼
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!--配置數據表-->
<schema name="itcast" checkSQLschema="false" sqlMaxLimit="100">
<table name="tb_ad" dataNode="dn1" rule="mod-long" />
</schema>
<!--配置分片關係-->
<dataNode name="dn1" dataHost="cluster1" database="itcast" />
<!--配置鏈接信息-->
<dataHost name="cluster1" maxCon="1000" minCon="10" balance="3" writeType="1" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="W1" url="192.168.1.18:3306" user="root" password="root">
<readHost host="W1R1" url="192.168.1.18:3307" user="root" password="root" />
</writeHost>
</dataHost>
</mycat:schema>
複製代碼
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<property name="count">1</property>
</function>
複製代碼
此時連接 192.168.1.18:8066便可測試mysql
MySQL集羣1:web
主機 | 端口 | 容器名稱 | 容器名稱 |
---|---|---|---|
192.168.1.18 | 3306 | percona-master01 | master |
192.168.1.18 | 3307 | percona-slave01 | slave |
MySQL集羣2:redis
主機 | 端口 | 容器名稱 | 容器名稱 |
---|---|---|---|
192.168.1.18 | 3316 | percona-master02 | master |
192.168.1.18 | 3317 | percona-slave02 | slave |
#搭建master
#建立目錄
mkdir /data/mysql/master02
cd /data/mysql/master02
mkdir conf data
chmod 777 * -R
#建立配置文件
cd /data/mysql/master02/conf
vim my.cnf
#輸入以下內容
[mysqld]
log-bin=mysql-bin #開啓二進制日誌
server-id=1 #服務id,不可重複
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
#建立容器
docker create --name percona-master02 -v /data/mysql/master02/data:/var/lib/mysql -v /data/mysql/master02/conf:/etc/my.cnf.d -p 3316:3306 -e MYSQL_ROOT_PASSWORD=root percona:5.7.23
#啓動
docker start percona-master02 && docker logs -f percona-master02
#建立同步帳戶以及受權
create user 'itcast'@'%' identified by 'itcast';
grant replication slave on *.* to 'itcast'@'%';
flush privileges;
#查看master狀態
show master status;
複製代碼
#搭建從庫
#建立目錄
mkdir /data/mysql/slave02
cd /data/mysql/slave02
mkdir conf data 6 chmod 777 * -R
#建立配置文件
cd /data/mysql/slave02/conf
vim my.cnf
#輸入以下內容
[mysqld]
server-id=2 #服務id,不可重複 15 sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
#建立容器
docker create --name percona-slave02 -v /data/mysql/slave02/data:/var/lib/mysql -v /data/mysql/slave02/conf:/etc/my.cnf.d -p 3317:3306 -e MYSQL_ROOT_PASSWORD=root percona:5.7.23
#啓動
docker start percona-slave02 && docker logs -f percona-slave02
#設置master相關信息
CHANGE MASTER TO
master_host='192.168.1.18',
master_user='itcast',
master_password='itcast',
master_port=3316,
master_log_file='xxxxxx',
master_log_pos=xxxx;
#啓動同步
start slave;
#查看master狀態
show slave status;
複製代碼
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!--配置數據表-->
<schema name="itcast" checkSQLschema="false" sqlMaxLimit="100">
<table name="tb_ad" dataNode="dn1,dn2" rule="mod-long" />
</schema>
<!--配置分片關係-->
<dataNode name="dn1" dataHost="cluster1" database="itcast" />
<dataNode name="dn2" dataHost="cluster2" database="itcast" />
<!--配置鏈接信息-->
<dataHost name="cluster1" maxCon="1000" minCon="10" balance="3" writeType="1" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="W1" url="192.168.1.18:3306" user="root" password="root">
<readHost host="W1R1" url="192.168.1.18:3307" user="root" password="root" />
</writeHost>
</dataHost>
<dataHost name="cluster2" maxCon="1000" minCon="10" balance="3" writeType="1" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="W2" url="192.168.1.18:3316" user="root" password="root">
<readHost host="W2R1" url="192.168.1.18:3317" user="root" password="root" />
</writeHost>
</dataHost>
</mycat:schema>
複製代碼
<function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<property name="count">2</property>
</function>
複製代碼
./startup_nowrap.sh && tail -f ../logs/mycat.log
複製代碼
cp mycat mycat2 -R
vim wrapper.conf
#設置jmx端口
wrapper.java.additional.7=-Dcom.sun.management.jmxremote.port=1985
vim server.xml
#設置服務端口以及管理端口
<property name="serverPort">8067</property>
<property name="managerPort">9067</property> \
#從新啓動服務
./startup_nowrap.sh
tail -f ../logs/mycat.log
複製代碼
#拉取鏡像
docker pull haproxy:1.9.3
#建立目錄,用於存放配置文件
mkdir /haoke/haproxy
#建立容器
docker create --name haproxy --net host -v /haoke/haproxy:/usr/local/etc/haproxy haproxy:1.9.3
複製代碼
#建立文件
vim /haoke/haproxy/haproxy.cfg
#輸入以下內容
global
log 127.0.0.1 local2
maxconn 4000
daemon
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen admin_stats
bind 0.0.0.0:4001
mode http
stats uri /dbs
stats realm Global\ statistics
stats auth admin:admin123
listen proxy-mysql
bind 0.0.0.0:4002
mode tcp
balance roundrobin
#代理mycat服務
server mycat_1 192.168.1.18:8066 check port 8066 maxconn 2000
server mycat_2 192.168.1.18:8067 check port 8067 maxconn 2000
複製代碼
docker restart haproxy && docker logs -f haproxy
複製代碼
經過web界面進行測試:http://192.168.1.18:4001/dbssql
經過navicat連接HAProxy查看數據docker