proxysql 2.6部署代理MGR集群读写分离

官方文档

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

下载安装proxysql

https://github.com/sysown/proxysql/releases/download/v2.6.0/proxysql-2.6.0-1-centos7.x86_64.rpmyum -y localinstall proxysql-2.6.0-1-centos7.x86_64.rpm
# 软链接数据目录
rm -rf /var/lib/proxysql/
mkdir -p /data/proxysql
ln -s /data/proxysql/ /var/lib/
chown -R proxysql.proxysql /var/lib/proxysql/
chown -R proxysql.proxysql /var/lib/proxysql# 启动proxysql
systemctl start proxysql
systemctl status proxysql
systemctl enable proxysql

防火墙放行

firewall-cmd --permanent --zone=public --add-port={6032..6033}/tcp && firewall-cmd --reload

下载安装mysql客户端

tar -zxvf mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar.gz
rpm -ivh mysql-community-client-8.0.19-1.el7.x86_64.rpm --nodeps --force

连接proxy管理接口配置

mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> '# 确认mysql_servers、mysql_replication_hostgroups、mysql_query_rules三张表的配置是否为空
SELECT * FROM mysql_servers;
SELECT * from mysql_replication_hostgroups;
SELECT * from mysql_query_rules;
proxysql添加后端mysql
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,'192.168.200.31',3308);
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,'192.168.200.32',3308);
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,'192.168.200.33',3308);load mysql servers to runtime;
save mysql servers to disk;

MGR集群主节点添加监控账号

create user 'monitor'@'192.168.200.%' identified with mysql_native_password by 'UBXWlwDw4X93s%#GtXyx';GRANT SELECT on sys.* to 'monitor'@'192.168.200.%';
GRANT SELECT on performance_schema.* to 'monitor'@'192.168.200.%';

proxysql添加设置监控账号

UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_username';
UPDATE global_variables SET variable_value='UBXWlwDw4X93s%#GtXyx' WHERE variable_name='mysql-monitor_password';load mysql variables to runtime;
save mysql variables to disk;

MGR集群主节点添加监控视图

# addition_to_sys.sql MGR创建监控视图sql文件
https://lefred.be/content/mysql-group-replication-native-support-in-proxysql/
cat > addition_to_sys.sql << EOF
USE sys;DELIMITER $$CREATE FUNCTION IFZERO(a INT, b INT)
RETURNS INT
DETERMINISTIC
RETURN IF(a = 0, b, a)$$CREATE FUNCTION LOCATE2(needle TEXT(10000), haystack TEXT(10000), offset INT)
RETURNS INT
DETERMINISTIC
RETURN IFZERO(LOCATE(needle, haystack, offset), LENGTH(haystack) + 1)$$CREATE FUNCTION GTID_NORMALIZE(g TEXT(10000))
RETURNS TEXT(10000)
DETERMINISTIC
RETURN GTID_SUBTRACT(g, '')$$CREATE FUNCTION GTID_COUNT(gtid_set TEXT(10000))
RETURNS INT
DETERMINISTIC
BEGINDECLARE result BIGINT DEFAULT 0;DECLARE colon_pos INT;DECLARE next_dash_pos INT;DECLARE next_colon_pos INT;DECLARE next_comma_pos INT;SET gtid_set = GTID_NORMALIZE(gtid_set);SET colon_pos = LOCATE2(':', gtid_set, 1);WHILE colon_pos != LENGTH(gtid_set) + 1 DOSET next_dash_pos = LOCATE2('-', gtid_set, colon_pos + 1);SET next_colon_pos = LOCATE2(':', gtid_set, colon_pos + 1);SET next_comma_pos = LOCATE2(',', gtid_set, colon_pos + 1);IF next_dash_pos < next_colon_pos AND next_dash_pos < next_comma_pos THENSET result = result +SUBSTR(gtid_set, next_dash_pos + 1,LEAST(next_colon_pos, next_comma_pos) - (next_dash_pos + 1)) -SUBSTR(gtid_set, colon_pos + 1, next_dash_pos - (colon_pos + 1)) + 1;ELSESET result = result + 1;END IF;SET colon_pos = next_colon_pos;END WHILE;RETURN result;
END$$CREATE FUNCTION gr_applier_queue_length()
RETURNS INT
DETERMINISTIC
BEGINRETURN (SELECT sys.gtid_count( GTID_SUBTRACT( (SELECT
Received_transaction_set FROM performance_schema.replication_connection_status
WHERE Channel_name = 'group_replication_applier' ), (SELECT
@@global.GTID_EXECUTED) )));
END$$CREATE FUNCTION gr_member_in_primary_partition()
RETURNS VARCHAR(3)
DETERMINISTIC
BEGINRETURN (SELECT IF( MEMBER_STATE='ONLINE' AND ((SELECT COUNT(*) FROM
performance_schema.replication_group_members WHERE MEMBER_STATE != 'ONLINE') >=
((SELECT COUNT(*) FROM performance_schema.replication_group_members)/2) = 0),
'YES', 'NO' ) FROM performance_schema.replication_group_members JOIN
performance_schema.replication_group_member_stats USING(member_id));
END$$CREATE VIEW gr_member_routing_candidate_status AS SELECT
sys.gr_member_in_primary_partition() as viable_candidate,
IF( (SELECT (SELECT GROUP_CONCAT(variable_value) FROM
performance_schema.global_variables WHERE variable_name IN ('read_only',
'super_read_only')) != 'OFF,OFF'), 'YES', 'NO') as read_only,
sys.gr_applier_queue_length() as transactions_behind, Count_Transactions_in_queue as 'transactions_to_cert' from performance_schema.replication_group_member_stats;$$DELIMITER ;
EOF
cat > addition_to_sys2.sql << EOF
USE sys;DELIMITER $$DROP FUNCTION gr_member_in_primary_partition$$DROP VIEW gr_member_routing_candidate_status$$CREATE FUNCTION gr_member_in_primary_partition()
RETURNS VARCHAR(3)
DETERMINISTIC
BEGINRETURN (SELECT IF( MEMBER_STATE='ONLINE' AND ((SELECT COUNT(*) FROM
performance_schema.replication_group_members WHERE MEMBER_STATE != 'ONLINE') >=
((SELECT COUNT(*) FROM performance_schema.replication_group_members)/2) = 0),
'YES', 'NO' ) FROM performance_schema.replication_group_members JOIN
performance_schema.replication_group_member_stats rgms USING(member_id) WHERE rgms.MEMBER_ID=@@SERVER_UUID);
END$$CREATE VIEW gr_member_routing_candidate_status AS SELECT
sys.gr_member_in_primary_partition() as viable_candidate,
IF( (SELECT (SELECT GROUP_CONCAT(variable_value) FROM
performance_schema.global_variables WHERE variable_name IN ('read_only',
'super_read_only')) != 'OFF,OFF'), 'YES', 'NO') as read_only,
sys.gr_applier_queue_length() as transactions_behind, Count_Transactions_in_queue as 'transactions_to_cert' 
from performance_schema.replication_group_member_stats rgms 
where rgms.MEMBER_ID=(select gv.VARIABLE_VALUE from `performance_schema`.global_variables gv where gv.VARIABLE_NAME='server_uuid');$$DELIMITER ;
EOF

