当前位置: 首页 > news >正文

Mysql(MGR)和ProxySQL搭建部署-Docker版本

项目路径:D:\study\backend\mysql\mgr

一、Mysql(MGR)

 1.1 docker-compose.yaml

 volumes:
     # MySQL配置文件目录
     - "./mysql-1/conf.d/my.cnf:/etc/mysql/my.cnf"
     # MySQL工作目录
     - "./mysql-1/data:/var/lib/mysql"
     # MySQL用于文件导入导出操作的默认存储目录
     - "./mysql-1/mysql-files:/var/lib/mysql-files"
     # MySQL第一次启动自动加载的SQL文件目录
     - "./mysql-1/init:/docker-entrypoint-initdb.d"

# 域名解析
extra_hosts:
      - "n1:192.168.100.11"
      - "n2:192.168.100.12"
      - "n3:192.168.100.13"

expose:
      # MySQL通讯端口
      - "3306"
      # MGR集群通讯端口
      - "24901"

version: "3"
services:mysql-1:image: mysql:8.0.28container_name: mysql-1hostname: n1networks:mysql:ipv4_address: 192.168.100.11  # 指定静态 IP 地址extra_hosts:- "n1:192.168.100.11"- "n2:192.168.100.12"- "n3:192.168.100.13"volumes:- "./mysql-1/conf.d/my.cnf:/etc/mysql/my.cnf"- "./mysql-1/data:/var/lib/mysql"- "./mysql-1/mysql-files:/var/lib/mysql-files"- "./mysql-1/init:/docker-entrypoint-initdb.d"environment:TZ: Asia/ShanghaiMYSQL_ROOT_PASSWORD: rootmcafee123ports:- "33061:3306"expose:- "3306"- "24901"mysql-2:image: mysql:8.0.28container_name: mysql-2hostname: n2networks:mysql:ipv4_address: 192.168.100.12  # 指定静态 IP 地址extra_hosts:- "n1:192.168.100.11"- "n2:192.168.100.12"- "n3:192.168.100.13"volumes:- "./mysql-2/conf.d/my.cnf:/etc/mysql/my.cnf"- "./mysql-2/data:/var/lib/mysql"- "./mysql-2/mysql-files:/var/lib/mysql-files"- "./mysql-2/init:/docker-entrypoint-initdb.d"environment:TZ: Asia/ShanghaiMYSQL_ROOT_PASSWORD: rootmcafee123ports:- "33062:3306"expose:- "3306"- "24901"mysql-3:image: mysql:8.0.28container_name: mysql-3hostname: n3networks:mysql:ipv4_address: 192.168.100.13  # 指定静态 IP 地址extra_hosts:- "n1:192.168.100.11"- "n2:192.168.100.12"- "n3:192.168.100.13"volumes:- "./mysql-3/conf.d/my.cnf:/etc/mysql/my.cnf"- "./mysql-3/data:/var/lib/mysql"- "./mysql-3/mysql-files:/var/lib/mysql-files"- "./mysql-3/init:/docker-entrypoint-initdb.d"environment:TZ: Asia/ShanghaiMYSQL_ROOT_PASSWORD: rootmcafee123ports:- "33063:3306"expose:- "3306"- "24901"
networks:mysql:driver: bridge  # 使用默认的桥接网络驱动ipam:driver: default  # 默认 IPAM 驱动config:- subnet: 192.168.100.0/24  # 指定网络的子网gateway: 192.168.100.1     # 指定网络的网关

1.2 mysql-1

1.2.1 mysql-1/conf.d/my.cnf

# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL# Custom config should go here
!includedir /etc/mysql/conf.d/
#使⽤mysql_native_password密码策略,防⽌navicat连不上mysql8
default_authentication_plugin=mysql_native_password
#设置MySQL插件所在⽬录,因为MGR基于插件,所以必须设置插件路径
plugin_dir=/usr/lib/mysql/plugin
#服务器编号
server_id=1
#开启binlog的GTID模式
gtid_mode=ON
#开启后MySQL只允许能够保障事务安全,并且能够被⽇志记录的SQL语句被执⾏
enforce_gtid_consistency=ON
#关闭binlog校验
binlog_checksum=NONE
#定义⽤于事务期间哈希写⼊提取的算法,组复制模式下必须设置为 XXHASH64
transaction_write_set_extraction=XXHASH64
#确定组复制恢复时是否应该应⽤ SSL,通常设置为“开”,但默认设置为“关”
loose-group_replication_recovery_use_ssl=ON
#该服务器的实例所在复制组的名称,必须是有效的 UUID,所有节点必须相同
loose-group_replication_group_name="bbbbbbbb-bbbb-cccc-dddd-eeeeeeeeeeee"
#确定服务器是否应该在服务器启动期间启动组复制
loose-group_replication_start_on_boot=OFF
#为复制组中其他的成员提供的⽹络地址,指定为“主机:端⼝”的格式化字符串。
#很多⼈想当然认为端⼝应该是3306,起始不然,MGR需要开启新端⼝24901同步交换
#所以这⾥不要写错,同时,前⾯我们配置了hosts⽂件做了主机名与IP的映射,这⾥直接写主机名即可
loose-group_replication_local_address="n1:24901"
#⽤于建⽴新成员到组的连接组成员列表。
#这个列表指定为由分隔号间隔的组成员⽹络地址列表,类似 host1:port1、host2:port2 的格式。
#同样采⽤n0~n2的主机名替代
loose-group_replication_group_seeds="n1:24901,n2:24901,n3:24901"
#配置此服务器为引导组,这个选项必须仅在⼀台服务器上设置,
#并且仅当第⼀次启动组或者重新启动整个组时。成功引导组启动后,将此选项设置为关闭
loose-group_replication_bootstrap_group=OFF

