重要参考步骤---ProxySQL Cluster 集群搭建步骤
环境
proxysql-1:192.168.20.202
proxysql-2:192.168.20.203
均采用yum方式安装
# cat <<EOF | tee /etc/yum.repos.d/proxysql.repo
[proxysql_repo]
name= ProxySQL YUM repository
baseurl=https://repo.proxysql.com/ProxySQL/proxysql-2.4.x/centos/\$releasever
gpgcheck=0
gpgkey=https://repo.proxysql.com/ProxySQL/repo_pub_key
EOF
# yum -y install proxysql
# systemctl start proxysql.service
# systemctl stop proxysql.service
# systemctl enable proxysql.service
# 查询端口占用情况,端口6032用于查看、配置ProxySQL;端口为6033,用于接收SQL语句,这个接口类似于MySQL的3306端口
# netstat -tulnp|grep "proxysql"
tcp 0 0 0.0.0.0:6032 0.0.0.0:* LISTEN 2115/proxysql
tcp 0 0 0.0.0.0:6033 0.0.0.0:* LISTEN 2115/proxysql
# 安装MySQL 客户端
wget https://repo.mysql.com//mysql80-community-release-el7-5.noarch.rpm
yum localinstall -y mysql80-community-release-el7-5.noarch.rpm
yum -y install mysql-community-client # 8.0.29版本
rpm包方式安装,需要先安装mysql-community-libs-compat,否则会报如下错误:yum -y install mysql-community-libs-compat
# rpm -ivh perl-DBD-MySQL-4.023-6.el7.x86_64.rpm
错误:依赖检测失败:
libmysqlclient.so.18()(64bit) 被 perl-DBD-MySQL-4.023-6.el7.x86_64 需要
libmysqlclient.so.18(libmysqlclient_18)(64bit) 被 perl-DBD-MySQL-4.023-6.el7.x86_64 需要
# 批量安装命令:ls *.rpm | xargs rpm -ivh
# 分步安装命令
rpm -ivh trousers-0.3.14-2.el7.x86_64.rpm
rpm -ivh nettle-2.7.1-9.el7_9.x86_64.rpm
rpm -ivh perl-Net-Daemon-0.48-5.el7.noarch.rpm
rpm -ivh perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm
rpm -ivh perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm
rpm -ivh perl-IO-Compress-2.061-2.el7.noarch.rpm
rpm -ivh perl-Data-Dumper-2.145-3.el7.x86_64.rpm
rpm -ivh perl-PlRPC-0.2020-14.el7.noarch.rpm
rpm -ivh gnutls-3.3.29-9.el7_6.x86_64.rpm
rpm -ivh perl-DBI-1.627-4.el7.x86_64.rpm
rpm -ivh perl-DBD-MySQL-4.023-6.el7.x86_64.rpm
rpm -ivh proxysql-2.4.1-1-centos7.x86_64.rpm
集群的搭建有很多种方式,如1+1+1的方式,还可以(1+1)+1的方式。
这里采用较简单的(1+1)+1,即先将两个节点作为集群启动,后续若有其他节点选择性加入的方式
数据库方面的操作
数据库环境
master mysql: 192.168.20.200
slave mysql: 192.168.20.201
只配置同步test库,自带的mysql库没有同步,因此主库添加账号密码,从库也得添加一遍
# 添加一个监控用账号(能监控到从库的复制情况) ,在这里要么主从库自带的mysql库需要同步,若是没同步则每个MySQL都需要做这一步操作
create user 'proxysql'@'192.168.20.%' identified with mysql_native_password by 'iD!^^EjU#Yxr5$p';
GRANT USAGE,process,replication slave,replication client ON *.* TO 'proxysql'@'192.168.20.%' with grant option;
flush privileges;
# 注意:这里的账号密码要和下面我们在proxysql里面的mysql_variables段的账号密码配置的一样
# 添加一个程序连接用的账号
create user 'sbuser'@'192.168.20.%' identified with mysql_native_password by 'iD!^^EjU#Yxr5$p';
GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'sbuser'@'192.168.20.%' with grant option;
flush privileges;
此时MySQL主从库中有如下账号信息
超级用户:root
主从库同步用户:repl (从库没这个用户)
普通用户:sbuser
proxysql监控MySQL主从情况使用的用户:proxysql
集群搭建
1.更改所有实例的配置文件
# cp /etc/proxysql.cnf /etc/proxysql.cnf.bak
# vim /etc/proxysql.cnf
admin_variables=
{
admin_credentials="admin:admin;cluster_20X:123456"
mysql_ifaces="0.0.0.0:6032"
cluster_username="cluster_20X"
cluster_password="123456"
cluster_check_interval_ms=200
cluster_check_status_frequency=100
cluster_mysql_query_rules_save_to_disk=true
cluster_mysql_servers_save_to_disk=true
cluster_mysql_users_save_to_disk=true
cluster_proxysql_servers_save_to_disk=true
cluster_mysql_query_rules_diffs_before_sync=3
cluster_mysql_servers_diffs_before_sync=3
cluster_mysql_users_diffs_before_sync=3
cluster_proxysql_servers_diffs_before_sync=3
}
mysql_variables=
{
threads=4
max_connections=10000
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="8.0.29"
connect_timeout_server=3000
monitor_username="proxysql"
monitor_password="iD!^^EjU#Yxr5$p"
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
#default_charset='utf8mb4' # 这个参数加上无法启动,只能先不加,等程序启动后再手动修改
#default_collation_connection='utf8mb4_general_ci' # 这个参数加上无法启动,只能先不加,等程序启动后再手动修改
max_allowed_packet=67108864
query_retries_on_failure=0
monitor_connect_timeout=1000
default_max_latency_ms=2000
monitor_replication_lag_interval=500
connect_timeout_server_max=3000
set_query_lock_on_hostgroup=0
}
proxysql_servers =
(
{
hostname="192.168.20.202"
port=6032
comment="proxysql-202"
},
{
hostname="192.168.20.203"
port=6032
comment="proxysql-203"
}
)
# 其余的配置信息保持不动
特别注意:如果存在"proxysql.db"文件(在/var/lib/proxysql目录下),则ProxySQL服务只有在第一次启动时才会去读取proxysql.cnf文件并解析;后面启动会就不会读取proxysql.cnf文件了!如果想要让proxysql.cnf文件里的配置在重启proxysql服务后生效(即想要让proxysql重启时读取并解析proxysql.cnf配置文件),则需要先删除/var/lib/proxysql/proxysql.db数据库文件,然后再重启proxysql服务。这样就相当于初始化启动proxysql服务了,会再次生产一个纯净的proxysql.db数据库文件(如果之前配置了proxysql相关路由规则等,则就会被抹掉)。
# 俩proxysql节点启动proxysql进程
systemctl start proxysql
# 登录进去(无特殊说明均在192.168.20.202上操作)
/usr/bin/mysql -uadmin -padmin -h 127.0.0.1 -P 6032
# 观察集群状况
mysql> select * from proxysql_servers;
+----------------+------+--------+--------------+
| hostname | port | weight | comment |
+----------------+------+--------+--------------+
| 192.168.20.202 | 6032 | 0 | proxysql-202 |
| 192.168.20.203 | 6032 | 0 | proxysql-203 |
+----------------+------+--------+--------------+
2 rows in set (0.00 sec)
mysql> select * from stats_proxysql_servers_metrics;
+----------------+------+--------+--------------+------------------+----------+---------------+---------+------------------------------+----------------------------+
| hostname | port | weight | comment | response_time_ms | Uptime_s | last_check_ms | Queries | Client_Connections_connected | Client_Connections_created |
+----------------+------+--------+--------------+------------------+----------+---------------+---------+------------------------------+----------------------------+
| 192.168.20.203 | 6032 | 0 | proxysql-203 | 2 | 683 | 2593 | 0 | 0 | 0 |
| 192.168.20.202 | 6032 | 0 | proxysql-202 | 2 | 686 | 2923 | 0 | 0 | 0 |
+----------------+------+--------+--------------+------------------+----------+---------------+---------+------------------------------+----------------------------+
2 rows in set (0.00 sec)
mysql> select hostname,port,comment,Uptime_s,last_check_ms from stats_proxysql_servers_metrics;
+----------------+------+--------------+----------+---------------+
| hostname | port | comment | Uptime_s | last_check_ms |
+----------------+------+--------------+----------+---------------+
| 192.168.20.203 | 6032 | proxysql-203 | 1266 | 15622 |
| 192.168.20.202 | 6032 | proxysql-202 | 1266 | 18806 |
+----------------+------+--------------+----------+---------------+
2 rows in set (0.00 sec)
mysql> select hostname,name,checksum,updated_at from stats_proxysql_servers_checksums;
+----------------+-------------------+--------------------+------------+
| hostname | name | checksum | updated_at |
+----------------+-------------------+--------------------+------------+
| 192.168.20.203 | admin_variables | 0xF8E25295F13135A0 | 1653448141 |
| 192.168.20.203 | mysql_query_rules | 0x0000000000000000 | 1653448141 |
| 192.168.20.203 | mysql_servers | 0xE5A163C3AD6BD3A7 | 1653448141 |
| 192.168.20.203 | mysql_users | 0x0000000000000000 | 1653448141 |
| 192.168.20.203 | mysql_variables | 0x3ECA231EE02626C9 | 1653448141 |
| 192.168.20.203 | proxysql_servers | 0x75C8DA71CAF992E0 | 1653448141 |
| 192.168.20.202 | admin_variables | 0xF8E25295F13135A0 | 1653448141 |
| 192.168.20.202 | mysql_query_rules | 0x0000000000000000 | 1653448141 |
| 192.168.20.202 | mysql_servers | 0xE5A163C3AD6BD3A7 | 1653448141 |
| 192.168.20.202 | mysql_users | 0x0000000000000000 | 1653448141 |
| 192.168.20.202 | mysql_variables | 0x3ECA231EE02626C9 | 1653448141 |
| 192.168.20.202 | proxysql_servers | 0x75C8DA71CAF992E0 | 1653448141 |
+----------------+-------------------+--------------------+------------+
12 rows in set (0.00 sec)
# 观察ProxySQL集群中实例之间的数据同步
# 原有数据
mysql> select * from mysql_servers;
Empty set (0.00 sec)
# 新增一个后端MySQL主机信息
mysql> insert into mysql_servers(hostgroup_id,hostname,port,comment) values (10,'192.168.20.200',3306,'master_mysql');
# 该主机上查看
mysql> select * from mysql_servers;
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| 10 | 192.168.20.200 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | master_mysql |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
1 row in set (0.00 sec)
# 此时到另一台proxysql主机上查看没有这个后端MySQL主机信息
# 持久化,并加载到运行环境中
mysql> save mysql servers to disk;
mysql> load mysql servers to runtime;
# 再次到另一台proxysql主机上查看,可以看到新插入的数据,发现有这个后端MySQL主机信息,已经被更新到192.168.20.203实例中的memory和runtime环境中。
mysql> select * from mysql_servers;
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| 10 | 192.168.20.200 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | master_mysql |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
1 row in set (0.00 sec)
mysql> select * from runtime_mysql_servers;
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| 10 | 192.168.20.200 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | master_mysql |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
1 row in set (0.00 sec)
# 查看另一台proxysql主机日志,会看到同步的具体信息
# tail -n 30 /var/lib/proxysql/proxysql.log
2022-05-25 11:03:27 [INFO] Cluster: Loading to runtime MySQL Servers from peer 192.168.20.202:6032
2022-05-25 11:03:27 [INFO] Dumping mysql_servers_incoming
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| hostgroup_id | hostname | port | gtid_port | weight | status | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| 10 | 192.168.20.200 | 3306 | 0 | 1 | 0 | 0 | 1000 | 0 | 0 | 0 | master_mysql |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
2022-05-25 11:03:27 [INFO] Dumping mysql_servers LEFT JOIN mysql_servers_incoming
+-------------+--------------+----------+------+
| mem_pointer | hostgroup_id | hostname | port |
+-------------+--------------+----------+------+
+-------------+--------------+----------+------+
2022-05-25 11:03:27 [INFO] Dumping mysql_servers JOIN mysql_servers_incoming
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+-------------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| hostgroup_id | hostname | port | gtid_port | weight | status | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment | mem_pointer | gtid_port | weight | status | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+-------------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
| 10 | 192.168.20.200 | 3306 | 0 | 1 | 0 | 0 | 1000 | 0 | 0 | 0 | master_mysql | 0 | 0 | 1 | 0 | 0 | 1000 | 0 | 0 | 0 | master_mysql |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+-------------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+--------------+
2022-05-25 11:03:27 [INFO] Creating new server in HG 10 : 192.168.20.200:3306 , gtid_port=0, weight=1, status=0
2022-05-25 11:03:27 [INFO] New mysql_group_replication_hostgroups table
2022-05-25 11:03:27 [INFO] New mysql_galera_hostgroups table
2022-05-25 11:03:27 [INFO] New mysql_aws_aurora_hostgroups table
2022-05-25 11:03:27 [INFO] Checksum for table mysql_servers is 6785001030445135624
2022-05-25 11:03:27 [INFO] MySQL_HostGroups_Manager::commit() locked for 1ms
2022-05-25 11:03:27 [INFO] Cluster: Saving to disk MySQL Servers from peer 192.168.20.202:6032
2022-05-25 11:03:27 [INFO] Cluster: checksum for admin_variables from peer 192.168.20.203:6032 matches with local checksum 0xF8E25295F13135A0, we won't sync.
2022-05-25 11:03:27 [INFO] Cluster: detected a new checksum for mysql_servers from peer 192.168.20.203:6032, version 2, epoch 1653447806, checksum 0xE5A163C3AD6BD3A7 . Not syncing yet ...
2022-05-25 11:03:27 [INFO] Cluster: checksum for mysql_servers from peer 192.168.20.203:6032 matches with local checksum 0xE5A163C3AD6BD3A7 , we won't sync.
注意:数据差异检查是根据runtime进行检查的,只对memory和disk进行更改,并不触发同步操作。
此时,两节点的proxysql cluster集群搭建完毕
后续若需要加入第三个proxysql节点到集群中的操作
这里以安装有MySQL master主机节点为例进行演示(也可以使用新主机)
192.168.20.200为全新的节点,使用conf文件启动,不使用更改global_variable的方式加入集群(操作复杂且容器出错)。
# 先安装好proxysql软件,不启动(mysql客户端已安装,此时不用再安装,若未安装也需要安装这个)
# 修改 /etc/proxysql.cnf (修改了admin_variables段、proxysql_servers段、mysql_variables段) 【要和proxysql cluster里面的其他节点运行配置一样,集群名称、各种账号密码要一致】
# cp /etc/proxysql.cnf /etc/proxysql.cnf.bak
# vim /etc/proxysql.cnf
admin_variables=
{
admin_credentials="admin:admin;cluster_20X:123456"
mysql_ifaces="0.0.0.0:6032"
cluster_username="cluster_20X"
cluster_password="123456"
cluster_check_interval_ms=200
cluster_check_status_frequency=100
cluster_mysql_query_rules_save_to_disk=true
cluster_mysql_servers_save_to_disk=true
cluster_mysql_users_save_to_disk=true
cluster_proxysql_servers_save_to_disk=true
cluster_mysql_query_rules_diffs_before_sync=3
cluster_mysql_servers_diffs_before_sync=3
cluster_mysql_users_diffs_before_sync=3
cluster_proxysql_servers_diffs_before_sync=3
}
mysql_variables=
{
threads=4
max_connections=10000
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="8.0.29"
connect_timeout_server=3000
monitor_username="proxysql"
monitor_password="iD!^^EjU#Yxr5$p"
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
#default_charset='utf8mb4' # 这个参数加上无法启动,只能先不加,等程序启动后再手动修改
#default_collation_connection='utf8mb4_general_ci' # 这个参数加上无法启动,只能先不加,等程序启动后再手动修改
max_allowed_packet=67108864
query_retries_on_failure=0
monitor_connect_timeout=1000
default_max_latency_ms=2000
monitor_replication_lag_interval=500
connect_timeout_server_max=3000
set_query_lock_on_hostgroup=0
}
proxysql_servers =
(
{
hostname="192.168.20.202"
port=6032
comment="proxysql-202"
},
{
hostname="192.168.20.203"
port=6032
comment="proxysql-203"
}
)
# 其余的配置信息保持不动
# 在新节点里启动proxysql后, 可以看下 192.168.20.200 的 /var/lib/proxysql/proxysql.log 日志里面, 192.168.20.200 这个新加入的节点 会去其它节点拉取配置(但是其它节点不知道这个192.168.20.200到底是什么身份的存在)。
# 然后,我们在老的proxysql的任一节点上,将 192.168.20.200 这个新节点加入到集群环境:
# 插入一条proxysql_server的信息
insert into proxysql_servers(hostname,port,comment ) values('192.168.20.200',6032,'bak proxysql') ;
# 加载到runtime,并把配置持久化
load proxysql servers to runtime;
save proxysql servers to disk;
# 查下结果是否正常
mysql> select * from proxysql_servers;
+----------------+------+--------+--------------+
| hostname | port | weight | comment |
+----------------+------+--------+--------------+
| 192.168.20.202 | 6032 | 0 | proxysql-202 |
| 192.168.20.203 | 6032 | 0 | proxysql-203 |
| 192.168.20.200 | 6032 | 0 | bak proxysql |
+----------------+------+--------+--------------+
3 rows in set (0.00 sec)
mysql> select * from runtime_proxysql_servers ;
+----------------+------+--------+--------------+
| hostname | port | weight | comment |
+----------------+------+--------+--------------+
| 192.168.20.200 | 6032 | 0 | bak proxysql |
| 192.168.20.203 | 6032 | 0 | proxysql-203 |
| 192.168.20.202 | 6032 | 0 | proxysql-202 |
+----------------+------+--------+--------------+
3 rows in set (0.00 sec)
经过上面的步骤后, 192.168.20.200 就完成集群添加新节点的操作了。
添加下后端mysql信息, 添加读写分离的路由规则
在3节点中的任意一台proyxql的admin控制台执行下面的这些操作(这个新增的配置会在load runtime时候,自动同步到集群其它节点):
mysql> delete from mysql_servers;
# 写节点组 100, 读节点组 1000
# 写组,master mysql节点,权重1
mysql> insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(100,'192.168.20.200',3306,1,1000,10,'100-1-master_mysql');
# 读组,salve mysql节点,权重9
mysql> insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(1000,'192.168.20.201',3306,9,1000,10,'1000-9-slave_mysql');
# 读组,master mysql节点,权重1 (当读组,salve mysql节点,权重9发生故障时,由该节点承担读任务,也就是说当读不到从库,回去主库查询)
mysql> insert into mysql_servers(hostgroup_id,hostname,port,weight,max_connections,max_replication_lag,comment) values(1000,'192.168.20.200',3306,1,1000,10,'1000-1-master_mysql');
# 加载到runtime,并把配置持久化
mysql> load mysql servers to runtime;
mysql> save mysql servers to disk;
mysql> select * from mysql_servers;
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------------------+
| hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------------------+
| 100 | 192.168.20.200 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | 100-1-master_mysql |
| 1000 | 192.168.20.200 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | 1000-1-master_mysql |
| 1000 | 192.168.20.201 | 3306 | 0 | ONLINE | 9 | 0 | 1000 | 10 | 0 | 0 | 1000-9-slave_mysql |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------------------+
3 rows in set (0.00 sec)
mysql> select * from runtime_mysql_servers;
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------------------+
| hostgroup_id | hostname | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------------------+
| 100 | 192.168.20.200 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | 100-1-master_mysql |
| 1000 | 192.168.20.200 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 10 | 0 | 0 | 1000-1-master_mysql |
| 1000 | 192.168.20.201 | 3306 | 0 | ONLINE | 9 | 0 | 1000 | 10 | 0 | 0 | 1000-9-slave_mysql |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------------------+
3 rows in set (0.01 sec)
# 添加一个账号,用于proxysql和后端主机的连接,在这里使用开头MySQL数据库中已经添加的sbuser用户信息
mysql> insert into mysql_users(username,password,active,default_hostgroup,transaction_persistent) values('sbuser','iD!^^EjU#Yxr5$p',1,100,1);
mysql> load mysql users to runtime;
mysql> save mysql users to disk;
# 发现一个情况
# 在添加用户的主机上查看信息如下,密码没加密且只有一个账号
mysql> 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 |
+----------+-----------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+------------+---------+
| sbuser | iD!^^EjU#Yxr5$p | 1 | 0 | 100 | NULL | 0 | 1 | 0 | 1 | 1 | 10000 | | |
+----------+-----------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+------------+---------+
1 row in set (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 | attributes | comment |
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+------------+---------+
| sbuser | *1A1EDDE95A2B74BFDCACFBCC83C7E0600C7D19A1 | 1 | 0 | 100 | | 0 | 1 | 0 | 0 | 1 | 10000 | | |
| sbuser | *1A1EDDE95A2B74BFDCACFBCC83C7E0600C7D19A1 | 1 | 0 | 100 | | 0 | 1 | 0 | 1 | 0 | 10000 | | |
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+------------+---------+
2 rows in set (0.00 sec)
mysql> select * from mysql_users\G
*************************** 1. row ***************************
username: sbuser
password: iD!^^EjU#Yxr5$p
active: 1
use_ssl: 0
default_hostgroup: 100
default_schema: NULL
schema_locked: 0
transaction_persistent: 1
fast_forward: 0
backend: 1
frontend: 1
max_connections: 10000
attributes:
comment:
1 row in set (0.00 sec)
mysql> select * from mysql_users\G
*************************** 1. row ***************************
username: sbuser
password: *1A1EDDE95A2B74BFDCACFBCC83C7E0600C7D19A1
active: 1
use_ssl: 0
default_hostgroup: 100
default_schema:
schema_locked: 0
transaction_persistent: 1
fast_forward: 0
backend: 0
frontend: 1
max_connections: 10000
attributes:
comment:
*************************** 2. row ***************************
username: sbuser
password: *1A1EDDE95A2B74BFDCACFBCC83C7E0600C7D19A1
active: 1
use_ssl: 0
default_hostgroup: 100
default_schema:
schema_locked: 0
transaction_persistent: 1
fast_forward: 0
backend: 1
frontend: 0
max_connections: 10000
attributes:
comment:
2 rows in set (0.00 sec)
# 定义读写分离的路由规则:
# 定义sql规则,发送到主库
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.00 sec)
# 定义sql规则,发送到从库
mysql> INSERT INTO mysql_query_rules(active,match_pattern,destination_hostgroup,apply) VALUES(1,'^SELECT',1000,1);
Query OK, 1 row affected (0.00 sec)
# 加载路由规则到runtime,并把配置持久化
mysql> load mysql query rules to runtime;
Query OK, 0 rows affected (0.01 sec)
mysql> save mysql query rules to disk;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from mysql_query_rules;
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+--------------+----------------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+------------+---------+
| rule_id | active | username | schemaname | flagIN | client_addr | proxy_addr | proxy_port | digest | match_digest | match_pattern | negate_match_pattern | re_modifiers | flagOUT | replace_pattern | destination_hostgroup | cache_ttl | cache_empty_result | cache_timeout | reconnect | timeout | retries | delay | next_query_flagIN | mirror_flagOUT | mirror_hostgroup | error_msg | OK_msg | sticky_conn | multiplex | gtid_from_hostgroup | log | apply | attributes | comment |
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+--------------+----------------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+------------+---------+
| 1 | 1 | NULL | NULL | 0 | NULL | NULL | NULL | NULL | NULL | ^SELECT.*FOR UPDATE$ | 0 | CASELESS | NULL | NULL | 100 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1 | | NULL |
| 2 | 1 | NULL | NULL | 0 | NULL | NULL | NULL | NULL | NULL | ^SELECT | 0 | CASELESS | NULL | NULL | 1000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1 | | NULL |
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+--------------+----------------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+------------+---------+
2 rows in set (0.00 sec)
mysql> select * from runtime_mysql_query_rules;
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+--------------+----------------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+------------+---------+
| rule_id | active | username | schemaname | flagIN | client_addr | proxy_addr | proxy_port | digest | match_digest | match_pattern | negate_match_pattern | re_modifiers | flagOUT | replace_pattern | destination_hostgroup | cache_ttl | cache_empty_result | cache_timeout | reconnect | timeout | retries | delay | next_query_flagIN | mirror_flagOUT | mirror_hostgroup | error_msg | OK_msg | sticky_conn | multiplex | gtid_from_hostgroup | log | apply | attributes | comment |
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+--------------+----------------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+------------+---------+
| 1 | 1 | NULL | NULL | 0 | NULL | NULL | NULL | NULL | NULL | ^SELECT.*FOR UPDATE$ | 0 | CASELESS | NULL | NULL | 100 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1 | | NULL |
| 2 | 1 | NULL | NULL | 0 | NULL | NULL | NULL | NULL | NULL | ^SELECT | 0 | CASELESS | NULL | NULL | 1000 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1 | | NULL |
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+--------------+----------------------+----------------------+--------------+---------+-----------------+-----------------------+-----------+--------------------+---------------+-----------+---------+---------+-------+-------------------+----------------+------------------+-----------+--------+-------------+-----------+---------------------+-----+-------+------------+---------+
2 rows in set (0.00 sec)
测试
然后,连接proxysql做crud的测试(可以测试下读写分离情况,不是本文的重点)
端口6032用于查看、配置ProxySQL;端口为6033,用于接收SQL语句,这个接口类似于MySQL的3306端口
mysql -usbuser -h 172.100.2.13 -P6033 -p # 密码是:iD!^^EjU#Yxr5$p
scheduler打印状态到日志
编辑脚本和目录
# mkdir -p /opt/proxysql/log
# vim /opt/proxysql/log/status.sh
#!/bin/bash
DATE=`date "+%Y-%m-%d %H:%M:%S"`
echo "{\"dateTime\":\"$DATE\",\"status\":\"running\"}" >> /opt/proxysql/log/status_log
# chmod 777 /opt/proxysql/log/status.sh
输入scheduler信息
mysql> insert into scheduler(active,interval_ms,filename) values (1,60000,'/opt/proxysql/log/status.sh');
Query OK, 1 row affected (0.00 sec)
mysql> LOAD SCHEDULER TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)
mysql> SAVE SCHEDULER TO DISK;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from global_variables where variable_name LIKE 'admin-web%' or variable_name LIKE 'admin-stats%';
+----------------------------------------+----------------+
| variable_name | variable_value |
+----------------------------------------+----------------+
| admin-stats_credentials | stats:stats |
| admin-stats_mysql_connections | 60 |
| admin-stats_mysql_connection_pool | 60 |
| admin-stats_mysql_query_cache | 60 |
| admin-stats_mysql_query_digest_to_disk | 0 |
| admin-stats_system_cpu | 60 |
| admin-stats_system_memory | 60 |
| admin-web_enabled | true |
| admin-web_port | 6080 |
+----------------------------------------+----------------+
9 rows in set (0.01 sec)
查看日志:
# tail -f status_log
{"dateTime":"2020-04-05 00:07:40","status":"running"}
{"dateTime":"2020-04-05 00:08:41","status":"running"}
{"dateTime":"2020-04-05 00:10:52","status":"running"}
优化
proxysql检查主从同步延迟情况,若是符合条件则屏蔽掉从库
Monitor模块会每隔一段时间(mysql-monitor_replication_lag_interval)去检查一次拖后腿情况,检测的方式是获取show slave status中的Seconds_Behind_Master字段值,然后和mysql_servers表中max_replication_lag字段的值比较:
mysql> update mysql_servers set max_replication_lag=10 where hostgroup_id=1000 and hostname="192.168.0.36"; # 确保只有从库设置
mysql> load mysql servers to runtime;
Query OK, 0 rows affected (0.00 sec)
mysql> save mysql servers to disk;
Query OK, 0 rows affected (0.01 sec)
mysql> select hostgroup_id,hostname,port,max_replication_lag from mysql_servers;
+--------------+---------------+------+---------------------+
| hostgroup_id | hostname | port | max_replication_lag |
+--------------+---------------+------+---------------------+
| 1000 | 192.168.0.36 | 3306 | 10 | # 从库
| 100 | 192.168.0.218 | 3306 | 0 | # 主库
| 1000 | 192.168.0.218 | 3306 | 0 | # 主库
+--------------+---------------+------+---------------------+
3 rows in set (0.00 sec)
mysql> select * from global_variables where variable_name like 'mysql-monitor%lag%';
+-----------------------------------------------------+----------------+
| variable_name | variable_value |
+-----------------------------------------------------+----------------+
| mysql-monitor_replication_lag_group_by_host | false |
| mysql-monitor_replication_lag_timeout | 1000 |
| mysql-monitor_replication_lag_count | 1 |
| mysql-monitor_replication_lag_use_percona_heartbeat | |
| mysql-monitor_slave_lag_when_null | 60 |
| mysql-monitor_replication_lag_interval | 500 |
+-----------------------------------------------------+----------------+
6 rows in set (0.00 sec)
重要参考步骤---ProxySQL Cluster 集群搭建步骤的更多相关文章
- redis cluster 集群搭建步骤和注意事项
1.安装Ubuntu ,修改root的密码. sudo passwd (apt-get update 更新系统) 2.安装 Gcc 和G++ sudo apt-get install build- ...
- Redis Cluster集群搭建与应用
1.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper,但从redis 3.0之后版本支持redis-cluster集群,redis-cluster采用无中心结 ...
- redis 学习笔记(6)-cluster集群搭建
上次写redis的学习笔记还是2014年,一转眼已经快2年过去了,在段时间里,redis最大的变化之一就是cluster功能的正式发布,以前要搞redis集群,得借助一致性hash来自己搞shardi ...
- Redis Cluster集群搭建与配置
Redis Cluster是一种服务器sharding分片技术,关于Redis的集群方案应该怎么做,请参考我的另一篇博客http://www.cnblogs.com/xckk/p/6134655.ht ...
- Redis Cluster集群搭建<原>
一.环境配置 一台window 7上安装虚拟机,虚拟机中安装的是centos系统. 二.目标 Redis集群搭建的方式有多种,根据集群逻辑的位置,大致可以分为三大类:基于客户端分片的Redis ...
- Ubuntu 16.04下Redis Cluster集群搭建(官方原始方案)
前提:先安装好Redis,参考:http://www.cnblogs.com/EasonJim/p/7599941.html 说明:Redis Cluster集群模式可以做到动态增加节点和下线节点,使 ...
- Redis Cluster 集群搭建与扩容、缩容
说明:仍然是伪集群,所有的Redis节点,都在一个服务器上,采用不同配置文件,不同端口的形式实现 前提:已经安装好了Redis,本文的redis的版本是redis-6.2.3 Redis的下载.安装参 ...
- 基于Hadoop 2.2.0的高可用性集群搭建步骤(64位)
内容概要: CentSO_64bit集群搭建, hadoop2.2(64位)编译,安装,配置以及测试步骤 新版亮点: 基于yarn计算框架和高可用性DFS的第一个稳定版本. 注1:官网只提供32位re ...
- Redis进阶实践之十一 Redis的Cluster集群搭建
一.引言 本文档只对Redis的Cluster集群做简单的介绍,并没有对分布式系统的详细概念做深入的探讨.本文只是提供了有关如何设置集群.测试和操作集群的说明,而不涉及Redis集群规范中涵 ...
随机推荐
- Redis系列3:高可用之主从架构
Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 1 主从复制介绍 上一篇<Redis系列2:数据持久化提高可用性>中,我们介绍了Redis中的数据 ...
- Mac平台下git命令自动补全
一.安装bash-completion 安装Homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com ...
- JavaWeb--Servlet详解
前言 Java Web 其实就是一个技术的总和,把Web看成一个容器而已主要使用JavaEE技术来实现.在加上各种中间件. 整个javaWeb阶段的内容通过实际的案例贯穿学习, 所涉及到的技术知识点会 ...
- 说起分布式自增ID只知道UUID?SnowFlake(雪花)算法了解一下(Python3.0实现)
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_155 但凡说起分布式系统,我们肯定会对一些海量级的业务进行分拆,比如:用户表,订单表.因为数据量巨大一张表完全无法支撑,就会对其进 ...
- Luogu2986 [USACO10MAR]伟大的奶牛聚集 (树形DP)
有点权的重心,拆掉点dfs不就是了吗 //#include <iostream> #include <cstdio> #include <cstring> //#i ...
- HTTP/3,它来了
HTTP 3.0 是 HTTP 协议的第三个主要版本,前两个分别是 HTTP 1.0 和 HTTP 2.0 ,但其实 HTTP 1.1 我认为才是真正的 HTTP 1.0. 如果你对 HTTP 1.1 ...
- 【HMS core】【FAQ】典型问题合集7
1.[HMS core][Account Kit][问题描述] 集成华为帐号服务后,登录服务异常,无法获取用户信息,报statusCode为907135001,抓取报错日志:Failed to re ...
- kubernetes之镜像拉取策略ImagePullSecrets;
1.容器镜像是什么? 1.容器镜像(Container Image)是最终运行的软件: 2.容器镜像(最初为Docker镜像,现在叫OCI镜像更合适)是将软件打包的形式.但是容器镜像还可以携带额外的设 ...
- identity4 系列————案例篇[三]
前言 前文介绍了identity的用法,同时介绍了什么是identitySourece.apiSource.client 这几个概念,和具体案例,那么下面继续介绍案例了. 正文 这里用官网的案例,因为 ...
- 踩坑,发现一个ShardingJdbc读写分离的BUG
ShardingJdbc 怎么处理写完数据立即读的情况的呢? 写在前面 我本地使用了两个库来做写库(ds_0_master)和读库(ds_0_salve),两个库并没有配置主从. 下面我就使用库里的 ...