加载视图sql

source /root/addition_to_sys.sql;

查看视图监控状态

select * from gr_member_routing_candidate_status;

如果报错

ERROR 1242 (21000): Subquery returns more than 1 row
source /root/addition_to_sys2.sql;

proxysql设置读写组

# 查看读写复制组表insert into mysql_group_replication_hostgroups(writer_hostgroup,backup_writer_hostgroup,reader_hostgroup,offline_hostgroup,active,max_writers,writer_is_also_reader,max_transactions_behind) 
values(10,20,30,40,1,1,0,0);load mysql servers to runtime;
save mysql servers to disk;

MGR集群状态查询

mysql> select * from sys.gr_member_routing_candidate_status;
+------------------+-----------+---------------------+----------------------+
| viable_candidate | read_only | transactions_behind | transactions_to_cert |
+------------------+-----------+---------------------+----------------------+
| YES              | NO        |                   0 |                    0 |
+------------------+-----------+---------------------+----------------------+
1 row in set (0.04 sec)

proxysql状态查询

Admin> select hostgroup_id, hostname, status  from runtime_mysql_servers;
+--------------+----------------+--------+
| hostgroup_id | hostname       | status |
+--------------+----------------+--------+
| 10           | 192.168.200.32 | ONLINE |
| 20           | 192.168.200.31 | ONLINE |
| 20           | 192.168.200.33 | ONLINE |
+--------------+----------------+--------+

MGR主节点创建业务账号

create user 'gadmin'@'192.168.200.%' identified with mysql_native_password by 'UBXWlwDw4X93s%#GtXyx';GRANT all privileges on *.* to 'gadmin'@'192.168.200.%';

proxysql添加业务账号

INSERT INTO mysql_users(username,password,default_hostgroup) VALUES ('gadmin','UBXWlwDw4X93s%#GtXyx',10);LOAD MYSQL USERS TO RUNTIME;
SAVE MYSQL USERS TO DISK;SELECT * FROM mysql_users;

proxysql添加读写分离规则