1.2.2 mysql-1/init/init.sql

#创建rpl_user账户,此账户⽤于实现主从数据同步

CREATE USER rpl_user@'%' IDENTIFIED BY 'asAS123456!';

#赋予主从同步权限
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';

#让刚才的修改⽣效
FLUSH PRIVILEGES;

#删除已产⽣的Binlog

#⼀定要RESET MASTER,它会删除刚才已产⽣的Binlog

#因为刚才Binglog包含创建⽤户这种⾼权限操作,⽤于主从同步的rpl_user账户是没有权限执⾏的

#这就会导致RelayLog重放⽆法正确执⾏,导致从属服务器卡死在"RECEVING"状态

#利⽤RESET MASTER删除这些⽆法执⾏的binlog,就没问题了
RESET MASTER;

#安装MGR插件
INSTALL PLUGIN group_replication SONAME 'group_replication.so';

CREATE USER rpl_user@'%' IDENTIFIED BY 'asAS123456!';
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
FLUSH PRIVILEGES;
RESET MASTER;
INSTALL PLUGIN group_replication SONAME 'group_replication.so';
/*
查看插件安装是否成功
SELECT * FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME = 'group_replication' \G;
*/
/*主节点也要执行,主节点重启后,也会变成从节点*/
CHANGE MASTER TO MASTER_USER="rpl_user", MASTER_PASSWORD="asAS123456!" FOR CHANNEL 'group_replication_recovery';
/*
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;SELECT * FROM performance_schema.replication_group_members;
*/

1.2.3 mysql-1/mysql-files/proxysql.sql(仅在主节点执行,同步给从节点)

proxysql.sql是使用proxysql的前置条件

