我们首先看一下自己的环境:
MHA已经搭建:
master:172.16.16.35:
slave:172.16.16.35:
slave:172.16.16.34:

MHA manager在172.16.16.34,配置文件如下:

[root@localhost bin]# cat /etc/masterha/app1.cnf
[server default]
manager_log=/var/log/mha/app1/manager.log
manager_workdir=/var/log/mha/app1.log
master_binlog_dir=/home/mysql/db3306/log/
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=
ping_interval=
remote_workdir=/tmp
repl_password=
repl_user=root
report_script=/usr/local/bin/send_report
shutdown_script=""
ssh_user=root
user=root
[server1]
hostname=172.16.16.35
port=
[server2]
candidate_master=
check_repl_delay=
hostname=172.16.16.34
port=
[server3]
hostname=172.16.16.35
port=
下面我们基于这样一套MHA环境搭建读写分离。
1:安装ProxySQL软件,这个我们部署到172.16.16.34上
[root@localhost bin]# sudo yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
[root@localhost bin]# yum install proxysql
最后有以下提示:
Installed:
proxysql.x86_64 :1.3.-1.1.el6
Complete!
也就是安装完成了。然后查一下具体的文件:
[root@localhost bin]# find / -name proxysql
/var/lib/proxysql
/var/run/proxysql
/etc/rc.d/init.d/proxysql
/usr/bin/proxysql
发现确实已经将ProxySQL安装成功了
2:启动配置ProxySQL
看一下配置文件:
[root@localhost bin]# cat /etc/proxysql-admin.cnf
# proxysql admin interface credentials.
export PROXYSQL_USERNAME="admin"
export PROXYSQL_PASSWORD="admin"
export PROXYSQL_HOSTNAME="localhost"
export PROXYSQL_PORT="" # PXC admin credentials for connecting to pxc-cluster-node.
export CLUSTER_USERNAME="admin"
export CLUSTER_PASSWORD="admin"
export CLUSTER_HOSTNAME="localhost"
export CLUSTER_PORT="" # proxysql monitoring user. proxysql admin script will create this user in pxc to monitor pxc-nodes.
export MONITOR_USERNAME="monitor"
export MONITOR_PASSWORD="monit0r" # Application user to connect to pxc-node through proxysql
export CLUSTER_APP_USERNAME="proxysql_user"
export CLUSTER_APP_PASSWORD="passw0rd" # ProxySQL read/write hostgroup
export WRITE_HOSTGROUP_ID=""
export READ_HOSTGROUP_ID="" # ProxySQL read/write configuration mode.
export MODE="singlewrite"
启动:
[root@localhost bin]# proxysql-admin --config-file=/etc/proxysql-admin.cnf --enable
This script will assist with configuring ProxySQL (currently only Percona XtraDB cluster in combination with ProxySQL is supported)
ProxySQL read/write configuration mode is singlewrite
ProxySQL is not running; please start the proxysql service
现在来说ProxySQL 的路由已经启动,提示我们要启动proxysql service
[root@localhost bin]# service proxy
proxy proxysql proxysql-admin proxysql_galera_checker proxysql_node_monitor
[root@localhost bin]# service proxysql start
Starting ProxySQL: DONE!
[root@localhost bin]# mysql -uadmin -padmin -h127.0.0. -P6032
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7. (ProxySQL Admin Module)
Copyright (c) , , Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
可以看到我们已经登录成功了,这里要说明的是 proxysql的默认配置文件是在:
[root@localhost bin]# find / -name proxysql.cnf
/etc/proxysql.cnf
接下来我们开始配置ProxySQL:
[root@localhost bin]# mysql -uadmin -padmin -h127.0.0. -P6032
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7. (ProxySQL Admin Module) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases;
+-----+---------+-------------------------------+
| seq | name | file |
+-----+---------+-------------------------------+
| | main | |
| | disk | /var/lib/proxysql/proxysql.db |
| | stats | |
| | monitor | |
+-----+---------+-------------------------------+
rows in set (0.00 sec) mysql> use admin
Database changed
mysql> show tables;
+--------------------------------------+
| tables |
+--------------------------------------+
| global_variables |
| mysql_collations |
| mysql_query_rules |
| mysql_replication_hostgroups |
| mysql_servers |
| mysql_users |
| runtime_global_variables |
| runtime_mysql_query_rules |
| runtime_mysql_replication_hostgroups |
| runtime_mysql_servers |
| runtime_mysql_users |
| runtime_scheduler |
| scheduler |
+--------------------------------------+
rows in set (0.00 sec)
下面加入主从信息:
mysql> insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(,'172.16.16.35',,,,,'test');
Query OK, row affected (0.00 sec)
mysql> insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(,'172.16.16.34',,,,,'test');
Query OK, row affected (0.00 sec)
mysql> insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(,'172.16.16.35',,,,,'test');
Query OK, row affected (0.00 sec)
mysql> select * from mysql_servers;
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| | 172.16.16.35 | | ONLINE | | | | | | | test |
| | 172.16.16.34 | | ONLINE | | | | | | | test |
| | 172.16.16.35 | | ONLINE | | | | | | | test |
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
rows in set (0.00 sec)
然后添加读写映射(主要是MHA后端切换的时候保证ProxySQL也能够自动切换):
mysql> insert into mysql_replication_hostgroups values(,,'masterha') ;
Query OK, row affected (0.00 sec)
mysql> select * from mysql_replication_hostgroups;
+------------------+------------------+----------+
| writer_hostgroup | reader_hostgroup | comment |
+------------------+------------------+----------+
| | | masterha |
+------------------+------------------+----------+
row in set (0.00 sec)
为ProxySQL添加监控账号:
mysql> GRANT SUPER, REPLICATION CLIENT ON *.* TO 'proxysql'@'172.16.16.%' IDENTIFIED BY 'proxysql';
Query OK, rows affected, warning (0.09 sec)
mysql> flush privileges;
Query OK, rows affected (0.10 sec)
配置监控账号(在proxySQL当中进行配置):
mysql> set mysql-monitor_username='proxysql';
Query OK, row affected (0.00 sec)
mysql> set mysql-monitor_password='proxysql';
Query OK, row affected (0.00 sec)
mysql> load mysql variables to runtime;
Query OK, rows affected (0.00 sec)
mysql> save mysql variables to disk;
Query OK, rows affected (0.02 sec)
PS:有时候runtime_mysql_servers的status不为ONLINE状态的话可以通过查看monitor.mysql_server_ping_log这个表来查看具体的报错信息。
mysql> select * from monitor.mysql_server_ping_log;
然后配置程序账号,简单点统一使用root:123456最高权限来配置:
mysql> insert into mysql_users(username,password,active,default_hostgroup,transaction_persistent) values('root','',,,);
Query OK, row affected (0.00 sec)
mysql> select * from mysql_users;
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| root | | | | | NULL | | | | | | |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
row in set (0.00 sec)
配置完成以后开始重新加载和保存我们的配置:
mysql> load mysql servers to runtime;
Query OK, rows affected (0.01 sec)
mysql> save mysql servers to disk;
Query OK, rows affected (0.08 sec)
mysql> load mysql users to runtime;
Query OK, rows affected (0.00 sec)
mysql> save mysql users to disk;
Query OK, rows affected (0.03 sec)
接下来开始配置路由规则:
mysql> INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(,'^SELECT.*FOR UPDATE$',,);
Query OK, row affected (0.01 sec)
mysql> INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(,'^SELECT',,);
Query OK, row affected (0.00 sec)
mysql> LOAD MYSQL QUERY RULES TO RUNTIME;
Query OK, rows affected (0.00 sec)
mysql> SAVE MYSQL QUERY RULES TO DISK;
Query OK, rows affected (0.04 sec)
至此配置已经完成了
3:测试读写分离
在172.16.16.35上链接proxySQL端口6033,并且做简单的select操作:
[root@localhost ~]# mysql -uroot -p123456 -h172.16.16. -P6033
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7. (ProxySQL)
Copyright (c) , , Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select * from maxiangqian.test;
+-----+------+
| id | name |
+-----+------+
| | qq |
| | qq |
| | aa |
| | a |
| | a |
+-----+------+
rows in set (0.04 sec)
然后在进行查询看一下:
mysql> select * from stats_mysql_query_digest;

