首页 > 数据库 >MySQL读写分离ProxySQL安装部署

MySQL读写分离ProxySQL安装部署

时间:2023-04-22 19:33:25浏览次数:48  
标签:monitor 10.6 proxysql 读写 MySQL ProxySQL time timeout mysql


ProxySQL简介

ProxySQL为MySQL的中间件,其有两个版本官方版和percona版,percona版是基于官方版基础上修改而来。ProxySQL是由C++语言开发,轻量级但性能优异(支持处理千亿级数据),其具有中间件所需要的绝大多数功能,如:

  • 多种方式的读写分离
  • 定制基于用户、基于schema、基于语言的规则对SQL语句进行路由
  • 缓存查询结果
  • 后端节点的控制

安装

下载安装

# 获取centos版本信息下载对应的
cat /etc/centos-release

curl -O  https://repo.proxysql.com/ProxySQL/proxysql-2.5.x/centos/7/proxysql-2.5.0-1-centos7.x86_64.rpm


yum install proxysql-2.5.0-1-centos7.x86_64.rpm

docker 安装(推荐)

vim proxysql.cnf

datadir="/var/lib/proxysql"

admin_variables=
{
    admin_credentials="admin:admin;radmin:radmin"
    mysql_ifaces="0.0.0.0:6032"
}

mysql_variables=
{
    threads=4
    max_connections=2048
    default_query_delay=0
    default_query_timeout=36000000
    have_compress=true
    poll_timeout=2000
    interfaces="0.0.0.0:6033"
    default_schema="information_schema"
    stacksize=1048576
    server_version="5.5.30"
    connect_timeout_server=3000
    monitor_username="monitor"
    monitor_password="monitor"
    monitor_history=600000
    monitor_connect_interval=60000
    monitor_ping_interval=10000
    monitor_read_only_interval=1500
    monitor_read_only_timeout=500
    ping_interval_server_msec=120000
    ping_timeout_server=500
    commands_stats=true
    sessions_sort=true
    connect_retries_on_failure=10
}

启动

docker run --privileged=true -p 16032:6032 -p 16033:6033 -p 16070:6070 -d -v /home/lys/proxysql.cnf:/etc/proxysql.cnf proxysql/proxysql

推荐使用这个

docker run --privileged=true network=host  -d -v /home/lys/proxysql.cnf:/etc/proxysql.cnf proxysql/proxysql

配置

登录

mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin>'

插入主备节点

INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (1,'10.6.8.174',3306);
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (2,'10.6.8.147',3306);

设置用户

UPDATE global_variables SET variable_value='monitor12345678' WHERE variable_name='mysql-monitor_username';
 UPDATE global_variables SET variable_value='monitor12345678' WHERE variable_name='mysql-monitor_password';
Admin>SELECT * FROM global_variables WHERE variable_name LIKE 'mysql-monitor_%';
+----------------------------------------------------------------------+-----------------+
| variable_name                                                        | variable_value  |
+----------------------------------------------------------------------+-----------------+
| mysql-monitor_enabled                                                | true            |
| mysql-monitor_connect_timeout                                        | 600             |
| mysql-monitor_ping_max_failures                                      | 3               |
| mysql-monitor_ping_timeout                                           | 1000            |
| mysql-monitor_read_only_max_timeout_count                            | 3               |
| mysql-monitor_replication_lag_interval                               | 10000           |
| mysql-monitor_replication_lag_timeout                                | 1000            |
| mysql-monitor_replication_lag_count                                  | 1               |
| mysql-monitor_groupreplication_healthcheck_interval                  | 5000            |
| mysql-monitor_groupreplication_healthcheck_timeout                   | 800             |
| mysql-monitor_groupreplication_healthcheck_max_timeout_count         | 3               |
| mysql-monitor_groupreplication_max_transactions_behind_count         | 3               |
| mysql-monitor_groupreplication_max_transactions_behind_for_read_only | 1               |
| mysql-monitor_galera_healthcheck_interval                            | 5000            |
| mysql-monitor_galera_healthcheck_timeout                             | 800             |
| mysql-monitor_galera_healthcheck_max_timeout_count                   | 3               |
| mysql-monitor_replication_lag_use_percona_heartbeat                  |                 |
| mysql-monitor_query_interval                                         | 60000           |
| mysql-monitor_query_timeout                                          | 100             |
| mysql-monitor_slave_lag_when_null                                    | 60              |
| mysql-monitor_threads_min                                            | 8               |
| mysql-monitor_threads_max                                            | 128             |
| mysql-monitor_threads_queue_maxsize                                  | 128             |
| mysql-monitor_wait_timeout                                           | true            |
| mysql-monitor_writer_is_also_reader                                  | true            |
| mysql-monitor_username                                               | monitor12345678 |
| mysql-monitor_password                                               | monitor12345678 |
| mysql-monitor_history                                                | 600000          |
| mysql-monitor_connect_interval                                       | 60000           |
| mysql-monitor_ping_interval                                          | 10000           |
| mysql-monitor_read_only_interval                                     | 1500            |
| mysql-monitor_read_only_timeout                                      | 500             |
+----------------------------------------------------------------------+-----------------+