# 执行命令
mysql -uroot -prootmcafee123 < /var/lib/mysql-files/proxysql.sql
/*
mysql -uroot -prootmcafee123 < /var/lib/mysql-files/proxysql.sql
*/
use sys;
DELIMITER $$
CREATE USER 'monitor'@'%' IDENTIFIED BY "monitor@1025";
CREATE USER 'proxysql'@'%' IDENTIFIED BY "proxysql@1025";
GRANT ALL PRIVILEGES ON *.* TO 'monitor'@'%' ;
GRANT ALL PRIVILEGES ON *.* TO 'proxysql'@'%' ;
FLUSH PRIVILEGES;
CREATE FUNCTION my_id() RETURNS TEXT(36) DETERMINISTIC NO SQL RETURN (SELECT @@global.server_uuid as my_id);$$
CREATE FUNCTION gr_member_in_primary_partition()RETURNS VARCHAR(3)DETERMINISTICBEGINRETURN (SELECT IF( MEMBER_STATE='ONLINE' AND ((SELECT COUNT(*) FROMperformance_schema.replication_group_members WHERE MEMBER_STATE NOT IN ('ONLINE', 'RECOVERING')) >=((SELECT COUNT(*) FROM performance_schema.replication_group_members)/2) = 0),'YES', 'NO' ) FROM performance_schema.replication_group_members JOINperformance_schema.replication_group_member_stats USING(member_id) where member_id=my_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,
Count_Transactions_Remote_In_Applier_Queue as transactions_behind, Count_Transactions_in_queue as 'transactions_to_cert'
from performance_schema.replication_group_member_stats where member_id=my_id();$$

# 选择数据库

use sys;
# 创建proxysql监控用户
CREATE USER 'monitor'@'%' IDENTIFIED BY "monitor@1025";

# 创建应用连接proxysql用户
CREATE USER 'proxysql'@'%' IDENTIFIED BY "proxysql@1025";

#赋权
GRANT ALL PRIVILEGES ON *.* TO 'monitor'@'%' ;

#赋权
GRANT ALL PRIVILEGES ON *.* TO 'proxysql'@'%' ;

#生效
FLUSH PRIVILEGES;

# proxysql监控mysql mgr集群状态的视图

# 函数

 my_id()

# 函数

gr_member_in_primary_partition()

# 视图

gr_member_routing_candidate_status

注意:一定要使用DELIMITER $$,否则执行创建函数和视图会报错

1.3 mysql-2

1.3.1 mysql-1/conf.d/my.cnf

# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL# Custom config should go here
!includedir /etc/mysql/conf.d/
default_authentication_plugin=mysql_native_password
plugin_dir=/usr/lib/mysql/plugin
server_id=2
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_checksum=NONE
loose-group_replication_recovery_get_public_key=ON
loose-group_replication_recovery_use_ssl=ON
loose-group_replication_group_name="bbbbbbbb-bbbb-cccc-dddd-eeeeeeeeeeee"
loose-group_replication_start_on_boot=OFF
loose-group_replication_local_address="n2:24901"
loose-group_replication_group_seeds="n1:24901,n2:24901,n3:24901"
loose-group_replication_bootstrap_group=OFF

1.3.2 mysql-1/init/init.sql

CREATE USER rpl_user@'%' IDENTIFIED BY 'asAS123456!';
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
FLUSH PRIVILEGES;
RESET MASTER;
INSTALL PLUGIN group_replication SONAME 'group_replication.so';
/*
SELECT * FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME = 'group_replication' \G;
*/
CHANGE MASTER TO MASTER_USER="rpl_user", MASTER_PASSWORD="asAS123456!" FOR CHANNEL 'group_replication_recovery';
/*
START GROUP_REPLICATION;
SELECT * FROM performance_schema.replication_group_members;
*/

1.4 mysql-3

1.4.1 mysql-1/conf.d/my.cnf

# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL# Custom config should go here
!includedir /etc/mysql/conf.d/
default_authentication_plugin=mysql_native_password
plugin_dir=/usr/lib/mysql/plugin
server_id=3
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_checksum=NONE
loose-group_replication_recovery_get_public_key=ON
loose-group_replication_recovery_use_ssl=ON
loose-group_replication_group_name="bbbbbbbb-bbbb-cccc-dddd-eeeeeeeeeeee"
loose-group_replication_start_on_boot=OFF
loose-group_replication_local_address="n3:24901"
loose-group_replication_group_seeds="n1:24901,n2:24901,n3:24901"
loose-group_replication_bootstrap_group=OFF

1.4.2 mysql-1/init/init.sql

CREATE USER rpl_user@'%' IDENTIFIED BY 'asAS123456!';
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
FLUSH PRIVILEGES;
RESET MASTER;
INSTALL PLUGIN group_replication SONAME 'group_replication.so';
/*
SELECT * FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME = 'group_replication' \G;
*/
CHANGE MASTER TO MASTER_USER="rpl_user", MASTER_PASSWORD="asAS123456!" FOR CHANNEL 'group_replication_recovery';
/*
START GROUP_REPLICATION;
SELECT * FROM performance_schema.replication_group_members;
*/

 1.5、启动

cd  D:\study\backend\mysql\mgrdocker-compose up -d# mysql-1
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;
# mysql-2
START GROUP_REPLICATION;
# mysql-3
START GROUP_REPLICATION;#查看集群状态
SELECT * FROM performance_schema.replication_group_members;#若搭配使用中间件proxysql,待集群状态正常后到mysql-1节点(master)执行proxysql.sql
mysql -uroot -prootmcafee123 < /var/lib/mysql-files/proxysql.sql

二、ProxySQL

项目地址:D:\study\backend\proxysql

2.1 docker-compose.yaml

version: "3"
services:proxysql:image: proxysql:2.6.5container_name: proxysqlnetworks:mgr_mysql:ipv4_address: 192.168.100.14  # 指定静态 IP 地址extra_hosts:- "n1:192.168.100.11"- "n2:192.168.100.12"- "n3:192.168.100.13"volumes:- "./proxysql.cnf:/etc/proxysql.cnf"- "./data:/var/lib/proxysql"environment:TZ: Asia/Shanghaiports:- "6033:6033"expose:- "6033"- "6032"- "6080"
networks:mgr_mysql:external: true
#networks:
#  mysql:
#    driver: bridge  # 使用默认的桥接网络驱动
#    ipam:
#      driver: default  # 默认 IPAM 驱动
#      config:
#      - subnet: 192.168.100.0/24  # 指定网络的子网
#        gateway: 192.168.100.1     # 指定网络的网关

2.2 proxysql.cnf

#file proxysql.cfg########################################################################################
# This config file is parsed using libconfig , and its grammar is described in:        
# http://www.hyperrealm.com/libconfig/libconfig_manual.html#Configuration-File-Grammar 
# Grammar is also copied at the end of this file                                       
################################################################################################################################################################################
# IMPORTANT INFORMATION REGARDING THIS CONFIGURATION FILE:                             
########################################################################################
# On startup, ProxySQL reads its config file (if present) to determine its datadir. 
# What happens next depends on if the database file (disk) is present in the defined
# datadir (i.e. "/var/lib/proxysql/proxysql.db").
#
# If the database file is found, ProxySQL initializes its in-memory configuration from 
# the persisted on-disk database. So, disk configuration gets loaded into memory and 
# then propagated towards the runtime configuration. 
#
# If the database file is not found and a config file exists, the config file is parsed 
# and its content is loaded into the in-memory database, to then be both saved on-disk 
# database and loaded at runtime.
#
# IMPORTANT: If a database file is found, the config file is NOT parsed. In this case
#            ProxySQL initializes its in-memory configuration from the persisted on-disk
#            database ONLY. In other words, the configuration found in the proxysql.cnf
#            file is only used to initial the on-disk database read on the first startup.
#
# In order to FORCE a re-initialise of the on-disk database from the configuration file 
# the ProxySQL service should be started with "systemctl start proxysql-initial".
#
########################################################################################datadir="/var/lib/proxysql"
errorlog="/var/lib/proxysql/proxysql.log"admin_variables=
{admin_credentials="admin:admin"
#	mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock"mysql_ifaces="0.0.0.0:6032"
#	refresh_interval=2000
#	debug=true
}mysql_variables=
{threads=4max_connections=2048default_query_delay=0default_query_timeout=36000000have_compress=truepoll_timeout=2000
#	interfaces="0.0.0.0:6033;/tmp/proxysql.sock"interfaces="0.0.0.0:6033"default_schema="information_schema"stacksize=1048576server_version="8.0.28 (ProxySQL)"connect_timeout_server=3000
# make sure to configure monitor username and password
# https://github.com/sysown/proxysql/wiki/Global-variables#mysql-monitor_username-mysql-monitor_passwordmonitor_username="monitor"monitor_password="monitor@1025"monitor_history=600000monitor_connect_interval=60000monitor_ping_interval=10000monitor_read_only_interval=1500monitor_read_only_timeout=500ping_interval_server_msec=120000ping_timeout_server=500commands_stats=truesessions_sort=trueconnect_retries_on_failure=10
}# defines all the MySQL servers
mysql_servers =
(
#	{
#		address = "127.0.0.1" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
#		port = 3306           # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
#		hostgroup = 0	        # no default, required
#		status = "ONLINE"     # default: ONLINE
#		weight = 1            # default: 1
#		compression = 0       # default: 0
#   max_replication_lag = 10  # default 0 . If greater than 0 and replication lag passes such threshold, the server is shunned
#	},
#	{
#		address = "/var/lib/mysql/mysql.sock"
#		port = 0
#		hostgroup = 0
#	},
#	{
#		address="127.0.0.1"
#		port=21891
#		hostgroup=0
#		max_connections=200
#	},
#	{ address="127.0.0.2" , port=3306 , hostgroup=0, max_connections=5 },
#	{ address="127.0.0.1" , port=21892 , hostgroup=1 },
#	{ address="127.0.0.1" , port=21893 , hostgroup=1 }
#	{ address="127.0.0.2" , port=3306 , hostgroup=1 },
#	{ address="127.0.0.3" , port=3306 , hostgroup=1 },
#	{ address="127.0.0.4" , port=3306 , hostgroup=1 },
#	{ address="/var/lib/mysql/mysql.sock" , port=0 , hostgroup=1 }{ address="n1" , port=3306 , hostgroup=10 },  { address="n2" , port=3306 , hostgroup=10 },  { address="n3" , port=3306 , hostgroup=10 }
)# defines all the MySQL users
mysql_users:
(
#	{
#		username = "username" # no default , required
#		password = "password" # default: ''
#		default_hostgroup = 0 # default: 0
#		active = 1            # default: 1
#	},
#	{
#		username = "root"
#		password = ""
#		default_hostgroup = 0
#		max_connections=1000
#		default_schema="test"
#		active = 1
#	},
#	{ username = "user1" , password = "password" , default_hostgroup = 0 , active = 0 }{username = "proxysql"password = "proxysql@1025"active = 1default_hostgroup = 10transaction_persistent = 1}
)#defines MySQL Query Rules
mysql_query_rules:
(
#	{
#		rule_id=1
#		active=1
#		match_pattern="^SELECT .* FOR UPDATE$"
#		destination_hostgroup=0
#		apply=1
#	},
#	{
#		rule_id=2
#		active=1
#		match_pattern="^SELECT"
#		destination_hostgroup=1
#		apply=1
#	}{rule_id=1active=1match_digest="^SELECT.*FOR UPDATE$"destination_hostgroup=10apply=1},{rule_id=2active=1match_digest="^SELECT"destination_hostgroup=30apply=1}
)scheduler=
(
#  {
#    id=1
#    active=0
#    interval_ms=10000
#    filename="/var/lib/proxysql/proxysql_galera_checker.sh"
#    arg1="0"
#    arg2="0"
#    arg3="0"
#    arg4="1"
#    arg5="/var/lib/proxysql/proxysql_galera_checker.log"
#  }
)mysql_replication_hostgroups=
(
#        {
#                writer_hostgroup=30
#                reader_hostgroup=40
#                comment="test repl 1"
#       },
#       {
#                writer_hostgroup=50
#                reader_hostgroup=60
#                comment="test repl 2"
#        }
)mysql_group_replication_hostgroups=
({writer_hostgroup=10backup_writer_hostgroup=20reader_hostgroup=30offline_hostgroup=40active=1max_writers=1writer_is_also_reader=0max_transactions_behind=100}
)# http://www.hyperrealm.com/libconfig/libconfig_manual.html#Configuration-File-Grammar
#
# Below is the BNF grammar for configuration files. Comments and include directives are not part of the grammar, so they are not included here. 
#
# configuration = setting-list | empty
#
# setting-list = setting | setting-list setting
#     
# setting = name (":" | "=") value (";" | "," | empty)
#     
# value = scalar-value | array | list | group
#     
# value-list = value | value-list "," value
#     
# scalar-value = boolean | integer | integer64 | hex | hex64 | float
#                | string
#     
# scalar-value-list = scalar-value | scalar-value-list "," scalar-value
#     
# array = "[" (scalar-value-list | empty) "]"
#     
# list = "(" (value-list | empty) ")"
#     
# group = "{" (setting-list | empty) "}"
#     
# empty =

下列介绍命令行方式和配置两种不同方法 ,以及对照关系

#命令方式
# 我的MGR集群的三个节点
insert into mysql_servers(hostgroup_id,hostname,port) values (10,'n1',3306);
insert into mysql_servers(hostgroup_id,hostname,port) values (10,'n2',3306);
insert into mysql_servers(hostgroup_id,hostname,port) values (10,'n3',3306);
# 启用上述配置 + 持久化保存
load mysql servers to runtime;
save mysql servers to disk;#配置文件方式
mysql_servers =
({ address="n1" , port=3306 , hostgroup=10 },  { address="n2" , port=3306 , hostgroup=10 },  { address="n3" , port=3306 , hostgroup=10 }
)#注意:address或者hostname一定要和mgr集群配置的保持一致,否则查询runtime_mysql_servers表会
出现重复节点。mgr集群配置使用域名这里就配置域名,mgr集群使用Ip这里就配置Ip。我猜测proxysql无法通过域名解析将域名与Ip进行匹配。---
# 命令方式
# 设置proxysql监控用户和密码
set mysql-monitor_username='monitor';
set mysql-monitor_password='monitor@1025';
# 设置应用连接mysql用户和密码
insert into mysql_users(username,password,active,default_hostgroup,transaction_persistent) values('proxysql','proxysql@1025',1,10,1);# 配置方式
mysql_variables=
{monitor_username="monitor"monitor_password="monitor@1025"}
mysql_users:
({username = "proxysql"password = "proxysql@1025"active = 1default_hostgroup = 10transaction_persistent = 1}
)---
# 命令方式
# 主负责写、从负责读,当MGR主库切换后,代理自动识别主从。
# ProxySQL代理每一个后端MGR集群时,都必须为这个MGR定义写组10、备写组20、读组30、离线组40,
# 注意:max_transactions_behind 是设置延迟大小,可以给大点,建议自己去开个并行复制。
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,100);# 配置方式
mysql_group_replication_hostgroups=
({writer_hostgroup=10backup_writer_hostgroup=20reader_hostgroup=30offline_hostgroup=40active=1max_writers=1writer_is_also_reader=0max_transactions_behind=100}
)# 通过命令行方式,记得启动规则
load mysql servers to runtime;
save mysql servers to disk;
load mysql users to runtime;
save mysql users to disk;
load mysql variables to runtime;
save mysql variables to disk;
# 读写分离配置
# 命令方式
# select for update走主节点,其他select走从节点
# 其他 insert update delete 走主节点
insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)
VALUES (1,1,'^SELECT.*FOR UPDATE$',10,1),(2,1,'^SELECT',30,1);# 配置方式
mysql_query_rules:
({rule_id=1active=1match_digest="^SELECT.*FOR UPDATE$"destination_hostgroup=10apply=1},{rule_id=2active=1match_digest="^SELECT"destination_hostgroup=30apply=1}
)# 选择命令行方式记得配置加载 + 持久化
load mysql query rules to runtime;
save mysql query rules to disk;