可以看到已经完成了读写分离了。
mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| |
+-------------+
row in set (0.01 sec)
查看一下server ID已经路由到了172.16.16.35:3307这个从库上了。
测一下for update:
mysql> select * from maxiangqian.test for update;
+-----+------+
| id | name |
+-----+------+
| | qq |
| | qq |
| | aa |
| | a |
| | a |
+-----+------+
rows in set (0.00 sec)
再查看
mysql> select * from stats_mysql_query_digest;

已经自动路由到了主库。
至此ProxySQL+MySQL MHA读写分离测试完成。。。未完待续
5:关于ProxySQL的思考以及简单的命令
ProxySQL通过以上方式是可以实现读写分离,但是这种方式真的就没有问题了吗,如果是一些比如查询订单状态的这种要求实时性非常高的SQL的话,似乎被路由到了从库就会出现BUG。我们可以选择在程序端控制这些参数,ProxySQL只作为一个负载均衡来使用,给ProxySQL创建多个账号,一个读写,一个只读。然后程序去实现读写分离。
ProxySQL是分三层来设计运行的,分别为RUNTIME ,MEMORY ,DISK :
RUNTIME 代表的是ProxySQL当前生效的配置,包括 global_variables, mysql_servers, mysql_users, mysql_query_rules。无法直接修改这里的配置,必须要从下一层load进来。
MEMORY 是平时在mysql命令行修改的 main 里头配置,可以认为是SQLite数据库在内存的镜像
DISK / CONFIG FILE 持久存储的那份配置,一般在$(DATADIR)/proxysql.db,在重启的时候会从硬盘里加载。 /etc/proxysql.cnf文件只在第一次初始化的时候用到,完了后,如果要修改监听端口,还是需要在管理命令行里修改,再 save 到硬盘
常用命令:
LOAD MYSQL SERVERS TO RUNTIME -- 让修改的配置生效,也就是从MEMORY 把参数LOAD过来,等价于LOAD MYSQL USERS FROM MEMORY,这个语句的语法比较单间,FROM代表从上层LOAD过来,TO代表从本层到某一个层。比如前面我们设置了MySQL的监控账号,但是还是要执行LOAD和SAVE保存变量并且使变量生效。

