主机IP信息

hostname IP
172.16.3.140 haproxy01
172.16.3.141 haproxy02
172.16.3.142 mycat01
172.16.3.143 mycat02
172.16.3.152 mha
172.16.3.153 master
172.16.3.154 slave01
172.16.3.155 slave02

mysql 安装:http://www.cnblogs.com/jenvid/p/8516781.html

mycat安装

mycat部署到节点

  • 172.16.3.142 mycat01
  • 172.16.3.143 mycat02
1.部署mycat

https://segmentfault.com/a/1190000009520414

https://www.cnblogs.com/raphael5200/p/5884931.html

https://www.cnblogs.com/hk315523748/p/6094656.html

http://www.roncoo.com/course/view/f614343765bc4aac8597c6d8b38f06fd

https://www.cnblogs.com/conanwang/p/5961019.html

1.1解压复制到/usr/local/mycat

http://dl.mycat.io/

1.2安装jdk

https://blog.csdn.net/linlinv3/article/details/45060705

  • 1)下载jdkwget http://dl.mycat.io/jdk-8u20-linux-x64.tar.gz
  • 2)解压tar -zxvf jdk-8u20-linux-x64.tar.gz -C /usr/lib
  • 3)解压后拷贝到lib目录cp -rp jdk1.8.0_20 /usr/lib
  • 4)配置环境变量
  1. vi /etc/profile
  2. export JAVA_HOME=/usr/lib/jdk1.8.0_20
  3. export JRE_HOME=$JAVA_HOME/jre
  4. export CLASSPATH=.:$JAVA_HOME/lib:$JER_HOME/lib:$CLASSPATH
  5. export PATH=$JAVA_HOME/bin:$JER_HOME/bin:$PATH:/usr/local/mycat/bin
  6. export MYCAT_HOME=/usr/local/mycat
  7. source /etc/profile
  8. java -version
  9. [root@mycat01 jdk1.8.0_20]# java -version
  10. java version "1.8.0_20"
  11. Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
  12. Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
2.配置mycat server.xml

grant select on *.* to 'readonly'@'172.16.3.%' identified by 'readonly';

server.xml

  • user定义的是mycat服务端口8066和管理端口9066的登录信息,和后端mysql授权没关系
  • schemas定义的是mycat的逻辑DB名称,必须和schema.xml的schema name=必须一致
  1. <property name="txlsolation">2</property>
  2. </system>
  3. <user name="mycatdb">
  4. <property name="password">1234567</property>
  5. <property name="schemas">test</property>
  6. </user>
  7. <user name="readonly">
  8. <property name="password">readonly1</property>
  9. <property name="schemas">test</property>
  10. <property name="readOnly">true</property>
  11. </user>
  12. </mycat:server>
3.配置mycat schema

schema.xml

  • schema name=必须和server.xml的schema一致
  • dataNode="dn_tpcc100"指定需要使用的数据节点
  • dataHost指定数据节点
  1. <?xml version="1.0"?>
  2. <!DOCTYPE mycat:schema SYSTEM "schema.dtd">
  3. <mycat:schema xmlns:mycat="http://org.opencloudb/" >
  4. <schema name="test" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn_test"> </schema>
  5. <dataNode name="dn_test" dataHost="dh_3.151" database="test" />
  6. <dataHost name="dh_3.151" maxCon="1000" minCon="10" balance="1"
  7. writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
  8. <heartbeat>show slave status</heartbeat>
  9. <!-- can have multi write hosts -->
  10. <writeHost host="Master_vip" url="172.16.3.151:3306" user="root" password="123456">
  11. <!-- can have multi read hosts -->
  12. <readHost host="S1_3.154" url="172.16.3.154:3306" user="root" password="123456" />
  13. <readHost host="S2_3.155" url="172.16.3.155:3306" user="root" password="123456" />
  14. </writeHost>
  15. <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
  16. </dataHost>
  17. </mycat:schema>

writeHost和readhost如果填写的用户名不一样,所有的读写将发送到writehost,balance不起作用

4.启动mycat./mycat start
  1. #因为磁盘满了,主库创建用户语句还没在从库生效
  2. Master_SSL_Key:
  3. Seconds_Behind_Master: 170301
  4. Master_SSL_Verify_Server_Cert: No
  5. Last_IO_Errno: 0
5.检查端口
  1. [root@mycat01 bin]# netstat -lnpt
  2. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  3. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 880/sshd
  4. tcp 0 0 127.0.0.1:32000 0.0.0.0:* LISTEN 1418/java
  5. tcp6 0 0 :::8066 :::* LISTEN 1418/java
  6. tcp6 0 0 :::9066 :::* LISTEN 1418/java
  7. tcp6 0 0 :::55407 :::* LISTEN 1418/java
  8. tcp6 0 0 :::35574 :::* LISTEN 1418/java
  9. tcp6 0 0 :::22 :::* LISTEN 880/sshd
  10. tcp6 0 0 :::1984 :::* LISTEN 1418/java