2.3 启动

cd D:\study\backend\proxysql
docker-compose up -d# 连接到Proxy 6032
mysql -u admin -padmin -h 127.0.0.1 -P6032 --prompt='Admin> '# 连接到Proxy 6033
mysql -uproxysql -pproxysql@1025 -h127.0.0.1 -P6033# 查看proxysql配置mgr节点信息
select * from mysql_servers;
# 查看proxysql监控mgr状态
select hostgroup_id, hostname, port,status from runtime_mysql_servers;
# 查看路由日志(6032)
select hostgroup,digest_text from stats_mysql_query_digest order by digest_text desc limit 10;

 三、读写分离测试

 这里注意,进行读写分离实战测试时,要连接ProxySQL 6033端口,6032是Admin,6033才是客户端(Java程序也连6033)


# 链接 ProxySQL 6033
mysql -uproxysql -pproxysql@1025 -h127.0.0.1 -P6033# 创建一个test数据库,查询一条数据,写一条数据
create database test;
use test;
CREATE TABLE test (id INT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(255) NOT NULL
);
select * from test;
INSERT INTO `test`.`test` (`id`, `name`) VALUES ('1', 'test1');
select * from test for update;# 回到6032,查看路由日志
mysql -uadmin -padmin -h127.0.0.1 -P6032
select hostgroup,digest_text from stats_mysql_query_digest order by digest_text desc limit 10;

 四、SpringBoot 整合

 4.1 yml文件

spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://192.168.8.168:6033/quick_chat?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&rewriteBatchedStatements=trueusername: proxysqlpassword: proxysql@1025

4.2 query_cache_size问题

proxysql Caused by: java.sql.SQLException: Unknown system variable 'query_cache_size'
# 命令行方式
# 连接到Proxy 6032
mysql -uadmin -padmin -h127.0.0.1 -P6032# 解决 Unknown system variable 'query_cache_size' 问题
update global_variables set variable_value='8.0.4 (ProxySQL)' where variable_name='mysql-server_version';
load mysql variables to run;save mysql variables to disk;# 配置方式
mysql_variables=
{server_version="8.0.28 (ProxySQL)"
}

参考博客

ProxySQL + MySQL MGR 实现读写分离实战_proxysql mgr-CSDN博客

万字详解 MySQL MGR 高可用集群搭建-CSDN博客

相关文章:

Mysql(MGR)和ProxySQL搭建部署-Docker版本

项目路径&#xff1a;D:\study\backend\mysql\mgr 一、Mysql(MGR) 1.1 docker-compose.yaml volumes: # MySQL配置文件目录 - "./mysql-1/conf.d/my.cnf:/etc/mysql/my.cnf" # MySQL工作目录 - "./mysql-1/data:/var/lib/mysql" …...

