首页 数据库 mysql教程 ProxySQL读写分离从配置到使用

ProxySQL读写分离从配置到使用

Jul 21, 2017 am 09:55 AM
分离 读写

在美团点评DBProxy读写分离使用说明文章中已经说明了使用目的,本文介绍ProxySQL的使用方法以及和DBProxy的性能差异。具体的介绍可以看官网的相关说明,并且这个中间件也是percona推的一款中间件。其特性和其他读写分离的中间件差距不大,具体的会在文中介绍。本文大致简单的介绍在使用过程中的一些说明,也可以看官方的wiki获得使用帮助。

环境: 

Distributor ID:    Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:    14.04Codename:    trusty

下载

percona站点:

https://www.percona.com/downloads/proxysql/

github/官网:

https://github.com/sysown/proxysql/releases



我们首先看一下自己的环境:

MHA已经搭建:
master:172.16.16.35:3306slave:172.16.16.35:3307slave:172.16.16.34:3307
登录后复制

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=123456ping_interval=1remote_workdir=/tmp
repl_password=123456repl_user=root
report_script=/usr/local/bin/send_report
shutdown_script=""ssh_user=root
user=root
[server1]
hostname=172.16.16.35port=3306[server2]
candidate_master=1check_repl_delay=0hostname=172.16.16.34port=3306[server3]
hostname=172.16.16.35port=3307
登录后复制

 

下面我们基于这样一套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 0:1.3.7-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="6032"
 # PXC admin credentials for connecting to pxc-cluster-node.
export CLUSTER_USERNAME="admin"export CLUSTER_PASSWORD="admin"export CLUSTER_HOSTNAME="localhost"export CLUSTER_PORT="3306"
 # 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="10"export READ_HOSTGROUP_ID="11"
 # 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.1 -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 1Server version: 5.7.14 (ProxySQL Admin Module)
Copyright (c) 2000, 2016, 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.1 -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 2Server version: 5.7.14 (ProxySQL Admin Module)
 
Copyright (c) 2000, 2016, 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 |
+-----+---------+-------------------------------+
| 0 | main | |
| 2 | disk | /var/lib/proxysql/proxysql.db |
| 3 | stats | |
| 4 | monitor | |
+-----+---------+-------------------------------+4 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 |
+--------------------------------------+13 rows in set (0.00 sec)
登录后复制

 

下面加入主从信息:
mysql> insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(100,'172.16.16.35',3306,1,1000,10,'test');
Query OK, 1 row affected (0.00 sec)
mysql> insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(101,'172.16.16.34',3306,1,1000,10,'test');
Query OK, 1 row affected (0.00 sec)
mysql> insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(101,'172.16.16.35',3307,1,1000,10,'test');
Query OK, 1 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 |
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 100 | 172.16.16.35 | 3306 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | test |
| 101 | 172.16.16.34 | 3306 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | test |
| 101 | 172.16.16.35 | 3307 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | test |
+--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+3 rows in set (0.00 sec)
登录后复制

 

然后添加读写映射(主要是MHA后端切换的时候保证ProxySQL也能够自动切换):
mysql> insert into mysql_replication_hostgroups values(100,101,'masterha') ;
Query OK, 1 row affected (0.00 sec)
mysql> select * from mysql_replication_hostgroups;+------------------+------------------+----------+
| writer_hostgroup | reader_hostgroup | comment |
+------------------+------------------+----------+
| 100 | 101 | masterha |
+------------------+------------------+----------+1 row in set (0.00 sec)
登录后复制

 

为ProxySQL添加监控账号:
mysql> GRANT SUPER, REPLICATION CLIENT ON *.* TO 'proxysql'@'172.16.16.%' IDENTIFIED BY 'proxysql';
Query OK, 0 rows affected, 1 warning (0.09 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.10 sec)
登录后复制

 

配置监控账号(在proxySQL当中进行配置):
mysql> set mysql-monitor_username='proxysql';
Query OK, 1 row affected (0.00 sec)
mysql> set mysql-monitor_password='proxysql';
Query OK, 1 row affected (0.00 sec)
mysql> load mysql variables to runtime;
Query OK, 0 rows affected (0.00 sec)
mysql> save mysql variables to disk;
Query OK, 74 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','123456',1,100,1);
Query OK, 1 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 | 123456 | 1 | 0 | 100 | NULL | 0 | 1 | 0 | 1 | 1 | 10000 |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+1 row in set (0.00 sec)
登录后复制

 

配置完成以后开始重新加载和保存我们的配置:
mysql> load mysql servers to runtime;
Query OK, 0 rows affected (0.01 sec)
mysql> save mysql servers to disk;
Query OK, 0 rows affected (0.08 sec)
mysql> load mysql users to runtime;
Query OK, 0 rows affected (0.00 sec)
mysql> save mysql users to disk;
Query OK, 0 rows affected (0.03 sec)
登录后复制

 

接下来开始配置路由规则:
mysql> INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(1,'^SELECT.*FOR UPDATE$',100,1);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(1,'^SELECT',101,1);
Query OK, 1 row affected (0.00 sec)
mysql> LOAD MYSQL QUERY RULES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)
mysql> SAVE MYSQL QUERY RULES TO DISK;
Query OK, 0 rows affected (0.04 sec)
登录后复制

 

至此配置已经完成了
3:测试读写分离
在172.16.16.35上链接proxySQL端口6033,并且做简单的select操作:
[root@localhost ~]# mysql -uroot -p123456 -h172.16.16.34 -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 22Server version: 5.7.14 (ProxySQL)
Copyright (c) 2000, 2016, 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 |
+-----+------+
| 1 | qq |
| 2 | qq |
| 4 | aa |
| 11 | a |
| 111 | a |
+-----+------+5 rows in set (0.04 sec)
登录后复制

 

