首页 > 数据库 >Mysql ProxySQL

Mysql ProxySQL

时间:2023-01-04 10:33:27浏览次数:49  
标签:monitor -- Mysql ProxySQL hostgroup mysql NULL id

rpm -ivh proxysql-2.4.1-1-centos7.x86_64.rpm

版本:proxysql --version
启动:service proxysql start
暂停:service proxysql stop
重启:service proxysql restart
状态:service proxysql status

控制台
mysql -uadmin -padmin -h127.0.0.1 -P6032 --prompt='Admin> ' --default-auth=mysql_native_password

--Main: 内存配置数据库,即 memory ,表里存放后端 db 实例,用户验证,路由规则等信息。 Main 库中有如下信息:
--
--mysql_servers-- 后端可以连接 mysql 服务器的列表
--
--mysql_users-- 配置后端数据库的账号和监控的账号
--
--mysql_query_rules-- 指定 query 路由到后端不同服务器的规则列表
--
--disk 库:持续化磁盘的配置。
--
--Stats 库:统计信息的汇总。
--
--Monitor 库:一些监控的收集信息,包括数据库的健康状态。

添加主从【ProxySQL】

insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'10.100.21.15',16330,1,'主库');
insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'10.100.21.18',16330,1,'从库1');
insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'10.100.21.19',16330,1,'从库2');

Admin> select * from mysql_servers;
+--------------+--------------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+--------------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 1 | 10.100.21.15 | 16330 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | 主库 |
| 2 | 10.100.21.18 | 16330 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | 从库1 |
| 2 | 10.100.21.19 | 16330 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | 从库2 |
+--------------+--------------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+

创建主从账号【MySQL】
CREATE USER 'proxysql'@'%' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON . TO 'proxysql'@'%' WITH GRANT OPTION;

添加主从账号【ProxySQL】
insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values('proxysql','123456',1,1);

查看主从账号【ProxySQL】

Admin> select * from mysql_users;
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+------------+---------+
| username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | attributes | comment |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+------------+---------+
| proxysql | 123456 | 1 | 0 | 1 | NULL | 0 | 1 | 0 | 1 | 1 | 10000 | | |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+------------+---------+

创建监控账号【MySQL】

CREATE USER 'monitor'@'%' IDENTIFIED BY 'monitor';
GRANT SELECT ON . TO 'monitor'@'%' WITH GRANT OPTION;

添加监控账号【ProxySQL】

set mysql-monitor_username='monitor';
set mysql-monitor_password='monitor';

查看监控账号【ProxySQL】

SELECT * FROM global_variables WHERE variable_name LIKE 'mysql-monitor_%';

//也可以这样快速定位
Admin> select @@mysql-monitor_username;
+--------------------------+
| @@mysql-monitor_username |
+--------------------------+
| monitor |
+--------------------------+
1 row in set (0.01 sec)

Admin> select @@mysql-monitor_password;
+--------------------------+
| @@mysql-monitor_password |
+--------------------------+
| monitor |
+--------------------------+
1 row in set (0.00 sec)

检测监控【ProxySQL】

读写映射【ProxySQL】
这里配置主从自动切换:互为主从,自动切换,保证高可用。

如果你没有做到互为主从,请跳过此项。

insert into mysql_replication_hostgroups (writer_hostgroup,reader_hostgroup) values(1,2);

Admin> select * from mysql_replication_hostgroups;
+------------------+------------------+------------+---------+
| writer_hostgroup | reader_hostgroup | check_type | comment |
+------------------+------------------+------------+---------+
| 1 | 2 | read_only | |
+------------------+------------------+------------+---------+

1 row in set (0.00 sec)

select * from mysql_server_read_only_log limit 3;

+--------------+-------+------------------+-----------------+-----------+-------+
| hostname | port | time_start_us | success_time_us | read_only | error |
+--------------+-------+------------------+-----------------+-----------+-------+
| 10.100.21.18 | 16330 | 1655950773100231 | 632 | 1 | NULL |
| 10.100.21.15 | 16330 | 1655950773114834 | 260 | 0 | NULL |
| 10.100.21.19 | 16330 | 1655950773130543 | 521 | 1 | NULL |
+--------------+-------+------------------+-----------------+-----------+-------+

读写路由【ProxySQL】

mysql_query_rules

常用的配置字段:
rule_id:规则编号,自动增长的整数,可以不指定;
active:规则是否有效,默认值为0,表示无效,需要在定义规则时,将其设置为1;
match_digest:定义规则的具体匹配内容;由正则表达式元字符组成,用来匹配SQL语句;
destination_hostgroup:对于符合规则的请求,设置目标主机组,从而实现路由转发;
apply:是否有效提交;默认值为0,表示无效,需要在定义规则时,将其值设置为1;

insert into mysql_query_rules(rule_id,active,match_pattern,destination_hostgroup,log,apply)values(1,1,'^UPDATE',1,1,1);
insert into mysql_query_rules(rule_id,active,match_pattern,destination_hostgroup,log,apply)values(3,1,'^SELECT',2,1,1);

--insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) values (2,1,'^SELECT.*FOR UPDATE$',1,1);
--insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) values (4,1,'^INSERT',1,1);