ProxySQL读写分离的更多相关文章

  1. MySQL ProxySQL读写分离实践

    目的 在上一篇文章MySQL ProxySQL读写分离使用初探里初步介绍了ProxySQL的使用,本文继续介绍它的一些特点和DBProxy的性能差异.深入一些去了解ProxySQL,通过测试来说明Pr ...

  2. mysqlfailover高可用与proxysql读写分离配置

    proxysql官方推荐两种高可用方案: 1.MHA+proxysql 2.mysqlrpladmin+proxysql MySQLfailover工具包含在mysqlrpladmin工具中,所以两者 ...

  3. MHA+ProxySQL 读写分离高可用

    文档结构如下: 1.ProxySQL说明 ProxySQL是mysql的一款中间件的产品,是灵活的mysql代理层,可以实现读写分离,支持query路由器的功能,支持动态指定sql进行缓存,支持动态加 ...

  4. MySQL ProxySQL读写分离使用初探

    目的 在美团点评DBProxy读写分离使用说明文章中已经说明了使用目的,本文介绍ProxySQL的使用方法以及和DBProxy的性能差异.具体的介绍可以看官网的相关说明,并且这个中间件也是percon ...

  5. ProxySQL 读写分离实践

    前言 ProxySQL是一个高性能的MySQL中间件,拥有强大的规则引擎.具有以下特性: 连接池,而且是 multiplexing 主机和用户的最大连接数限制 自动下线后端DB 延迟超过阀值 ping ...

  6. Proxysql读写分离配置

    ProxySQL是Percona主推的读写分离中间件,下载地址为: https://www.percona.com/downloads/proxysql/ 一.安装 1:下载 wget https:/ ...

  7. ProxySQL读写分离代理

    实现ProxySQL反向代理Mysql读写分离 简介 ProxySQL相当于小型的数据库,在磁盘上有存放数据库的目录:ProxySQL用法和mysql相似 启动ProxySQL后会有两个监听端口: 6 ...

  8. ProxySQL 读写分离方法

    转载自:https://www.jianshu.com/p/597b840bf70c (使用正则表达式实现基本的读/写分离) 在这一部分,我将通过一个示例来演示如何通过正则表达式来实现读/写分离. 首 ...

  9. ProxySQL读写分离测试(续)

      Preface       I've implemented ProxySQL on PXC yesterday but got some errors when configured query ...