然后在进行查询看一下:
mysql> select * from stats_mysql_query_digest;
登录后复制
登录后复制

可以看到已经完成了读写分离了。
mysql> select @@server_id;+-------------+
| @@server_id |
+-------------+
| 353307 |
+-------------+1 row in set (0.01 sec)
登录后复制

 

查看一下server ID已经路由到了172.16.16.35:3307这个从库上了。
测一下for update:
mysql> select * from maxiangqian.test for update;+-----+------+
| id | name |
+-----+------+
| 1 | qq |
| 2 | qq |
| 4 | aa |
| 11 | a |
| 111 | a |
+-----+------+5 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读写分离从配置到使用的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何使用Java 9中的模块系统来分离和隔离代码 如何使用Java 9中的模块系统来分离和隔离代码 Jul 30, 2023 pm 07:46 PM

如何使用Java9中的模块系统来分离和隔离代码随着软件规模不断扩大,代码的复杂度也在不断增加。为了更好地组织和管理代码,Java9引入了模块系统。模块系统的出现解决了传统的包依赖的问题,使得代码的分离和隔离更加容易和灵活。本文将介绍如何使用Java9中的模块系统来实现代码的分离和隔离。一、模块的定义在Java9中,我们可以使用module关键字来定义

如何使用 PHP 实现数据缓存和读写功能 如何使用 PHP 实现数据缓存和读写功能 Sep 05, 2023 pm 05:45 PM

如何使用PHP实现数据缓存和读写功能缓存是提高系统性能的一种重要方式,通过缓存可以将频繁使用的数据存储在内存中,以提高数据的读取速度。在PHP中,我们可以使用各种方法来实现数据缓存和读写功能。本文将介绍两种常用的方法:使用文件缓存和使用内存缓存。一、使用文件缓存文件缓存是将数据存储在文件中,以便后续读取。下面是一个使用文件缓存实现数据读写的示例代码:

Golang前后端分离开发中前端技术选择建议。 Golang前后端分离开发中前端技术选择建议。 Mar 05, 2024 pm 12:12 PM

标题:Golang前后端分离开发中前端技术选择建议随着Web应用程序的复杂性和需求不断增加,前后端分离开发模式变得越来越流行。在这种开发模式下,后端负责处理业务逻辑,前端负责展示页面和与用户交互,两者通过API进行通信。对于使用Golang作为后端语言的开发团队来说,选择合适的前端技术至关重要。本文将探讨在Golang前后端分离开发中,建议选择的前端技术,并

实战:Linux上硬盘io读写测试 实战:Linux上硬盘io读写测试 Feb 19, 2024 pm 03:40 PM

概念fio,又称为FlexibleIOTester,是JensAxboe编写的应用程序。Jens是LinuxKernel中blockIOsubsystem的维护者。FIO是一种用于测试网络文件系统和磁盘性能的工具,常用于验证机型和比较文件系统性能。它能自动将fio命令发送到集群机器列表,并收集小文件的IOPS和大文件的吞吐量数据。rw=[mode]rwmixwrite=30在混合读写的模式下,写占30%moderead顺序读write顺序写readwrite顺序混合读写randwrite随机写r

揭秘 Java 文件操作的内部原理 揭秘 Java 文件操作的内部原理 Feb 28, 2024 am 08:22 AM

文件系统APIJava文件操作的内部原理与操作系统的文件系统api密切相关。在Java中,文件操作是由java.NIO包中的java.nio.file模块提供的。该模块提供了对文件系统API的封装,使Java开发者能够在不同的操作系统上使用统一的API进行文件操作。文件对象当Java程序需要访问文件时,它首先需要创建一个java.nio.file.Path对象。Path对象代表了文件系统中的一个路径,可以是绝对路径也可以是相对路径。一旦创建了Path对象,就可以使用它来获取文件的各种属性,如名称

解密Java中处理DBF文件的读取和写入方法 解密Java中处理DBF文件的读取和写入方法 Mar 29, 2024 pm 12:39 PM

解密Java中处理DBF文件的读取和写入方法DBF(dBASEFile)是一种常见的数据库文件格式,通常用于存储表格数据。在Java程序中,处理DBF文件的读取和写入是一个比较常见的需求。本文将介绍如何使用Java解密这一过程,并提供具体的代码示例。一、读取DBF文件在Java中,读取DBF文件通常需要借助第三方库,比如使用dbfread库。首先需要在项目

PHP开发者必备:MySQL读写分离实现方法详解 PHP开发者必备:MySQL读写分离实现方法详解 Mar 04, 2024 pm 04:36 PM

PHP开发者是在网站开发过程中,经常会面对数据库操作的情况,而MySQL作为非常流行的数据库管理系统,其读写分离是提高网站性能的重要手段之一。在PHP开发中,实现MySQL读写分离可以极大地提升网站的并发访问能力和用户体验。本文将详细介绍MySQL读写分离的实现方法,并提供具体的PHP代码示例,帮助PHP开发者更好地理解和应用读写分离功能。什么是MySQL读

如何通过静态资源分离提升Java网站的访问速度? 如何通过静态资源分离提升Java网站的访问速度? Aug 04, 2023 pm 03:21 PM

如何通过静态资源分离提升Java网站的访问速度?随着互联网的快速发展,越来越多的人使用网站来获取信息和进行交流。对于一个Java网站来说,访问速度是至关重要的,它直接影响着用户体验和网站的成功与否。其中,静态资源的加载速度是影响网站访问速度的重要因素之一。本文将介绍如何通过静态资源分离来提升Java网站的访问速度。什么是静态资源首先,我们需要明确什么是静态资

See all articles