实验目的:
基于proxysql实现MySQL的主从读写分离。
实验过程:
前期准备:
一共有四台虚拟机,其中三台为配置好的一主两从虚拟机,还有一台干净的虚拟机用来配置proxysql。
主机名 | 地址 |
master | 192.168.27.137 |
node1 | 192.168.27.139 |
node2 | 192.168.27.140 |
proxysql | 192.168.27.141 |
proxysql下载:(此处链接为官方,也可进入percona或者github官网进行下载适合的版本)ProxySQL - A High Performance Open Source MySQL Proxyhttps://www.proxysql.com/
实验过程:
在proxysql所在虚拟机配置:
下载并安装proxysql以及mysql客户端(mariadb):
[root@localhost ~]# yum install proxysql-2.5.5-1-centos8.x86_64.rpm
[root@localhost ~]# dnf install mariadb
启动proxysql服务:
[root@localhost ~]# systemctl enable --now proxysql
登录proxysql:
[root@localhost ~]# mysql -uadmin -padmin -h 127.0.0.1 -P 6032
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MySQL [(none)]> show databases;//查看数据库
+-----+---------------+-------------------------------------+
| seq | name | file |
+-----+---------------+-------------------------------------+
| 0 | main | |
| 2 | disk | /var/lib/proxysql/proxysql.db |
| 3 | stats | |
| 4 | monitor | |
| 5 | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
5 rows in set (0.000 sec)
可见有五个库: main、disk、stats 、monitor 和 stats_history
main: 内存配置数据库,即 MEMORY,表里存放后端 db 实例、用户验证、路由规则等信息。main 库中有如下信息:
MySQL [(none)]> show tables from main;
+----------------------------------------------------+
| tables |
+----------------------------------------------------+
| coredump_filters |
| global_variables |
| mysql_aws_aurora_hostgroups |
| mysql_collations |
| mysql_firewall_whitelist_rules |
| mysql_firewall_whitelist_sqli_fingerprints |
| mysql_firewall_whitelist_users |
| mysql_galera_hostgroups |
| mysql_group_replication_hostgroups |
| mysql_hostgroup_attributes |
| mysql_query_rules |
| mysql_query_rules_fast_routing |
| mysql_replication_hostgroups |
| mysql_servers |
| mysql_users |
| proxysql_servers |
| restapi_routes |
| runtime_checksums_values |
| runtime_coredump_filters |
| runtime_global_variables |
| runtime_mysql_aws_aurora_hostgroups |
| runtime_mysql_firewall_whitelist_rules |
| runtime_mysql_firewall_whitelist_sqli_fingerprints |
| runtime_mysql_firewall_whitelist_users |
| runtime_mysql_galera_hostgroups |
| runtime_mysql_group_replication_hostgroups |
| runtime_mysql_hostgroup_attributes |
| runtime_mysql_query_rules |
| runtime_mysql_query_rules_fast_routing |
| runtime_mysql_replication_hostgroups |
| runtime_mysql_servers |
| runtime_mysql_users |
| runtime_proxysql_servers |
| runtime_restapi_routes |
| runtime_scheduler |
| scheduler |
+----------------------------------------------------+
36 rows in set (0.001 sec)
mysql_servers: 后端可以连接 MySQL 服务器的列表
mysql_users: 配置后端数据库的账号和监控的账号。
mysql_query_rules: 指定 Query 路由到后端不同服务器的规则列表。
注: 表名以 runtime_开头的表示 ProxySQL 当前运行的配置内容,不能通过 DML 语句修改。
只能修改对应的不以 runtime 开头的表,然后 “LOAD” 使其生效,“SAVE” 使其存到硬盘以供下次重启加载。
在主库(master)中的配置:
创建proxysql的监控账户和对外访问账户:
mysql> create user 'monitor'@'192.168.%.%' identified with mysql_native_password by 'Monitor@123.com';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to 'monitor'@'192.168.%.%' with grant option;
Query OK, 0 rows affected (0.01 sec)
mysql> create user 'proxysql'@'192.168.%.%' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to 'proxysql'@'192.168.%.%' with grant option;
Query OK, 0 rows affected (0.00 sec)
配置proxysql:
查看需要用到的表的表结构:
MySQL [(none)]> show create table mysql_replication_hostgroups \G
*************************** 1. row ***************************table: mysql_replication_hostgroups
Create Table: CREATE TABLE mysql_replication_hostgroups (writer_hostgroup INT CHECK (writer_hostgroup>=0) NOT NULL PRIMARY KEY,reader_hostgroup INT NOT NULL CHECK (reader_hostgroup<>writer_hostgroup AND reader_hostgroup>=0),check_type VARCHAR CHECK (LOWER(check_type) IN ('read_only','innodb_read_only','super_read_only','read_only|innodb_read_only','read_only&innodb_read_only')) NOT NULL DEFAULT 'read_only',comment VARCHAR NOT NULL DEFAULT '', UNIQUE (reader_hostgroup))
1 row in set (0.000 sec)
创建新的组(定义写为1,读为0)
MySQL [(none)]> insert into mysql_replication_hostgroups (writer_hostgroup,reader_hostgroup,comment) values (1,0,'proxy');
Query OK, 1 row affected (0.000 sec)
MySQL [(none)]> load mysql servers to runtime;//加载到当前生效
Query OK, 0 rows affected (0.003 sec)
MySQL [(none)]> save mysql servers to disk;//持久化保存
Query OK, 0 rows affected (0.018 sec)
ProxySQL会根据server的read_only的取值将服务器进行分组。read_only=0的server,master被分到编号为1的写组,read_only=1的server,slave则分到编号为0的读组
MySQL [(none)]> select * from mysql_replication_hostgro
+------------------+------------------+------------+---
| writer_hostgroup | reader_hostgroup | check_type | co
+------------------+------------------+------------+---
| 1 | 0 | read_only | pr
+------------------+------------------+------------+---
1 row in set (0.000 sec)
添加主从服务器节点:
MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values (1,'192.168.27.137',3306);
Query OK, 1 row affected (0.000 sec)
MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values (0,'192.168.27.139',3306);
Query OK, 1 row affected (0.000 sec)
MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values (0,'192.168.27.140',3306);
Query OK, 1 row affected (0.000 sec)
MySQL [(none)]> load mysql servers to runtime;
Query OK, 0 rows affected (0.002 sec)
MySQL [(none)]> save mysql servers to disk;
Query OK, 0 rows affected (0.015 sec)
MySQL [(none)]> 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 | 192.168.27.137 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 0 | 192.168.27.139 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 0 | 192.168.27.140 | 3306 | 0 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+--------------+----------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
3 rows in set (0.000 sec)
为proxysql监控mysql后端节点:
MySQL [(none)]> use monitor//使用monitor
Database changed
MySQL [monitor]> set mysql-monitor_username='monitor';//创建用户
Query OK, 1 row affected (0.000 sec)
MySQL [monitor]> set mysql-monitor_password='Monitor@123.com';//填写密码
Query OK, 1 row affected (0.000 sec)
MySQL [monitor]> load mysql variables to runtime;//加载到当前
Query OK, 0 rows affected (0.001 sec)
MySQL [monitor]> save mysql variables to disk;//持久化保存
Query OK, 158 rows affected (0.003 sec)
MySQL [monitor]> select @@mysql-monitor_username;//查看用户名
+--------------------------+
| @@mysql-monitor_username |
+--------------------------+
| monitor |
+--------------------------+
1 row in set (0.001 sec)
MySQL [monitor]> select @@mysql-monitor_password;//查看密码
+--------------------------+
| @@mysql-monitor_password |
+--------------------------+
| Monitor@123.com |
+--------------------------+
1 row in set (0.001 sec)
MySQL [monitor]> select * from monitor.mysql_server_connect_log;//查看日志信息
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
| hostname | port | time_start_us | connect_success_time_us | connect_error |
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
| 192.168.27.140 | 3306 | 1709796180625505 | 0 | Access denied for user 'monitor'@'192.168.27.141' (using password: YES) |
| 192.168.27.137 | 3306 | 1709796181067381 | 0 | Access denied for user 'monitor'@'192.168.27.141' (using password: YES) |
| 192.168.27.139 | 3306 | 1709796181508932 | 0 | Access denied for user 'monitor'@'192.168.27.141' (using password: YES) |
| 192.168.27.140 | 3306 | 1709796240626303 | 0 | Access denied for user 'monitor'@'192.168.27.141' (using password: YES) |
| 192.168.27.137 | 3306 | 1709796241217740 | 0 | Access denied for user 'monitor'@'192.168.27.141' (using password: YES) |
| 192.168.27.139 | 3306 | 1709796241809274 | 0 | Access denied for user 'monitor'@'192.168.27.141' (using password: YES) |
| 192.168.27.139 | 3306 | 1709796300626918 | 0 | Access denied for user 'monitor'@'192.168.27.141' (using password: YES) |
| 192.168.27.137 | 3306 | 1709796301097796 | 0 | Access denied for user 'monitor'@'192.168.27.141' (using password: YES) |
| 192.168.27.140 | 3306 | 1709796301568472 | 0 | Access denied for user 'monitor'@'192.168.27.141' (using password: YES) |
| 192.168.27.139 | 3306 | 1709796306543844 | 1884 | NULL |
| 192.168.27.140 | 3306 | 1709796307338491 | 1778 | NULL |
| 192.168.27.137 | 3306 | 1709796308132494 | 1261 | NULL |
| 192.168.27.137 | 3306 | 1709796366544441 | 1317 | NULL |
| 192.168.27.139 | 3306 | 1709796367110254 | 1339 | NULL |
| 192.168.27.140 | 3306 | 1709796367683285 | 1771 | NULL |
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
15 rows in set (0.000 sec)
查看心跳信息:
MySQL [monitor]> select * from mysql_server_ping_log;
+----------------+------+------------------+--------------------------+
| hostname | port | time_start_us | ping_succe |
+----------------+------+------------------+--------------------------+
| 192.168.27.140 | 3306 | 1709796150706279 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796150706317 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796150707742 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796160707187 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796160707414 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796160709026 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796170707830 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796170707893 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796170709330 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796180708671 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796180708437 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796180711690 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796190709041 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796190709143 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796190710542 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796200709720 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796200709721 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796200710996 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796210710277 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796210710291 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796210711528 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796220711158 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796220711010 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796220712626 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796230711681 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796230711766 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796230712791 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796240712290 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796240712403 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796240714933 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796250712823 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796250712816 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796250714279 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796260713537 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796260713528 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796260715467 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796270714115 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796270714202 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796270715440 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796280714926 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796280715011 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796280716414 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796290715577 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796290715497 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796290718626 | 0 password: YES) |
| 192.168.27.140 | 3306 | 1709796300716091 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796300716161 | 0 password: YES) |
| 192.168.27.139 | 3306 | 1709796300717430 | 0 password: YES) |
| 192.168.27.137 | 3306 | 1709796306725083 | 234 |
| 192.168.27.140 | 3306 | 1709796306725072 | 160 |
| 192.168.27.139 | 3306 | 1709796306725193 | 132 |
| 192.168.27.137 | 3306 | 1709796316725952 | 436 |
| 192.168.27.139 | 3306 | 1709796316726154 | 259 |
| 192.168.27.140 | 3306 | 1709796316726119 | 301 |
| 192.168.27.140 | 3306 | 1709796326726613 | 501 |
| 192.168.27.139 | 3306 | 1709796326726788 | 353 |
| 192.168.27.137 | 3306 | 1709796326726752 | 396 |
| 192.168.27.137 | 3306 | 1709796336726831 | 425 |
| 192.168.27.140 | 3306 | 1709796336726960 | 319 |
| 192.168.27.139 | 3306 | 1709796336726942 | 343 |
| 192.168.27.140 | 3306 | 1709796346742368 | 1308 |
| 192.168.27.139 | 3306 | 1709796346742539 | 1165 |
| 192.168.27.137 | 3306 | 1709796346742517 | 1197 |
| 192.168.27.137 | 3306 | 1709796356743147 | 547 |
| 192.168.27.140 | 3306 | 1709796356743301 | 425 |
| 192.168.27.139 | 3306 | 1709796356743278 | 459 |
| 192.168.27.137 | 3306 | 1709796366743429 | 407 |
| 192.168.27.140 | 3306 | 1709796366743562 | 291 |
| 192.168.27.139 | 3306 | 1709796366743545 | 314 |
| 192.168.27.140 | 3306 | 1709796376745397 | 868 |
| 192.168.27.139 | 3306 | 1709796376745537 | 752 |
| 192.168.27.137 | 3306 | 1709796376745518 | 778 |
| 192.168.27.139 | 3306 | 1709796386745976 | 521 |
| 192.168.27.140 | 3306 | 1709796386746122 | 406 |
| 192.168.27.137 | 3306 | 1709796386746109 | 428 |
| 192.168.27.140 | 3306 | 1709796396746574 | 1349 |
| 192.168.27.137 | 3306 | 1709796396746710 | 1239 |
| 192.168.27.139 | 3306 | 1709796396746690 | 1266 |
| 192.168.27.137 | 3306 | 1709796406747306 | 1100 |
| 192.168.27.139 | 3306 | 1709796406747510 | 944 |
| 192.168.27.140 | 3306 | 1709796406747486 | 979 |
| 192.168.27.139 | 3306 | 1709796416748048 | 431 |
| 192.168.27.137 | 3306 | 1709796416748195 | 305 |
| 192.168.27.140 | 3306 | 1709796416748215 | 291 |
| 192.168.27.139 | 3306 | 1709796426748430 | 797 |
| 192.168.27.137 | 3306 | 1709796426748565 | 684 |
| 192.168.27.140 | 3306 | 1709796426748549 | 707 |
| 192.168.27.140 | 3306 | 1709796436748942 | 551 |
| 192.168.27.139 | 3306 | 1709796436749096 | 428 |
| 192.168.27.137 | 3306 | 1709796436749075 | 457 |
| 192.168.27.139 | 3306 | 1709796446749755 | 677 |
| 192.168.27.140 | 3306 | 1709796446749900 | 561 |
| 192.168.27.137 | 3306 | 1709796446749880 | 590 |
| 192.168.27.137 | 3306 | 1709796456753858 | 1380 |
| 192.168.27.139 | 3306 | 1709796456754148 | 1129 |
| 192.168.27.140 | 3306 | 1709796456754111 | 1176 |
| 192.168.27.140 | 3306 | 1709796466754180 | 882 |
| 192.168.27.137 | 3306 | 1709796466754360 | 741 |
| 192.168.27.139 | 3306 | 1709796466754339 | 769 |
| 192.168.27.137 | 3306 | 1709796476755056 | 389 |
| 192.168.27.139 | 3306 | 1709796476755239 | 229 |
| 192.168.27.140 | 3306 | 1709796476755187 | 294 |
| 192.168.27.139 | 3306 | 1709796486755641 | 441 |
| 192.168.27.137 | 3306 | 1709796486755703 | 397 |
| 192.168.27.140 | 3306 | 1709796486755684 | 422 |
| 192.168.27.137 | 3306 | 1709796496755840 | 479 |
| 192.168.27.140 | 3306 | 1709796496756003 | 381 |
| 192.168.27.139 | 3306 | 1709796496755990 | 401 |
| 192.168.27.139 | 3306 | 1709796506756185 | 348 |
| 192.168.27.137 | 3306 | 1709796506756298 | 260 |
| 192.168.27.140 | 3306 | 1709796506756313 | 252 |
| 192.168.27.140 | 3306 | 1709796516756932 | 517 |
| 192.168.27.139 | 3306 | 1709796516757138 | 331 |
| 192.168.27.137 | 3306 | 1709796516757160 | 367 |
| 192.168.27.140 | 3306 | 1709796526757309 | 337 |
| 192.168.27.139 | 3306 | 1709796526757433 | 236 |
| 192.168.27.137 | 3306 | 1709796526757417 | 258 |
| 192.168.27.139 | 3306 | 1709796536757782 | 458 |
| 192.168.27.137 | 3306 | 1709796536757910 | 349 |
| 192.168.27.140 | 3306 | 1709796536757894 | 372 |
| 192.168.27.140 | 3306 | 1709796546758288 | 477 |
| 192.168.27.137 | 3306 | 1709796546758460 | 327 |
| 192.168.27.139 | 3306 | 1709796546758436 | 358 |
| 192.168.27.137 | 3306 | 1709796556758969 | 473 |
| 192.168.27.139 | 3306 | 1709796556759144 | 345 |
| 192.168.27.140 | 3306 | 1709796556759119 | 377 |
| 192.168.27.140 | 3306 | 1709796566759542 | 440 |
| 192.168.27.137 | 3306 | 1709796566759671 | 330 |
| 192.168.27.139 | 3306 | 1709796566759654 | 355 |
+----------------+------+------------------+--------------------------+
129 rows in set (0.000 sec)
查看read_only监控:
MySQL [monitor]> select * from mysql_server_read_only_log;//此次查询结果报错信息全为0需要进行过更改
对两个从服务器的配置文件进行更改,添加read_only=1让两从文件只课读:
mysql> system vim /etc/my.cnf
mysql> system systemctl restart mysql
再次查看read_only表信息:|
| 192.168.27.137 | 3306 | 1709796956508275 | 485 | 0 | NULL |
| 192.168.27.139 | 3306 | 1709796956508323 | 446 | 1 | NULL |
| 192.168.27.139 | 3306 | 1709796958008641 | 627 | 1 | NULL |
| 192.168.27.140 | 3306 | 1709796958008789 | 516 | 1 | NULL |
| 192.168.27.137 | 3306 | 1709796958008776 | 542 | 0 | NULL
proxysql配置对外访问账号:
MySQL [monitor]> insert into
mysql_users(username,password,default_hostgroup,transaction_persistent) values ('proxysql','123456',1,1);
Query OK, 1 row affected (0.000 sec)
MySQL [monitor]> load mysql users to runtime;
Query OK, 0 rows affected (0.000 sec)
MySQL [monitor]> save mysql users to disk;
Query OK, 0 rows affected (0.011 sec)
MySQL [monitor]> select * from mysql_users\G
*************************** 1. row ***************************username: proxysqlpassword: 123456active: 1use_ssl: 0default_hostgroup: 1default_schema: NULLschema_locked: 0
transaction_persistent: 1fast_forward: 0backend: 1frontend: 1max_connections: 10000attributes: comment:
1 row in set (0.000 sec)
在从库192.168.27.139上通过对方询问账号proxysql连接,是否路由能默认到写组:
[root@node1 ~]# mysql -h192.168.27.141 -uproxysql -p'123456' -P 6033
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.30 (ProxySQL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
5 rows in set (0.01 sec)mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)mysql> create database keme;
Query OK, 1 row affected (0.01 sec)
在从库192.168.27.140上进行验证看是否能查看到keme:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| keme |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
6 rows in set (0.00 sec)