INSERT INTO mysql_query_rules (rule_id,active,username,match_digest,destination_hostgroup,apply) VALUES (10,1,'gadmin','^SELECT.*FOR UPDATE$',10,1);INSERT INTO mysql_query_rules (rule_id,active,username,match_digest,destination_hostgroup,apply) VALUES (20,1,'gadmin','^SELECT',30,1);load mysql query rules to runtime;
save mysql query rules to disk;# 查看规则
SELECT match_digest,destination_hostgroup FROM mysql_query_rules WHERE active=1 AND username='gadmin' ORDER BY rule_id;# 查看路由状态
SELECT hostgroup hg, sum_time, count_star, digest_text FROM stats_mysql_query_digest ORDER BY sum_time DESC;

测试读写分离

mysql -ugadmin -p'UBXWlwDw4X93s%#GtXyx' -h192.168.200.30 -P6033 -e 'create database proxysql_mgr_test;'mysql -ugadmin -p'UBXWlwDw4X93s%#GtXyx' -h192.168.200.30 -P6033 -e 'use proxysql_mgr_test;select * from tb01;'mysql -ugadmin -p'UBXWlwDw4X93s%#GtXyx' -h192.168.200.30 -P6033 -e 'show databases;'mysql -ugadmin -p'UBXWlwDw4X93s%#GtXyx' -h192.168.200.30 -P6033 -e 'drop database proxysql_mgr_test;'

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/725194.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ASPICE实操中的那点事儿-如何解决上、下游一致性难以保证的问题

写在前面 ASPICE理解起来容易&#xff0c;毕竟是有条有理的。但实操起来&#xff0c;尤其是把ASPICE各过程域做全的时候&#xff0c;会遇到各种各样的问题&#xff08;不是技术问题有多难&#xff0c;而是该如何做选择&#xff0c;如何既能符合ASPICE要求&#xff0c;保证过程质…

外包干了10天,技术退步明显。。。。。

先说一下自己的情况&#xff0c;本科生&#xff0c;2019年我通过校招踏入了南京一家软件公司&#xff0c;开始了我的职业生涯。那时的我&#xff0c;满怀热血和憧憬&#xff0c;期待着在这个行业中闯出一片天地。然而&#xff0c;随着时间的推移&#xff0c;我发现自己逐渐陷入…

Java必须掌握的莫夫曼树(含面试大厂题和源码)

面试题&#xff1a;构建哈夫曼树 相关知识点&#xff1a; 哈夫曼树&#xff08;Huffman Tree&#xff09;&#xff1a;哈夫曼树是一种用于数据压缩的树形结构。它是一种最优二叉树&#xff0c;其特点是频率高的字符出现在树的顶部&#xff0c;频率低的字符出现在树的底部&…

2024年阿里云域名优惠口令更新,亲测有效口令大全

2024年阿里云域名优惠口令&#xff0c;com域名续费优惠口令“com批量注册更享优惠”&#xff0c;cn域名续费优惠口令“cn注册多个价格更优”&#xff0c;cn域名注册优惠口令“互联网上的中国标识”&#xff0c;阿里云优惠口令是域名专属的优惠码&#xff0c;可用于域名注册、续…

OCR图片预处理之去除红色水印

import cv2 读取图像 src cv2.imread(“page-2_0.jpg”) if src is None: print(“Fail to open image!”) exit() 将图像转换为灰度图 gray cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) # 全局二值化 th 180 # 阈值要根据实际情况调整 binary cv2.threshold(gray, t…

aiofiles,一个超酷的 Python 异步编程库!

目录 前言 什么是aiofiles库&#xff1f; 安装aiofiles库 基本功能 1. 异步打开文件 2. 异步读取文件 3. 异步写入文件 4. 异步追加内容到文件 应用场景 1. 异步Web服务器 2. 异步数据处理 3. 异步日志记录 总结 前言 大家好&#xff0c;今天为大家分享一个超酷的 Pytho…

stack/queue

链表完了之后就是我们的栈和队列了&#xff0c;当然我们的STL中也有实现&#xff0c;下面我们先来看一下简单用法&#xff0c;跟我们之前C语言实现的一样&#xff0c;stack和queue有这么几个重要的成员函数 最主要的就是这么几个&#xff1a;empty&#xff0c;push&#xff0c;…

Spring揭秘:ImportBeanDefinitionRegistrar应用场景及实现原理!

内容概念 ImportBeanDefinitionRegistrar接口提供了强大的动态注册Bean的能力&#xff0c;它允许开发者在Spring容器初始化时&#xff0c;灵活地根据特定条件或需求来添加或修改Bean定义&#xff0c;从而实现更为精细的控制和扩展性。这是构建可扩展框架、插件系统或处理复杂配…

2024.03.05作业