https://blog.csdn.net/kefengwang/article/details/54233390

6.登录读写分离服务8066
  • 这里以server.xml的定义的用户登录
  • 显示的是mycat的逻辑库
  1. [root@mycat01 ~]# mysql -uroot -p1234567 -h127.0.0.1 -P 8066
  2. Warning: Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 3
  5. Server version: 5.5.8-mycat-1.5.1-RELEASE-20160525110043 MyCat Server (OpenCloundDB)
  6. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. root@127.0.0.1 12:04: [(none)]> show databases;
  12. +----------+
  13. | DATABASE |
  14. +----------+
  15. | db1 |
  16. | test |
  17. | tpcc100 |
  18. +----------+
  19. 3 rows in set (0.00 sec)
  20. root@127.0.0.1 12:04: [(none)]>
7.登录mycat管理端9066
  • show @@heartbeat;里面显示的name是在schema.xml里面定义

    • RS_CODE=1表示心跳正常
  1. [root@mycat01 bin]# mysql -uroot -p1234567 -h127.0.0.1 -P 9066
  2. Warning: Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 1
  5. Server version: 5.5.8-mycat-1.5.1-RELEASE-20160525110043 MyCat Server (monitor)
  6. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. root@127.0.0.1 12:06: [(none)]> show @@heartbeat;
  12. +------------+-------+--------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
  13. | NAME | TYPE | HOST | PORT | RS_CODE | RETRY | STATUS | TIMEOUT | EXECUTE_TIME | LAST_ACTIVE_TIME | STOP |
  14. +------------+-------+--------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
  15. | Master_vip | mysql | 172.16.3.151 | 3306 | 1 | 0 | idle | 0 | 0,0,0 | 2018-03-26 12:06:53 | false |
  16. | S1_3.154 | mysql | 172.16.3.154 | 3306 | 1 | 0 | idle | 0 | 0,0,0 | 2018-03-26 12:06:53 | false |
  17. | S2_3.155 | mysql | 172.16.3.155 | 3306 | 1 | 0 | idle | 0 | 0,0,0 | 2018-03-26 12:06:53 | false |
  18. +------------+-------+--------------+------+---------+-------+--------+---------+--------------+---------------------+-------+
  19. 3 rows in set (0.01 sec)
  20. root@127.0.0.1 12:07: [(none)]> show @@datasource;
  21. +------------+------------+-------+--------------+------+------+--------+------+------+---------+
  22. | DATANODE | NAME | TYPE | HOST | PORT | W/R | ACTIVE | IDLE | SIZE | EXECUTE |
  23. +------------+------------+-------+--------------+------+------+--------+------+------+---------+
  24. | dn_db1 | Master_vip | mysql | 172.16.3.151 | 3306 | W | 0 | 15 | 1000 | 244 |
  25. | dn_db1 | S1_3.154 | mysql | 172.16.3.154 | 3306 | R | 0 | 11 | 1000 | 234 |
  26. | dn_db1 | S2_3.155 | mysql | 172.16.3.155 | 3306 | R | 0 | 11 | 1000 | 235 |
  27. | dn_test | Master_vip | mysql | 172.16.3.151 | 3306 | W | 0 | 15 | 1000 | 244 |
  28. | dn_test | S1_3.154 | mysql | 172.16.3.154 | 3306 | R | 0 | 11 | 1000 | 234 |
  29. | dn_test | S2_3.155 | mysql | 172.16.3.155 | 3306 | R | 0 | 11 | 1000 | 235 |
  30. | dn_tpcc100 | Master_vip | mysql | 172.16.3.151 | 3306 | W | 0 | 15 | 1000 | 244 |
  31. | dn_tpcc100 | S1_3.154 | mysql | 172.16.3.154 | 3306 | R | 0 | 11 | 1000 | 234 |
  32. | dn_tpcc100 | S2_3.155 | mysql | 172.16.3.155 | 3306 | R | 0 | 11 | 1000 | 235 |
  33. +------------+------------+-------+--------------+------+------+--------+------+------+---------+
  34. 9 rows in set (0.00 sec)