QML学习(一) Qt Quick和QML介绍以及适用场景说明

一.介绍 1.Qt Quick 介绍 Qt Quick 提供了一套高动态&#xff0c;丰富的 QML 元素来定制用户界面的说明性框架。Qt Quick 有助于程序开发员与界面设计员的合作为便携式设备建立流畅的用户界面&#xff0c;例如&#xff1a;移动电话、媒体播放器&#xff0c;机顶盒以及上网本等…...

深入理解 PyTorch 的 view() 函数:以多头注意力机制(Multi-Head Attention)为例 (中英双语)

深入理解 PyTorch 的 view() 函数&#xff1a;以多头注意力机制&#xff08;Multi-Head Attention&#xff09;为例 在深度学习模型的实现中&#xff0c;view() 是 PyTorch 中一个非常常用的张量操作函数&#xff0c;它能够改变张量的形状&#xff08;shape&#xff09;而不改…...

使用PHP函数 “setcookie“ 设置cookie

在网站开发中&#xff0c;cookie是一种非常常用的技术&#xff0c;它用于在用户的浏览器中存储少量的数据&#xff0c;以便在不同页面之间传递信息。PHP提供了一个名为 "setcookie" 的函数&#xff0c;用于设置cookie的值和属性。在本文中&#xff0c;我们将学习如何…...

redis优化

在高并发、高性能、高可用系统中&#xff0c;Redis 的优化至关重要。以下是一些在面试中可以详细说明的 Redis 优化策略&#xff0c;以及具体的实践经验和技术亮点&#xff1a; 1. 数据模型与结构设计优化 使用合适的数据结构 &#xff1a;根据业务需求选择合适的 Redis 数据结…...

数据分析的革命——解读云数据库 SelectDB 版的力量

在当今数据驱动的时代&#xff0c;实时数据分析已成为企业决策中的关键一环。如何在海量数据中快速找到核心价值&#xff0c;如何让决策者在毫秒间洞悉变化&#xff0c;这不仅考验着企业的技术能力&#xff0c;也对基础设施提出了新的要求。云数据库 SelectDB 版&#xff0c;正…...

Ngnix介绍、安装、实战及用法!!!

一、Nginx简介 1、Nginx概述 Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理服务器&#xff0c;特点是占有内存少&#xff0c;并发能力强&#xff0c;能经受高负载的考验,有报告表明能支持高达 50,000 个并发连接数 。 2、正向代理 正向代理&#xff1a;如果把局…...