随机推荐

  1. Javascript框架设计思路图

    这个系列的随笔都是关于Javascript框架设计一书的读书笔记(作者是司徒正美),不是本人原创!!! 一.简介: 1.市面上主流的JS框架,大多数是由一个个模块组合而成,模块化是大多数让软件所遵循的 ...

  2. 【Guava】Optional接口来避免空指针错误

    null会带来很多问题,从开始有null开始有无数程序栽在null的手里,null的含义是不清晰的,检查null在大多数情况下是不得不做的,而我们又在很多时候忘记了对null做检查,在我们的产品真正投 ...

  3. clipse validation 优化设置

  4. MySQL存储引擎 InnoDB与MyISAM的区别

    来源:http://www.jb51.net/article/47597.htm 基本的差别:MyISAM类型不支持事务处理等高级处理,而InnoDB类型支持.MyISAM类型的表强调的是性能,其执行 ...

  5. java 继承多态的一些理解和不理解

    1.向上转型的一个误区 一直以为Child 继承Parent以后, Parent p = new Child();  p可以调用Child类中拓展Parent的方法,原来必须在强制转换成Child类才 ...

  6. 微信授权获取code(微信支付)

    摘要:最近在做h5支付,然后发现一个问题,微信自带浏览器不支持h5支付,然后后台又做了一个微信支付的接口,然后要传code参数,代码写好总结后,就发到这里记录一下: 因为有两个支付接口,所以首先判断打 ...

  7. Golang Socket编程

    Socket编程 在很多底层网络应用开发者的眼里一切编程都是Socket,话虽然有点夸张,但却也几乎如此了,现在的网络编程几乎都是用Socket来编程.你想过这些情景么?我们每天打开浏览器浏览网页时, ...

  8. [C语言] 变量和数据类型和整数和字符

    1.数据类型和变量: 数据是放在内存中的,明确三件事:数据存储在哪里.数据的长度以及数据的处理方式 int n;数据类型指明了数据的长度和处理方式,变量名指明了数据存储在哪里 2.数据长度:是指数据占 ...

  9. php 在函数前面加个@的作用

    @是错误控制运算符,用屏蔽错误提示比如:@mysql_connect() 不会出现Warning, 而原来mysql_connect 会在页面上访提示Warning.主要是高版本的php不在支持mys ...

  10. 使用xml4j xml与字符串之间转换

    jar准备:dom4j-2.1.1.jar  jaxen-1.1.6.jar jaxen/jaxen/ Maven依赖写法 <dependency> <groupId>jaxe ...