查看路由【ProxySQL】
Admin> select * from mysql_query_rules \G;
*************************** 1. row ***************************
rule_id: 1
active: 1
username: NULL
schemaname: NULL
flagIN: 0
client_addr: NULL
proxy_addr: NULL
proxy_port: NULL
digest: NULL
match_digest: NULL
match_pattern: ^UPDATE
negate_match_pattern: 0
re_modifiers: CASELESS
flagOUT: NULL
replace_pattern: NULL
destination_hostgroup: 1
cache_ttl: NULL
cache_empty_result: NULL
cache_timeout: NULL
reconnect: NULL
timeout: NULL
retries: NULL
delay: NULL
next_query_flagIN: NULL
mirror_flagOUT: NULL
mirror_hostgroup: NULL
error_msg: NULL
OK_msg: NULL
sticky_conn: NULL
multiplex: NULL
gtid_from_hostgroup: NULL
log: 1
apply: 1
attributes:
comment: NULL
*************************** 2. row ***************************
rule_id: 2
active: 1
username: NULL
schemaname: NULL
flagIN: 0
client_addr: NULL
proxy_addr: NULL
proxy_port: NULL
digest: NULL
match_digest: NULL
match_pattern: ^SELECT
negate_match_pattern: 0
re_modifiers: CASELESS
flagOUT: NULL
replace_pattern: NULL
destination_hostgroup: 2
cache_ttl: NULL
cache_empty_result: NULL
cache_timeout: NULL
reconnect: NULL
timeout: NULL
retries: NULL
delay: NULL
next_query_flagIN: NULL
mirror_flagOUT: NULL
mirror_hostgroup: NULL
error_msg: NULL
OK_msg: NULL
sticky_conn: NULL
multiplex: NULL
gtid_from_hostgroup: NULL
log: 1
apply: 1
attributes:
comment: NULL
2 rows in set (0.00 sec)

配置生效【ProxySQL】

//加载到内存
load mysql users to runtime;
load mysql servers to runtime;
load mysql query rules to runtime;
load mysql variables to runtime;
load admin variables to runtime;

//永久生效
save mysql users to disk;
save mysql servers to disk;
save mysql query rules to disk;
save mysql variables to disk;
save admin variables to disk;

验证【ProxySQL】

Admin> select * from stats_mysql_query_rules;
+---------+------+
| rule_id | hits |
+---------+------+
| 1 | 0 |
| 2 | 0 |
| 3 | 0 |
| 4 | 0 |
+---------+------+

2 rows in set (0.00 sec)

mysql -uproxysql -h10.100.21.15 -P6033 -p12356 -e 'select @@server_id for update;'
mysql -uproxysql -h10.100.21.15 -P6033 -p12356 -e 'select @@server_id;'

标签:monitor,--,Mysql,ProxySQL,hostgroup,mysql,NULL,id
From: https://blog.51cto.com/u_13482808/5987050

相关文章

  • LNMP架构环境之Mariadb数据库环境 nginx+php+mysql
    1)安装Mariadb数据库#1)安装Mariadb数据库yuminstallmariadb-servermariadb-y#2)启动Mariadb数据库,并加入开机自动复制代码systemctlstartmariadbsystemctle......
  • MySQL的优化多种方法(至少15条)
    转自:https://www.cnblogs.com/tdskee/p/16536166.htmlMYSQL的优化,是每一个程序员在做数据查询处理的时候,经常有的步骤那么SQL的优化有很多种,它可以是在硬件方面的,可以是在......
  • mysql5.7对json字段查询
    {"data":{"shipper":{"address":"No.236QianwangangRoad,WestCoastNewArea","province":"SHANDONG","cit......
  • MySQL记录锁、间隙锁、临键锁
    最近要在公司内做一次技术分享,思来想去不知道该分享些什么,最后在朋友的提示下,准备分享一下MySQL的InnoDB引擎下的事务幻读问题与解决方案--LBCC&MVCC。经过好几天的熬夜通......
  • 从零开始学 MySQL -- 数据库和数据表操作
    阅读本文大概需要7 分钟前言上篇文章我们学习了SELECT语句,今天我们学习下核心的内容,学习并实践如何对数据库表和表中的内容做修改,删除,重命名等操作。(想看看周末还有多少......
  • 从零开始学习 MySQL 系列--索引、视图、导入和导出
    前言上篇文章我们学习了数据库和数据表操作语句,今天我们学习下数据库索引,视图,导入和导出的知识。作为基础篇,不会涉及到关于索引和视图的高级应用和核心概念,但是基本操作大家......
  • Mysql 访问视图却报ERROR 1356 (HY000)错误
    目录1.适用范围2.问题概述3.问题原因3.1.通过root用户查询问题仍然存在,且所有视图问题都是相同的。3.2.root自己的创建的视图可以正常访问3.3.查看用户权限3.4.以为是权......
  • Mysql安装
    1、windows安装mysql有两种安装方式,一种是使用exe安装程序去安装,一种是直接解压安装。推荐使用解压安装的方式,因为exe程序安装的方式,安装过程中会和操作系统有过多的关......
  • Mysql读写分离
    MySQL主从复制介绍:MySQL主从复制是一个异步的复制过程,底层是基于Mysql数据库自带的二进制日志功能。就是一台或多台MySOL数据库(slave,即从库)从另一台MySOL数据库(master......
  • Centos7离线安装Mysql8(rpm安装)
    1.下载:        官网下载: MySQL::DownloadMySQLCommunityServer2.解压将下载好的tar文件放到centos中,目录文件夹名称自定义,解压后得到:[root@localhost......