算法基础一:冒泡排序

一、冒泡排序 1、定义 冒泡排序&#xff08;英语&#xff1a;Bubble Sort&#xff09;是一种简单的排序算法。它重复地走访过要排序的数列&#xff0c;一次比较两个元素&#xff0c;如果他们的顺序&#xff08;如从大到小、首字母从A到Z&#xff09;错误就把他们交换过来。 …...

云开发实战教程:手把手教你高效开发应用

声明&#xff1a;本文仅供实践教学使用&#xff0c;没有任何打广告成分 目录 1.引言 2.云开发 Copilot介绍 云开发 Copilot 的功能与特点 3.环境准备 步骤一登录账号 步骤二新建环境 4.开发实践 4.1AI 生成低代码应用 4.2AI 生成低代码页面/区块 4.3AI 优化低代码组件…...

Git基本操作快速入门(30min)

Git基本操作快速入门&#xff08;30min&#xff09; 文章目录 Git基本操作快速入门&#xff08;30min&#xff09;1. 建立本地仓库2. 本地仓库链接到远端仓库3. 将本地仓库推送到远端4. Git常用命令 作为一名程序员&#xff0c;使用Github来进行代码的版本管理是必修课&#xf…...

VS Code AI开发之Copilot配置和使用详解

随着AI开发工具的迅速发展&#xff0c;GitHub Copilot在Cursor、Winsuf、V0等一众工具的冲击下&#xff0c;推出了免费版本。接下来&#xff0c;我将为大家介绍GitHub Copilot的配置和使用方法。GitHub Copilot基于OpenAI Codex模型&#xff0c;旨在为软件开发者提供智能化的代…...

QT中使用OpenGL function

1.前言 QT做界面编程很方便&#xff0c;QTOpenGL的使用也很方便&#xff0c;因为QT对原生的OpenGL API进行了面向对象化的封装。 如&#xff1a; 函数&#xff1a;initializeOpenGLFunctions()...... 类&#xff1a;QOpenGLVertexArrayObject、QOpenGLBuffer、QOpenGLShader…...

STM32-笔记16-定时器中断点灯

一、实验目的 使用定时器 2 进行中断点灯&#xff0c;500ms LED 灯翻转一次。 二&#xff0c;定时器溢出时间计算 Tout&#xff1a;定时器溢出时间 Ft&#xff1a;定时器的时钟源频率 ARR&#xff1a;自动重装载寄存器的值&#xff08;可设置ARR从0开始&#xff0c;但是计数到…...

Live555、FFmpeg、GStreamer介绍

Live555、FFmpeg 和 GStreamer 都是处理流媒体和视频数据的强大开源框架和工具&#xff0c;它们广泛应用于实时视频流的推送、接收、处理和播放。每个框架有不同的设计理念、功能特性以及适用场景。下面将详细分析这三个框架的作用、解决的问题、适用场景、优缺点&#xff0c;并…...

oracle基础:理解 Oracle SQL 中的 WHERE 后的 (+) 用法

在使用 Oracle 数据库进行 SQL 查询时&#xff0c;可能会遇到 WHERE 子句后带有 () 的语法。这是 Oracle 专有的外连接&#xff08;Outer Join&#xff09;表示法。虽然现代 SQL 标准推荐使用 LEFT JOIN 和 RIGHT JOIN 语法&#xff0c;但在某些遗留系统中&#xff0c;这种写法…...

【linux】进程间通信(IPC)——匿名管道,命名管道与System V内核方案的共享内存,以及消息队列和信号量的原理概述

目录 ✈必备知识 进程间通信概述 &#x1f525;概述 &#x1f525;必要性 &#x1f525;原理 管道概述 &#x1f525;管道的本质 &#x1f525;管道的相关特性 &#x1f525;管道的同步与互斥机制 匿名管道 &#x1f525;系统调用接口介绍 &#x1f525;内核原理 …...

【深度学习】卷积网络代码实战ResNet

ResNet (Residual Network) 是由微软研究院的何凯明等人在2015年提出的一种深度卷积神经网络结构。ResNet的设计目标是解决深层网络训练中的梯度消失和梯度爆炸问题&#xff0c;进一步提高网络的表现。下面是一个ResNet模型实现&#xff0c;使用PyTorch框架来展示如何实现基本的…...

org.apache.zookeeper.server.quorum.QuorumPeerMain

QuorumPeerMain源代码 package org.apache.zookeeper.server.quorum;import java.io.IOException; import javax.management.JMException; import javax.security.sasl.SaslException; import org.apache.yetus.audience.InterfaceAudience; import org.apache.zookeeper.audi…...

oscp学习之路,Kioptix Level2靶场通关教程