select实现tcp并发服务器 #include "test.h"#define SER_IP "192.168.42.106" #define SER_PORT 8888int create_socket() {int sfd socket(AF_INET, SOCK_STREAM, 0);if(sfd -1){perror("socket error");exit(-1);}printf("sfd %d\n&q…

安装VMWare+创建Linux虚拟机

点击VMware官网进入官网&#xff0c;下载VMware安装包。 一、安装VMware 一、安装VMware软件 &#xff08;1&#xff09;点击下一步 &#xff08;2&#xff09;勾选“我接受许可协议中的条款”&#xff0c;再点 ”击下一步“。 &#xff08;3&#xff09;选择下一步&#xf…

Linux编程3.2 进程-C程序启动过程

正常情况Linux 启动流程&#xff1a; ① BIOS 启动&#xff0c;完成自检&#xff0c;选择启动硬件 ②如果是磁盘系统读取 MBR ③从 MBR 指示&#xff0c;找到 GRUB 所在分区&#xff0c;加载 GRUB 显示菜单 ④加载 Linux 内核到内存中 ⑤执行 INIT 程序 ⑥进入用户界面 1、内核…

nvm安装、部署以及使用

1. nvm安装 官方地址&#xff1a;https://github.com/nvm-sh/nvm/blob/master/README.md nvm window安装&#xff1a;https://github.com/coreybutler/nvm-windows/releases 看个人习惯&#xff0c;通过不同形式来安装nvm&#xff0c;省劲就用.exe安装即可。 Tips&#xff1…

「连载」边缘计算(二十五)03-05:边缘部分源码(源码分析篇)

&#xff08;接上篇&#xff09; 1&#xff09;EdgehubConfig初始化具体如下所示。 config.InitEdgehubConfig() config.InitEdgehubConfig()函数定义具体如下所示。 KubeEdge/edge/pkg/edgehub/config/config.go // InitEdgehubConfig init edgehub config func InitEdgeh…

代码随想录算法训练营第三十四天|LeetCode860 柠檬水找零、LeetCode406 根据身高重建队列、LeetCode452 用最少数量的箭引爆气球

860.柠檬水找零 思路&#xff1a;只有5、10、20三种面额的纸币&#xff0c;因此每接收一种纸币对应的数量就&#xff0c;当接收10的&#xff0c;5的数量就--&#xff0c;当接收20的&#xff0c;有限消耗10的纸币&#xff08;贪心&#xff09;&#xff0c;因为10只能用于20找零…

【Flink网络数据传输(4)】RecordWriter(下)封装数据并发送到网络的过程

文章目录 一. RecordWriter封装数据并发送到网络1. 数据发送到网络的具体流程2. 源码层面2.1. Serializer的实现逻辑a. SpanningRecordSerializer的实现b. SpanningRecordSerializer中如何对数据元素进行序列化 2.2. 将ByteBuffer中间数据写入BufferBuilder 二. BufferBuilder申…

FreeRtos自学笔记3-----参考正点原子视频

FreeRtos任务的创建与删除 任务的创建与删除本质上是调用FreeRtos的API函数。 API函数&#xff1a; 1.xTaskGreate():动态创建任务函数&#xff1b; 2.xTaskGreateStatic();静态创建任务函数&#xff1b; 3.xTaskDelete():任务删除 动态创建任务&#xff1a;任务的任务控制块以…

java 中 string常用方法及相关的例子

我将为您详细讲解 Java 中 String 类的常用方法及其相关例子。String 类是 Java 中最常用的类之一&#xff0c;它代表字符串&#xff0c;提供了许多用于操作字符串的方法。 1. 字符串比较 - equals(Object obj): 比较字符串的内容是否相等。 - equalsIgnoreCase(String str): 比…

自研cloud框架专题

自己开发的cloud框架,可作为企业java web开发底层依赖,提供多个模块能力,降低搭建环境难度,增强模块能力. 开源地址:https://github.com/2892824942/ty-cloud 自研cloud框架专题–框架介绍&#xff08;零&#xff09; 自研cloud框架专题–mybatis-puls模块&#xff08;一&…

Linux 如何安装python

【我的系统是Centos7】在 linux上安装Python之前需要先安装前置依赖程序. 登录Linux中,使用 yum程序进行依赖程序安装,执行如下命令: yum install wget bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make zlib zlib-devel libffi-dev…

如何利用python实现自己的modbus-tcp库

如果你想使用纯Socket编程来实现Modbus TCP通讯,而不是依赖于Modbus库,你需要理解Modbus TCP协议的细节,并能够手动构建和解析Modbus消息。以下是一个简单的示例,展示了如何使用Python的socket库来实现Modbus TCP通讯: 了解Modbus TCP协议: Modbus TCP协议使用TCP作为底层…