检查登录情况

Admin>SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 3;
+------------+------+------------------+-------------------------+-----------------------------------------------------------------------------+
| hostname   | port | time_start_us    | connect_success_time_us | connect_error                                                               |
+------------+------+------------------+-------------------------+-----------------------------------------------------------------------------+
| 10.6.8.147 | 3306 | 1681299772585477 | 0                       | Access denied for user 'monitor12345678'@'10.6.8.174' (using password: YES) |
| 10.6.8.174 | 3306 | 1681299771791832 | 0                       | Access denied for user 'monitor12345678'@'10.6.8.174' (using password: YES) |
| 10.6.8.174 | 3306 | 1681299767550776 | 0                       | Access denied for user 'monitor'@'10.6.8.174' (using password: YES)         |
+------------+------+------------------+-------------------------+-----------------------------------------------------------------------------+
3 rows in set (0.00 sec)

放开白名单即可

然后健康检查结果

Admin>SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 3;
+------------+------+------------------+-------------------------+---------------+
| hostname   | port | time_start_us    | connect_success_time_us | connect_error |
+------------+------+------------------+-------------------------+---------------+
| 10.6.8.174 | 3306 | 1681300972640975 | 604                     | NULL          |
| 10.6.8.147 | 3306 | 1681300971793675 | 1768                    | NULL          |
| 10.6.8.147 | 3306 | 1681300912640330 | 1582                    | NULL          |
+------------+------+------------------+-------------------------+---------------+
3 rows in set (0.00 sec)

Admin>SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us DESC LIMIT 3;
+------------+------+------------------+----------------------+------------+
| hostname   | port | time_start_us    | ping_success_time_us | ping_error |
+------------+------+------------------+----------------------+------------+
| 10.6.8.174 | 3306 | 1681301032123280 | 178                  | NULL       |
| 10.6.8.147 | 3306 | 1681301031931784 | 458                  | NULL       |
| 10.6.8.174 | 3306 | 1681301022061284 | 225                  | NULL       |
+------------+------+------------------+----------------------+------------+
3 rows in set (0.00 sec)

配置主备切换的自动检测

all the MySQL backend servers that are either configured in hostgroup 1 or 2 will be placed into their respective hostgroup based on their read_only value:

If they have , they will be moved to hostgroup 1read_only=0
If they have , they will be moved to hostgroup 2read_only=1

INSERT INTO mysql_replication_hostgroups (writer_hostgroup,reader_hostgroup,comment) VALUES (1,2,'cluster1');

# 生效
 LOAD MYSQL SERVERS TO RUNTIME;

检查生效

Admin>SELECT * FROM monitor.mysql_server_read_only_log ORDER BY time_start_us DESC LIMIT 3;
+------------+------+------------------+-----------------+-----------+-------+
| hostname   | port | time_start_us    | success_time_us | read_only | error |
+------------+------+------------------+-----------------+-----------+-------+
| 10.6.8.174 | 3306 | 1681809841749869 | 278             | 1         | NULL  |
| 10.6.8.147 | 3306 | 1681809841734498 | 650             | 0         | NULL  |
| 10.6.8.174 | 3306 | 1681809840250699 | 258             | 1         | NULL  |
+------------+------+------------------+-----------------+-----------+-------+
3 rows in set (0.00 sec)

配置读写分离规则

Key points about these query rules (and query rules in general):

  • Query rules are processed as ordered by rule_id
  • Only rules that have active=1 are processed
  • The first rule example uses caret (^) and dollar ($) : these are special regex characters that mark the beginning and the end of a pattern i.e. in this case match_digestormatch_pattern should completely match the query
  • The second rule in the example doesn’t use caret or dollar : the match could be anywhere in the query
  • The question mark is escaped as it has a special meaning in regex
    apply=1 means that no further rules should be evaluated if the current rule was matched
INSERT INTO mysql_query_rules (rule_id,active,username,match_digest,destination_hostgroup,apply) VALUES (10,1,'monitor12345678','^SELECT',2,1);


# 生效
LOAD MYSQL QUERY RULES TO RUNTIME;

验证读写分离

Admin>SELECT hostgroup hg, sum_time, count_star, digest_text FROM stats_mysql_query_digest where hg=2 ORDER BY sum_time asc;
+----+----------+------------+---------------------------------------------------+
| hg | sum_time | count_star | digest_text                                       |
+----+----------+------------+---------------------------------------------------+
| 2  | 515      | 1          | SELECT * FROM `test01`.`aa` WHERE `a` = ? LIMIT ? |
| 2  | 9565     | 13         | SELECT * FROM `test01`.`aa` LIMIT ?,?             |
+----+----------+------------+---------------------------------------------------+
2 rows in set (0.01 sec)