8.mycat读写分离验证
  • 1)修改mycat日志等级为debug,重启mycat生效
  1. vi /usr/local/mycat/conf/log4j.xml
  2. <level value="debug" />
  • 2)以mycat的root用户创建表插入数据
  1. [root@mycat01 ~]# mysql -uroot -p1234567 -h127.0.0.1 -P 8066
  2. Warning: Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 5
  5. Server version: 5.5.8-mycat-1.5.1-RELEASE-20160525110043 MyCat Server (OpenCloundDB)
  6. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. root@127.0.0.1 12:12: [(none)]> select user();
  12. +----------------+
  13. | USER() |
  14. +----------------+
  15. | root@127.0.0.1 |
  16. +----------------+
  17. 1 row in set (0.00 sec)
  18. root@127.0.0.1 12:12: [(none)]> show databases;
  19. +----------+
  20. | DATABASE |
  21. +----------+
  22. | db1 |
  23. | test |
  24. | tpcc100 |
  25. +----------+
  26. 3 rows in set (0.01 sec)
  27. root@127.0.0.1 12:12: [(none)]> use db1;
  28. Database changed
  29. root@127.0.0.1 12:12: [db1]> show tables;
  30. +---------------+
  31. | Tables_in_db1 |
  32. +---------------+
  33. | t1 |
  34. | t2 |
  35. | t3 |
  36. +---------------+
  37. 3 rows in set (0.01 sec)
  38. create table t4 (id int not null primary key,user_id varchar(20),user_name varchar(20),date DATE);
  39. insert into t4 values(1,'001','yzw1',20180326);
  40. insert into t4 values(2,'002','yzw2',20180326);
  41. insert into t4 values(3,'003','yzw3',20180326);
  • 在mycat.log中可以看到路由信息
  1. 03/26 12:17:23.925 DEBUG [$_NIOREACTOR-1-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=7, lastTime=1522037843904, user=root, schema=db1
  2. , old shema=db1, borrowed=true, fromSlaveDB=false, threadId=183, charset=utf8, txIsolation=3, autocommit=true, attachment=dn_db1{insert into t4 values(3,'003','yzw3
  3. ',20180326)}, respHandler=SingleNodeHandler [node=dn_db1{insert into t4 values(3,'003','yzw3',20180326)}, packetId=1], host=172.16.3.151, port=3306, statusSync=null
  4. , writeQueue=0, modifiedSQLExecuted=true]
  • 读的时候则选择从库路由
  1. 03/26 13:27:24.531 DEBUG [$_NIOREACTOR-0-RW] (NonBlockingSession.java:229) -release connection MySQLConnection [id=35, lastTime=1522042044524, user=readonly, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=true, threadId=83, charset=utf8, txIsolation=3, autocommit=true, attachment=dn_db1{select * from db1}, respHandler=SingleNodeHandler [node=dn_db1{select * from db1}, packetId=1], host=172.16.3.155, port=3306, statusSync=org.opencloudb.mysql.nio.MySQLConnection$StatusSync@c7c82a3, writeQueue=0, modifiedSQLExecuted=false]
  2. 03/26 13:27:24.531 DEBUG [$_NIOREACTOR-0-RW] (PhysicalDatasource.java:403) -release channel MySQLConnection [id=35, lastTime=1522042044524, user=readonly, schema=db1, old shema=db1, borrowed=true, fromSlaveDB=true, threadId=83, charset=utf8, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=172.16.3.155, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
  • 只读账号进行写数据则报错
  1. readonly@127.0.0.1 13:28: [db1]> create table t5 (id int);
  2. ERROR 1495 (HY000): User readonly
  3. readonly@127.0.0.1 13:28: [db1]>

keepalived安装

keepalvied部署到节点

  • 172.16.3.140 haproxy01
  • 172.16.3.141 haproxy02
1.下载解压编译安装

http://www.keepalived.org/download.html

  1. wget http://www.keepalived.org/software/keepalived-1.4.2.tar.gz
  2. yum install curl gcc openssl-devel libnl3-devel net-snmp-devel libnfnetlink-devel
  3. tar -zxvf keepalived-1.4.2.tar.gz
  4. cd keepalived-1.4.2
  5. ./configure --prefix=/usr/local/keepalived-1.4.2
  6. make && make install

http://www.keepalived.org/doc/installing_keepalived.html

2.拷贝文件
  1. cp -rp /root/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d
  2. cp -rp /root/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
  3. ln -s /usr/local/keepalived-1.4.2/sbin/keepalived /usr/sbin
  4. cp -rp /usr/local/keepalived-1.4.2/etc/keepalived/keepalived.conf /etc/keepalived
  5. systemctl enable keepalived
  6. ln -s /usr/local/keepalived-1.4.2 /usr/local/keepalived/
  7. mkdir /usr/local/keepalived/log/
3.增加配置/etc/keepalived/keepalived.conf
  • master
  1. ! Configuration File for keepalived
  2. global_defs {
  3. ## keepalived 自带的邮件提醒需要开启 sendmail 服务。建议用独立的监控或第三方 SMTP
  4. router_id haproxy02 ## 标识本节点的字条串,通常为 hostname
  5. }
  6. ## keepalived 会定时执行脚本并对脚本执行的结果进行分析,动态调整 vrrp_instance 的优先级。
  7. ## 如果脚本执行结果为 0,并且 weight 配置的值大于 0,则优先级相应的增加。
  8. ## 如果脚本执行结果非 0,并且 weight 配置的值小于 0,则优先级相应的减少。
  9. ## 其他情况,维持原本配置的优先级,即配置文件中 priority 对应的值。
  10. vrrp_script chk_haproxy {
  11. script "/etc/keepalived/haproxy_check.sh" ## 检测 haproxy 状态的脚本路径
  12. interval 2 ## 检测时间间隔
  13. weight 2 ## 如果条件成立,权重+2
  14. }
  15. ## 定义虚拟路由, VI_1 为虚拟路由的标示符,自己定义名称
  16. vrrp_instance VI_1 {
  17. state BACKUP ## 默认主设备(priority 值大的)和备用设备(priority 值小的)都设置为 BACKUP,
  18. ## 由 priority 来控制同时启动情况下的默认主备,否则先启动的为主设备
  19. interface eth0 ## 绑定虚拟 IP 的网络接口,与本机 IP 地址所在的网络接口相同,我的是 eth0
  20. virtual_router_id 35 ## 虚拟路由的 ID 号,两个节点设置必须一样,可选 IP 最后一段使用,
  21. ## 相同的 VRID 为一个组,他将决定多播的 MAC 地址
  22. priority 180 ## 节点优先级,值范围 0-254, MASTER 要比 BACKUP 高
  23. nopreempt ## 主设备(priority 值大的)配置一定要加上 nopreempt,否则非抢占也不起作用
  24. advert_int 1 ## 组播信息发送间隔,两个节点设置必须一样,默认 1s
  25. ## 设置验证信息,两个节点必须一致
  26. authentication {
  27. auth_type PASS
  28. auth_pass 180326 ## 真实生产,按需求对应该过来
  29. }
  30. ## 将 track_script 块加入 instance 配置块
  31. track_script {
  32. chk_haproxy ## 检查 HAProxy 服务是否存活
  33. }
  34. ## 虚拟 IP 池, 两个节点设置必须一样
  35. virtual_ipaddress {
  36. 172.16.3.144 ## 虚拟 ip,可以定义多个,每行一个
  37. }
  38. }
  • backup
  1. ! Configuration File for keepalived
  2. global_defs {
  3. ## keepalived 自带的邮件提醒需要开启 sendmail 服务。建议用独立的监控或第三方 SMTP
  4. router_id haproxy01 ## 标识本节点的字条串,通常为 hostname
  5. }
  6. ## keepalived 会定时执行脚本并对脚本执行的结果进行分析,动态调整 vrrp_instance 的优先级。
  7. ## 如果脚本执行结果为 0,并且 weight 配置的值大于 0,则优先级相应的增加。
  8. ## 如果脚本执行结果非 0,并且 weight 配置的值小于 0,则优先级相应的减少。
  9. ## 其他情况,维持原本配置的优先级,即配置文件中 priority 对应的值。
  10. vrrp_script chk_haproxy {
  11. script "/etc/keepalived/haproxy_check.sh" ## 检测 haproxy 状态的脚本路径
  12. interval 2 ## 检测时间间隔
  13. weight 2 ## 如果条件成立,权重+2
  14. }
  15. ## 定义虚拟路由, VI_1 为虚拟路由的标示符,自己定义名称
  16. vrrp_instance VI_1 {
  17. state BACKUP ## 默认主设备(priority 值大的)和备用设备(priority 值小的)都设置为 BACKUP,
  18. ## 由 priority 来控制同时启动情况下的默认主备,否则先启动的为主设备
  19. interface eth0 ## 绑定虚拟 IP 的网络接口,与本机 IP 地址所在的网络接口相同,我的是 eth0
  20. virtual_router_id 35 ## 虚拟路由的 ID 号,两个节点设置必须一样,可选 IP 最后一段使用,
  21. ## 相同的 VRID 为一个组,他将决定多播的 MAC 地址
  22. priority 120 ## 节点优先级,值范围 0-254, MASTER 要比 BACKUP 高
  23. nopreempt ## 主设备(priority 值大的)配置一定要加上 nopreempt,否则非抢占也不起作用
  24. advert_int 1 ## 组播信息发送间隔,两个节点设置必须一样,默认 1s
  25. ## 设置验证信息,两个节点必须一致
  26. authentication {
  27. auth_type PASS
  28. auth_pass 180326 ## 真实生产,按需求对应该过来
  29. }
  30. ## 将 track_script 块加入 instance 配置块
  31. track_script {
  32. chk_haproxy ## 检查 HAProxy 服务是否存活
  33. }
  34. ## 虚拟 IP 池, 两个节点设置必须一样
  35. virtual_ipaddress {
  36. 172.16.3.144 ## 虚拟 ip,可以定义多个,每行一个
  37. }
  38. }
4.检查脚本
  1. vi /etc/keepalived/haproxy_check.sh
  2. #!/bin/bash
  3. START_HAPROXY="/etc/rc.d/init.d/haproxy start"
  4. STOP_HAPROXY="/etc/rc.d/init.d/haproxy stop"
  5. LOG_FILE="/usr/local/keepalived/log/haproxy-check.log"
  6. HAPS=`ps -C haproxy --no-header |wc -l`
  7. date "+%Y-%m-%d %H:%M:%S" >> $LOG_FILE
  8. echo "check haproxy status" >> $LOG_FILE
  9. if [ $HAPS -eq 0 ];then
  10. echo $START_HAPROXY >> $LOG_FILE
  11. $START_HAPROXY >> $LOG_FILE 2>&1
  12. sleep 3
  13. if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
  14. echo "start haproxy failed, killall keepalived" >> $LOG_FILE
  15. killall keepalived
  16. fi
  17. fi
5.授权检查脚本chmod +x /etc/keepalived/haproxy_check.sh

https://blog.csdn.net/l1028386804/article/details/76397064

6.在2个节点上启动keepalivedsystemctl start keepalived
  1. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
  2. link/ether 00:50:56:a3:d0:2e brd ff:ff:ff:ff:ff:ff
  3. inet 172.16.3.140/24 brd 172.16.3.255 scope global eth0
  4. valid_lft forever preferred_lft forever
  5. inet 172.16.3.144/32 scope global eth0
  6. valid_lft forever preferred_lft forever
  7. inet6 fe80::250:56ff:fea3:d02e/64 scope link
  8. valid_lft forever preferred_lft forever
7.切换VIP测试
  1. [root@haproxy01 keepalived]# ip a
  2. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
  3. link/ether 00:50:56:a3:d0:2e brd ff:ff:ff:ff:ff:ff
  4. inet 172.16.3.140/24 brd 172.16.3.255 scope global eth0
  5. valid_lft forever preferred_lft forever
  6. inet 172.16.3.144/32 scope global eth0
  7. valid_lft forever preferred_lft forever
  8. inet6 fe80::250:56ff:fea3:d02e/64 scope link
  9. valid_lft forever preferred_lft forever
  10. [root@haproxy01 keepalived]# systemctl stop keepalived
  11. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
  12. link/ether 00:50:56:a3:d0:2e brd ff:ff:ff:ff:ff:ff
  13. inet 172.16.3.140/24 brd 172.16.3.255 scope global eth0
  14. valid_lft forever preferred_lft forever
  15. inet6 fe80::250:56ff:fea3:d02e/64 scope link
  16. valid_lft forever preferred_lft forever
  17. [root@haproxy02 keepalived]# ip a
  18. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
  19. link/ether 00:50:56:a3:fe:0a brd ff:ff:ff:ff:ff:ff
  20. inet 172.16.3.141/24 brd 172.16.3.255 scope global eth0
  21. valid_lft forever preferred_lft forever
  22. inet 172.16.3.144/32 scope global eth0
  23. valid_lft forever preferred_lft forever
  24. inet6 fe80::250:56ff:fea3:fe0a/64 scope link
  25. valid_lft forever preferred_lft forever

haproxy安装

mycat部署到节点

  • 172.16.3.140 haproxy01
  • 172.16.3.141 haproxy02
1.部署haproxy

https://www.cnblogs.com/zhangs1986/p/6517788.html

  1. wget http://www.haproxy.org/download/1.7/src/haproxy-1.7.3.tar.gz
  2. [root@haproxy01 haproxy-1.7.3]# uname -a
  3. Linux haproxy01 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
  4. make TARGET=linux3100 CPU=x86_64 PREFIX=/usr/local/haproxy-1.7.3
  5. make install PREFIX=/usr/local/haproxy-1.7.3
2.配置haproxy
  • 2.1创建配置文件目录
  1. mkdir -p /usr/local/haproxy-1.7.3/conf
  • 2.2创建配置文件目录
  1. mkdir -p /etc/haproxy
  • 2.3创建配置文件
  1. touch /usr/local/haproxy-1.7.3/conf/haproxy.cfg
  • 2.4添加配置文件软连接
  1. ln -s /usr/local/haproxy-1.7.3/conf/haproxy.cfg /etc/haproxy/haproxy.cfg
  • 2.5拷贝错误页面
  1. cp -r /root/haproxy-1.7.3/examples/errorfiles /usr/local/haproxy-1.7.3/errorfiles
  • 2.6添加软连接
  1. ln -s /usr/local/haproxy-1.7.3/errorfiles /etc/haproxy/errorfiles
  • 2.7创建日志文件目录
  1. mkdir -p /usr/local/haproxy-1.7.3/log
  • 2.8创建日志文件
  1. touch /usr/local/haproxy-1.7.3/log/haproxy.log
  • 2.9添加软连接
  1. ln -s /usr/local/haproxy-1.7.3/log/haproxy.log /var/log/haproxy.log
  • 2.10拷贝启动文件
  1. cp /root/haproxy-1.7.3/examples/haproxy.init /etc/rc.d/init.d/haproxy
  • 2.11添加脚本执行权限
  1. chmod +x /etc/rc.d/init.d/haproxy
  • 2.12设置开机启动(6.x)
  1. chkconfig haproxy on
  • 2.13添加软连接
  1. ln -s /usr/local/haproxy-1.7.3/sbin/haproxy /usr/sbin
3.配置参数
3.1haproxy.cfg
  1. vi /usr/local/haproxy-1.7.3/conf/haproxy.cfg
  2. global
  3. chroot /usr/local/haproxy-1.7.3
  4. user haproxy
  5. group haproxy
  6. node haproxy01
  7. description haproxy01
  8. defaults
  9. listen admin_stats
  10. bind 0.0.0.0:8089
  11. stats enable
  12. mode http
  13. log global
  14. stats uri /stats
  15. stats realm Haproxy\ Statistics
  16. stats auth admin:admin
  17. stats admin if TRUE
  18. stats refresh 30s
  19. option httplog
  20. listen mycat_service
  21. bind :3306
  22. mode tcp
  23. option tcplog
  24. option tcpka
  25. balance roundrobin
  26. server mycat01 172.16.3.142:8066 check port 48700 inter 2000ms weight 10 rise 2 fall 3
  27. server mycat02 172.16.3.143:8066 check port 48700 inter 2000ms weight 10 rise 2 fall 3
  28. listen mycat_admin
  29. bind 0.0.0.0:9066
  30. mode tcp
  31. option tcplog
  32. option tcpka
  33. balance roundrobin
  34. server mycat01 172.16.3.142:9066 check port 48700 inter 2000ms weight 10 rise 2 fall 3
  35. server mycat02 172.16.3.143:9066 check port 48700 inter 2000ms weight 10 rise 2 fall 3
  36. errorfile 403 /etc/haproxy/errorfiles/403.http
  37. errorfile 500 /etc/haproxy/errorfiles/500.http
  38. errorfile 502 /etc/haproxy/errorfiles/502.http
  39. errorfile 503 /etc/haproxy/errorfiles/503.http
  40. errorfile 504 /etc/haproxy/errorfiles/504.http
3.1创建haproxy运行用户
  1. groupadd haproxy
  2. useradd -g haproxy haproxy
  3. chown -R haproxy:haproxy /usr/local/haproxy-1.7.3/
3.2配置rsyslog
  1. /etc/rsyslog.conf
  2. # 在 #### RULES #### 上面增加配置
  3. $IncludeConfig /etc/rsyslog.d/*.conf
3.3配置haproxy日志
  1. vi /etc/rsyslog.d/haproxy.conf
  2. #增加下内容:
  3. $ModLoad imudp
  4. $UDPServerRun 514
  5. local2.* /var/log/haproxy.log
  6. & stop
3.4重启rsyslog
  1. systemctl restart rsyslog
3.5配置网络IP转发
  1. vi /etc/sysctl.conf
  2. net.ipv4.ip_forward = 1
  3. sysctl -p
  4. # echo "1" > /proc/sys/net/ipv4/ip_forward
3.6在mycat服务器上增加mycat检查脚本
  • 1)安装xinetdyum install xinetd -y
  • 2)/etc/xinetd.conf 最后增加includedir /etc/xinetd.d
  • 3)增加端口服务
  1. vi /etc/xinetd.d/mycat_status
  2. service mycat_status
  3. {
  4. flags = REUSE
  5. socket_type = stream
  6. port = 48700
  7. wait = no
  8. user = root
  9. server = /usr/local/mycat/bin/mycat_status
  10. log_on_failure += USERID
  11. disable = no
  12. }
  • 4)增加检查脚本
  1. vi /usr/local/mycat/bin/mycat_status
  2. #!/bin/bash
  3. #/usr/local/mycat/bin/mycat_status
  4. # This script checks if a mycat server is healthy running on localhost. It will
  5. # return:
  6. #
  7. # "HTTP/1.x 200 OK\r" (if mycat is running smoothly)
  8. #
  9. # "HTTP/1.x 503 Internal Server Error\r" (else)
  10. mycat=`/usr/local/mycat/bin/mycat status | grep 'not running' | wc -l`
  11. if [ "$mycat" = "0" ];
  12. then
  13. /bin/echo -e "HTTP/1.1 200 OK\r\n"
  14. else
  15. /bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n"
  16. fi
  • 5)增加可执行权限chmod a+x /usr/local/mycat/bin/mycat_status
  • 6)增加服务
  1. vi /etc/services
  2. mycat_status 48700/tcp # mycat_status
  • 7)启动网络服务systemctl start xinetd
  • 8)检查端口状态
  1. [root@mycat01 bin]# netstat -ano|grep 487
  2. tcp6 0 0 :::48700 :::* LISTEN off (0.00/0/0)

https://blog.csdn.net/u012758088/article/details/78654628

  • centos7写入开机启动

https://blog.csdn.net/chenxiabinffff/article/details/51374635

3.7启动haproxy
  1. /etc/init.d/haproxy start
3.8启动报错
  1. Mar 26 13:53:15 localhost haproxy[10067]: Server mycat_servers/mycat01 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
  2. Mar 26 13:53:15 haproxy01 haproxy: [ OK ]
  3. Mar 26 13:53:15 haproxy01 systemd: Started SYSV: HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited for high availability environments..
  4. Mar 26 13:53:16 localhost haproxy[10067]: Server mycat_servers/mycat02 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
  5. Mar 26 13:53:16 localhost haproxy[10067]: proxy mycat_servers has no server available!

因为haproxy.conf里面的48700并没有在mycat服务器启动

https://blog.csdn.net/u012758088/article/details/78654628

3.9登录状态页查看状态

http://172.16.3.140:8089/stats

http://172.16.3.141:8089/stats

参考:

http://www.roncoo.com/course/view/f614343765bc4aac8597c6d8b38f06fd

https://www.jianshu.com/p/f4b02609a500

https://www.cnblogs.com/hk315523748/p/6094656.html

https://www.cnblogs.com/fxwl/p/7990906.html


压测

1.sysbench
  1. sysbench --test=/usr/local/share/sysbench/tests/include/oltp_legacy/select.lua \
  2. --oltp-table-size=20000 --mysql-table-engine=innodb --mysql-user=mycatdb --mysql-password=1234567 \
  3. --mysql-port=3306 --mysql-host=172.16.3.144 --mysql-db=test --max-requests=0 --max-time=120 \
  4. --oltp-tables-count=20 --report-interval=10 --num_threads=10 prepare
2.直接压测mysql
  1. sysbench --test=/usr/local/share/sysbench/tests/include/oltp_legacy/select.lua \
  2. --oltp-table-size=20000 --mysql-table-engine=innodb --mysql-user=root --mysql-password=123456 \
  3. --mysql-port=3306 --mysql-host=172.16.3.153 --mysql-db=test --max-requests=0 --max-time=120 \
  4. --oltp-tables-count=20 --report-interval=10 --num_threads=10 run
  5. SQL statistics:
  6. queries performed:
  7. read: 2171226
  8. write: 0
  9. other: 0
  10. total: 2171226
  11. transactions: 2171226 (18092.46 per sec.)
  12. queries: 2171226 (18092.46 per sec.)
  13. ignored errors: 0 (0.00 per sec.)
  14. reconnects: 0 (0.00 per sec.)
3.压测haproxy VIP
  1. sysbench --test=/usr/local/share/sysbench/tests/include/oltp_legacy/select.lua \
  2. --oltp-table-size=20000 --mysql-table-engine=innodb --mysql-user=mycatdb --mysql-password=1234567 \
  3. --mysql-port=3306 --mysql-host=172.16.3.144 --mysql-db=test --max-requests=0 --max-time=120 \
  4. --oltp-tables-count=20 --report-interval=10 --num_threads=10 run
  5. # 压测haproxy的QPS/TPS比直接压测mysql更低??!!差了将近4倍
  6. SQL statistics:
  7. queries performed:
  8. read: 498794
  9. write: 0
  10. other: 0
  11. total: 498794
  12. transactions: 498794 (4156.37 per sec.)
  13. queries: 498794 (4156.37 per sec.)
  14. ignored errors: 0 (0.00 per sec.)
  15. reconnects: 0 (0.00 per sec.)
4压测mycat IP
  1. sysbench --test=/usr/local/share/sysbench/tests/include/oltp_legacy/select.lua \
  2. --oltp-table-size=20000 --mysql-table-engine=innodb --mysql-user=mycatdb --mysql-password=1234567 \
  3. --mysql-port=8066 --mysql-host=172.16.3.143 --mysql-db=test --max-requests=0 --max-time=120 \
  4. --oltp-tables-count=20 --report-interval=10 --num_threads=10 run
  5. # 基本只有haproxy的一半
  6. SQL statistics:
  7. queries performed:
  8. read: 233707
  9. write: 0
  10. other: 0
  11. total: 233707
  12. transactions: 233707 (1947.43 per sec.)
  13. queries: 233707 (1947.43 per sec.)
  14. ignored errors: 0 (0.00 per sec.)
  15. reconnects: 0 (0.00 per sec.)

https://my.oschina.net/shyloveliyi/blog/725810

http://blog.51cto.com/arthur376/2045596

不重启mycat加载配置

  • 更改server的配置可以Mysql>reload @@config
  • 如果改了schema的配置,需要这个命令Mysql>reload @@config_all


使用0.5的sysbench

https://github.com/akopytov/sysbench/tree/0.5

再增加一个mycat,压测haproxy VIP

  1. OLTP test statistics:
  2. queries performed:
  3. read: 658118
  4. write: 0
  5. other: 0
  6. total: 658118
  7. transactions: 0 (0.00 per sec.)
  8. read/write requests: 658118 (5484.25 per sec.)
  9. other operations: 0 (0.00 per sec.)
  10. ignored errors: 0 (0.00 per sec.)
  11. reconnects: 0 (0.00 per sec.)

仅仅增加15W多QPS

https://blog.csdn.net/zhxdick/article/details/50813081

http://blog.51cto.com/arthur376/2045596

haproxy mycat mysql 读写分离MHA高可用的更多相关文章

  1. Mysql读写分离 及高可用高性能负载均衡实现

    什么是读写分离,说白了就是mysql服务器读的操作和写的操作是分开的,当然这个需要两台服务器,master负责写,slave负责读,当然我们可以使用多个slave,这样我们也实现了简单意义上的高可用和 ...

  2. Mycat - 实现数据库的读写分离与高可用

    前言 开心一刻 上语文课,不小心睡着了,坐在边上的同桌突然叫醒了我,并小声说道:“读课文第三段”.我立马起身大声读了起来.正在黑板写字的老师吓了一跳,老师郁闷的看着我,问道:“同学有什么问题吗?”,我 ...

  3. MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(二)

    ProxySQL安装 yum/rpm安装 在github或官网上可以下载rpm包,wiki的Getting start章节有详细介绍. cat <<EOF | tee /etc/yum.r ...

  4. MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(一)

    准备服务器: docker network create --subnet=192.168.0.0/16 staticnetdocker run -d --privileged -v `pwd`/my ...

  5. MySQL 之 MHA + ProxySQL + keepalived 实现读写分离,高可用(三)

    设置Keepalived VIP切换邮件告警 修改keepalived.conf配置: [root@server01 keepalived]# cat keepalived.conf ! Config ...

  6. SpringBoot 整合 MyCat 实现读写分离

    MyCat一个彻底开源的,面向企业应用开发的大数据库集群.基于阿里开源的Cobar产品而研发.能满足数据库数据大量存储:提高了查询性能.文章介绍如何实现MyCat连接MySQL实现主从分离,并集成Sp ...

  7. MHA高可用 MHA+Keepalive

    MHA高可用 MHA简介 MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebo ...

  8. 高可用Mysql架构_Mysql主从复制、Mysql双主热备、Mysql双主双从、Mysql读写分离(Mycat中间件)、Mysql分库分表架构(Mycat中间件)的演变

    [Mysql主从复制]解决的问题数据分布:比如一共150台机器,分别往电信.网通.移动各放50台,这样无论在哪个网络访问都很快.其次按照地域,比如国内国外,北方南方,这样地域性访问解决了.负载均衡:M ...

  9. MHA高可用架构与Atlas读写分离

    1.1 MHA简介 1.1.1 MHA软件介绍 MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton ...

随机推荐

  1. ansible的剧本play(四)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA68AAAETCAYAAADZDzDOAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjw

  2. 终止过久没有返回的 Windows API 函数 ---- “CancelSynchronousIo”

    Marks pending synchronous I/O operations that are issued by the specified thread as canceled. BOOL W ...

  3. Windows 计划任务 如果选择未登录就运行 则看不到GUI

    You can specify that a task should run even if the account under which the task is scheduled to run ...

  4. Linux网络服务第七章DNS域名解析服务

    端口:53 一.DNS服务器 正向解析:根据域名查IP地址,即将指定的域名解析为相对应的IP地址.域名的正向解析是DNS服务器最基本的功能,也是最常用的功能. 反向解析:根据IP地址查域名,即将指定的 ...

  5. Ali_Cloud++:阿里云服务器部署【禅道】项目管理系统

    1.开源版安装包下载 地址一:百度云下载 10.0  提取码:2dyg  地址二:官方下载 2.直接解压安装包到/opt目录下 注意:这里我安装的是Linux一键安装包官方给出的方法就是直接解压到/o ...

  6. java 之 enum(枚举)

    推荐博客 http://blog.csdn.net/javazejian/article/details/71333103

  7. 【集群实战】NFS网络文件共享服务

    1. NFS介绍 1.1 什么是NFS? NFS是Network File System的缩写,中文意思是网络文件系统. 它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录. ...

  8. 02-线性结构4 Pop Sequence

    02-线性结构4 Pop Sequence   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 https://pta.p ...

  9. OpenRASP管理后台安装记录

    OpenRASP项目地址https://rasp.baidu.com/ 一.安装java 在CentOS中安装ElasticSearch需要Java1.8.0,可执行命令java -version查看 ...

  10. centos7 安装高版本svn

    一.安装高版本svn 1.创建一个新的yum库文件,vim /etc/yum.repos.d/wandisco-svn.repo 内容如下 [WandiscoSVN] name=Wandisco SV ...