oscp学习之路&#xff0c;Kioptix Level2靶场通关教程 靶场下载&#xff1a;Kioptrix Level 2.zip 链接: https://pan.baidu.com/s/1gxVRhrzLW1oI_MhcfWPn0w?pwd1111 提取码: 1111 搭建好靶场之后输入ip a看一下攻击机的IP。 确定好本机IP后&#xff0c;使用nmap扫描网段&…...

SkyWalking java-agent 是如何工作的,自己实现一个监控sql执行耗时的agent

Apache SkyWalking 是一个开源的应用性能监控 (APM) 工具&#xff0c;支持分布式系统的追踪、监控和诊断。SkyWalking Agent 是其中的一个重要组件&#xff0c;用于在服务端应用中收集性能数据和追踪信息&#xff0c;并将其发送到 SkyWalking 后端服务器进行处理和展示。 SkyW…...

【力扣数据库知识手册笔记】索引

索引 索引的优缺点 优点1. 通过创建唯一性索引&#xff0c;可以保证数据库表中每一行数据的唯一性。2. 可以加快数据的检索速度&#xff08;创建索引的主要原因&#xff09;。3. 可以加速表和表之间的连接&#xff0c;实现数据的参考完整性。4. 可以在查询过程中&#xff0c;…...

2024年赣州旅游投资集团社会招聘笔试真

2024年赣州旅游投资集团社会招聘笔试真 题 ( 满 分 1 0 0 分 时 间 1 2 0 分 钟 ) 一、单选题(每题只有一个正确答案,答错、不答或多答均不得分) 1.纪要的特点不包括()。 A.概括重点 B.指导传达 C. 客观纪实 D.有言必录 【答案】: D 2.1864年,()预言了电磁波的存在,并指出…...

生成 Git SSH 证书

&#x1f511; 1. ​​生成 SSH 密钥对​​ 在终端&#xff08;Windows 使用 Git Bash&#xff0c;Mac/Linux 使用 Terminal&#xff09;执行命令&#xff1a; ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" ​​参数说明​​&#xff1a; -t rsa&#x…...

Java-41 深入浅出 Spring - 声明式事务的支持 事务配置 XML模式 XML+注解模式

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; &#x1f680; AI篇持续更新中&#xff01;&#xff08;长期更新&#xff09; 目前2025年06月05日更新到&#xff1a; AI炼丹日志-28 - Aud…...

纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join

纯 Java 项目&#xff08;非 SpringBoot&#xff09;集成 Mybatis-Plus 和 Mybatis-Plus-Join 1、依赖1.1、依赖版本1.2、pom.xml 2、代码2.1、SqlSession 构造器2.2、MybatisPlus代码生成器2.3、获取 config.yml 配置2.3.1、config.yml2.3.2、项目配置类 2.4、ftl 模板2.4.1、…...

【Linux】Linux安装并配置RabbitMQ

目录 1. 安装 Erlang 2. 安装 RabbitMQ 2.1.添加 RabbitMQ 仓库 2.2.安装 RabbitMQ 3.配置 3.1.启动和管理服务 4. 访问管理界面 5.安装问题 6.修改密码 7.修改端口 7.1.找到文件 7.2.修改文件 1. 安装 Erlang 由于 RabbitMQ 是用 Erlang 编写的&#xff0c;需要先安…...

用 Rust 重写 Linux 内核模块实战:迈向安全内核的新篇章

用 Rust 重写 Linux 内核模块实战&#xff1a;迈向安全内核的新篇章 ​​摘要&#xff1a;​​ 操作系统内核的安全性、稳定性至关重要。传统 Linux 内核模块开发长期依赖于 C 语言&#xff0c;受限于 C 语言本身的内存安全和并发安全问题&#xff0c;开发复杂模块极易引入难以…...

数据分析六部曲?

引言 上一章我们说到了数据分析六部曲&#xff0c;何谓六部曲呢&#xff1f; 其实啊&#xff0c;数据分析没那么难&#xff0c;只要掌握了下面这六个步骤&#xff0c;也就是数据分析六部曲&#xff0c;就算你是个啥都不懂的小白&#xff0c;也能慢慢上手做数据分析啦。 第一…...

自定义线程池1.2

自定义线程池 1.2 1. 简介 上次我们实现了 1.1 版本&#xff0c;将线程池中的线程数量交给使用者决定&#xff0c;并且将线程的创建延迟到任务提交的时候&#xff0c;在本文中我们将对这个版本进行如下的优化&#xff1a; 在新建线程时交给线程一个任务。让线程在某种情况下…...

生成对抗网络(GAN)损失函数解读

GAN损失函数的形式&#xff1a; 以下是对每个部分的解读&#xff1a; 1. ⁡, ​ &#xff1a;这个部分表示生成器&#xff08;Generator&#xff09;G的目标是最小化损失函数。 &#xff1a;判别器&#xff08;Discriminator&#xff09;D的目标是最大化损失函数。 GAN的训…...