Admin>SELECT hostgroup hg, SUM(sum_time), SUM(count_star) FROM stats_mysql_query_digest GROUP BY hostgroup;
+----+---------------+-----------------+
| hg | SUM(sum_time) | SUM(count_star) |
+----+---------------+-----------------+
| 1  | 505998        | 300             |
| 2  | 10080         | 14              |
+----+---------------+-----------------+
2 rows in set (0.00 sec)

参考文献

https://proxysql.com/documentation/installing-proxysql/

https://proxysql.com/documentation/ProxySQL-Configuration/

标签:monitor,10.6,proxysql,读写,MySQL,ProxySQL,time,timeout,mysql
From: https://blog.51cto.com/liuyunshengsir/6215476

相关文章

  • mysql获取当前年月 mysql中replace into用法
    mysql获取当前年月 //1.获取年月日时分秒selectSYSDATE()AS'年月日时分秒';2020-07-0216:36:17//2.获取(年月日)selectDATE(CURDATE())as'年月日';selectCURDATE()as'年月日';selectcurrent_dateAS'年月日';2020-07-02//3.获取(时分秒):......
  • mysql综合练习题
    作业1第一题D错误。别名有空格要用“”都引起来第三题别名和之前的列名都可以使用作业2SELECT*FROMempSELECT*FROMdeptSELECT*FROMsalgrade--作业2--2.查看dept表和emp表的结构DESCdept;DESCemp;--3.1显示所有部门的名称SELECTdnameFROMd......
  • linux openClouldOS 8.6安装最新版MySQL详细教程
    参考linux安装最新版MySQL详细教程rpm包下载其中下载MySQL官网的仓库文件,根据服务器情况选择的RedHatEnterpriseLinux8/OracleLinux8(ArchitectureIndependent),RPMPackage下载地址改为wgethttp://repo.mysql.com/mysql80-community-release-el8-5.noarch.rpm再......
  • docker部署mysql主从数据库
    一、mysql主库搭建1.1.14.xx.216作为mysql-master主库2.创建挂载数据目录mkdir-p/docker_v/mysql-master/{logs,conf,data}chmod777-R/docker_v/mysql-master/3.拉取镜像mysql:5.7并开放3306端口dockerpullmysql:5.74.在conf目录下创建文件my.cnf[mysqld]##设置se......
  • Java开发 - 读写分离初体验
    前言上一篇中,我们介绍了主从复制,相信学过的小伙伴已经能够很好的掌握主从复制的技术,实际上也并没有那么难,虽然没有讲一主多从,多主多从的配置,但是从一主一从的配置中也很容易联想到该怎么配置,你没猜错,就是你想的那样。这篇博客,我们要讲解的东西是主从复制的应用——读写分离。一般来......
  • 1-MySQL概述
    1.数据库相关概念数据库,数据库管理系统,SQL名称全称简称数据库存储数据的仓库,数据是有组织的进行存储DataBase(DB)数据库管理系统操纵和管理数据库的大型软件DataBaseManagementSystem(DBMS)SQL操作关系型数据库的编程语言,定义了一套操作关系型数据......
  • mysql如何创建数据库?
    创建数据库的命令格式:createdatabase数据库名称;通过按键盘上的【win】+【S】键执行搜索功能,在搜索框中输入“cmd”然后点击【以管理员身份运行】,如下图所示。     在cmd中输入“netstartmysql80”,并按下【enter】回车键,启动MySQL服务器,如下图所示。......
  • Mac连接Windows上的mysql服务器
    1、关闭Windows的防火墙2、在Windows上新建入站规则,允许访问3306端口  一直下一步,完成设置3、在Windows上更新mysql的root用户的host为'%'selecthost,userfromuser;updateusersethost='%'whereuser='root';flushprivileges; 4、在MAC上连接Windows上......
  • Windows10上安装mysql 8.0|mysql 8.0安装教程
    1、下载适合自己操作系统的mysql安装文件,解压就可直接用  下载地址:https://dev.mysql.com/downloads/mysql/ 2、解压压缩包,目录结构如下。解压后的压缩包里没有my.ini文件和data文件夹,my.ini可以自己手动建,但是data文件夹千万不可手动创建3、新建my.ini文件并添加最简配......
  • MySQL 时间类型 date、datetime 和 timestamp 的用法与区别
    时间范围datetime和timestamp区别时间范围不一样,TIMESTAMP要小很多,且最大范围为2038-01-1903:14:07.999999,到期也不远了。datetime与时区无关、timestamp与时区有关。对于timestamp,它把客户端插入的时间从当前时区转化为UTC(世界标准时间)进行存储。查询时,将其又转化为客户......