linuxsir首页 LinuxSir.Org | Linux、BSD、Solaris、Unix | 开源传万世,因有我参与欢迎您!
网站首页 | 设为首页 | 加入收藏
您所在的位置:主页 > Linux数据库 >

ProxySQL实现MySQL读写分离

时间:2019-05-21  来源:未知  作者:admin666

注意事项:在实现主从复制时从节点在配置文件中必须要设置read_only,这是ProxySQL区分是用来作为读服务器还是写服务器的依据

一、实现主从复制 主节点配置

1.修改配置文件

[root@Master ~]# vim /etc/my.cnf
[mysqld]
server-id=1
log-bin
binlog-format=row

2.启动MySQL服务

[root@Master ~]# systemctl start mariadb
[root@Master ~]# mysql -e "SHOW MASTER LOGS;"
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 245 |
+--------------------+-----------+

3.创建用来复制的账号

[root@Master ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.73.%' IDENTIFIED BY 'CentOS';"
从节点配置

1.修改配置文件

[root@Slave ~]# vim /etc/my.cnf
[mysqld]
server-id=2
log-bin
binlog-format=row
read-only #必须写

2.启动数据库服务

[root@Slave ~]# systemctl start mariadb

3.写入CHANGE MASTSER TO信息

MariaDB [(none)] CHANGE MASTER TO MASTER_HOST='192.168.73.110', MASTER_USER='repluser',MASTER_PASSWORD='centos',MASTER_PORT=3306,MASTER_LOG_FILE='mariadb-bin.000001',MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.00 sec)

4.启动复制线程

MariaDB [(none)] START SLAVE;
Query OK, 0 rows affected (0.00 sec)

5.查看状态

MariaDB [(none)] SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
 Slave_IO_State: Waiting for master to send event
 Master_Host: 192.168.73.110
 Master_User: repluser
 Master_Port: 3306
 Connect_Retry: 60
 Master_Log_File: mariadb-bin.000001
 Read_Master_Log_Pos: 402
 Relay_Log_File: mariadb-relay-bin.000002
 Relay_Log_Pos: 688
 Relay_Master_Log_File: mariadb-bin.000001
 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes

1.主节点导入数据库

[root@Master ~]# mysql hellodb_innodb.sql 
[root@Master ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+

2.从节点查看

[root@Slave ~]# mysql -e "SHOW DATABASES;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+

主从复制配置完毕

二、在ProxySQL上配置读写分离

1.在ProxySQL主机上配置yum源

[root@ProxySQL ~]# vim /etc/yum.repos.d/proxysql.repo
[proxysql_repo] 
name= ProxySQL YUM repository 
baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/\$releasever 
gpgcheck=1
gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key 

2.安装ProxySQL和mariadb客户端
ProxySQL内置了一个轻量级的数据库,所以需要有MySQL客户端连上去对其进行配置

[root@ProxySQL ~]# yum install proxysql mariadb -y

3.启动ProxySQL服务

[root@ProxySQL ~]# service proxysql start
Starting ProxySQL: 2019-05-08 14:03:07 [INFO] Using config file /etc/proxysql.cnf
DONE!

4.连接管理端口

[root@ProxySQL ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1
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.

5.将MySQL主从服务器信息添加入mysql_servers表中
先将主从服务器存放在同一组内,等指定好读写规则后,系统会根据配置文件中的read-only值自动将其分别添加至读组和写组。

MySQL [(none)] INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,'192.168.73.110',3306);
Query OK, 1 row affected (0.00 sec)
MySQL [(none)] INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,'192.168.73.111',3306);
Query OK, 1 row affected (0.00 sec)
MySQL [(none)] SELECT * FROM mysql_servers
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10 | 192.168.73.110 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 10 | 192.168.73.111 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
2 rows in set (0.00 sec)

6.在MySQL服务器的主节点上为ProxySQL添加账号用来查看MySQL节点是主还是从

[root@Master ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO 'monitor'@'192.168.73.%' IDENTIFIED BY 'centos';"

7.在Proxy上配置监控账号

MySQL [(none)] SET mysql-monitor_username='monitor';
Query OK, 1 row affected (0.00 sec)
MySQL [(none)] SET mysql-monitor_password='centos';
Query OK, 1 row affected (0.00 sec)

8.将配置加载至内存,将配置保存至磁盘

MySQL [(none)] LOAD MYSQL VARIABLES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)
MySQL [(none)] SAVE MYSQL VARIABLES TO DISK;
Query OK, 97 rows affected (0.00 sec)

9.测试
9.1查看连接状态

MySQL [(none)] select * from mysql_server_connect_log;
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
| hostname | port | time_start_us | connect_success_time_us | connect_error |
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
| 192.168.73.110 | 3306 | 1557296528658352 | 0 | Access denied for user 'monitor'@'192.168.73.112' (using password: YES) |
| 192.168.73.111 | 3306 | 1557296648056186 | 0 | Access denied for user 'monitor'@'192.168.73.112' (using password: YES) |
| 192.168.73.110 | 3306 | 1557296649025169 | 0 | Access denied for user 'monitor'@'192.168.73.112' (using password: YES) |
| 192.168.73.110 | 3306 | 1557296708057600 | 0 | Access denied for user 'monitor'@'192.168.73.112' (using password: YES) |
| 192.168.73.111 | 3306 | 1557296708872496 | 0 | Access denied for user 'monitor'@'192.168.73.112' (using password: YES) |
| 192.168.73.110 | 3306 | 1557296758752550 | 2763 | NULL | #此前由于没有创建监控账号所以连接一直失败
| 192.168.73.111 | 3306 | 1557296759862679 | 3205 | NULL |
| 192.168.73.110 | 3306 | 1557296818752346 | 1014 | NULL |
| 192.168.73.111 | 3306 | 1557296819498108 | 3120 | NULL |
| 192.168.73.110 | 3306 | 1557296878752978 | 3245 | NULL |
| 192.168.73.111 | 3306 | 1557296879410404 | 3063 | NULL |
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
22 rows in set (0.00 sec) 

9.2测试连接ping

MySQL [(none)] select * from mysql_server_ping_log;
+----------------+------+------------------+----------------------+-------------------------------------------------------------------------+
| hostname | port | time_start_us | ping_success_time_us | ping_error |
+----------------+------+------------------+----------------------+-------------------------------------------------------------------------+
| 192.168.73.111 | 3306 | 1557296508118738 | 0 | Access denied for user 'monitor'@'192.168.73.112' (using password: YES) |
| 192.168.73.110 | 3306 | 1557296508302837 | 0 | Access denied for user 'monitor'@'192.168.73.112' (using password: YES) |
...中间省略...
| 192.168.73.110 | 3306 | 1557297088874658 | 675 | NULL |
| 192.168.73.111 | 3306 | 1557297089037256 | 435 | NULL |
| 192.168.73.110 | 3306 | 1557297098875954 | 1144 | NULL |
| 192.168.73.111 | 3306 | 1557297099069333 | 1252 | NULL |
+----------------+------+------------------+----------------------+-------------------------------------------------------------------------+
122 rows in set (0.00 sec)
#已经可以联通

10.设置读写分组

MySQL [(none)] INSERT INTO mysql_replication_hostgroups VALUES(10,20,"test");
Query OK, 1 row affected (0.00 sec)
MySQL [(none)] SELECT * FROM mysql_replication_hostgroups;
+------------------+------------------+---------+
| writer_hostgroup | reader_hostgroup | comment |
+------------------+------------------+---------+
| 10 | 20 | test |
+------------------+------------------+---------+
1 row in set (0.00 sec)

11.让读写表生效

MySQL [(none)] LOAD MYSQL SERVERS TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)

12.查看mysql_server表此时已经将服务器分组

MySQL [(none)] SELECT * FROM mysql_servers;
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10 | 192.168.73.110 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 20 | 192.168.73.111 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
2 rows in set (0.00 sec)

13.保存配置至磁盘

MySQL [(none)] SAVE MYSQL SERVERS TO DISK;
Query OK, 0 rows affected (0.02 sec)

至此读写分离配置完毕,接下来需要定义读写分离的规则

三、定义读写分离规则

1.在主节点上创建一个账户让客户端连接调度器去访问主从服务器(此处授予的权限较大,实际生产中可以根据需要定义指定的那张表)

[root@Master ~]# mysql -e "GRANT ALL ON *.* TO 'sqluser'@'192.168.73.%' IDENTIFIED BY 'centos';"

2.在ProxySQL服务器上,将sqluser用户添加至mysql_users表中

MySQL [(none)] INSERT INTO mysql_users(username,password,default_hostgroup) VALUES ('sqluser','centos',10);
Query OK, 1 row affected (0.00 sec)

3.查看mysql_user表信息

MySQL [(none)] SELECT * FROM mysql_users;
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| sqluser | centos | 1 | 0 | 10 | NULL | 0 | 1 | 0 | 1 | 1 | 10000 |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
1 row in set (0.00 sec)

4.生效存盘

MySQL [(none)] load mysql users to runtime;
Query OK, 0 rows affected (0.00 sec)
MySQL [(none)] SAVE MYSQL USERS TO DISK;
Query OK, 0 rows affected (0.00 sec)

5.测试
目前尚未设置读写路由规则,所有的请求都是发往主节点

[root@Client ~]# mysql -usqluser -pcentos -h192.168.73.112 -P6033 -e "SELECT @@server_id;"
+-------------+
| @@server_id |
+-------------+
| 1 |
+-------------+

6.在ProxySQL上定义调度规则

MySQL [(none)] INSERT INTO mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) VALUES (1,1,'^SELECT.*FOR UPDATE$',10,1),(2,1,'^SELECT',20,1);
Query OK, 2 rows affected (0.00 sec)

7.查看定义规则

MySQL [(none)] SELECT * FROM mysql_query_rules\G;
*************************** 1. row ***************************
 rule_id: 1
 active: 1
 username: NULL
 schemaname: NULL
 flagIN: 0
 client_addr: NULL
 proxy_addr: NULL
 proxy_port: NULL
 digest: NULL
 match_digest: ^SELECT.*FOR UPDATE$
 match_pattern: NULL
 negate_match_pattern: 0
 re_modifiers: CASELESS
 flagOUT: NULL
 replace_pattern: NULL
destination_hostgroup: 10
 cache_ttl: NULL
 reconnect: NULL
 timeout: NULL
 retries: NULL
 delay: NULL
 next_query_flagIN: NULL
 mirror_flagOUT: NULL
 mirror_hostgroup: NULL
 error_msg: NULL
 OK_msg: NULL
 sticky_conn: NULL
 multiplex: NULL
 log: NULL
 apply: 1
 comment: NULL
*************************** 2. row ***************************
 rule_id: 2
 active: 1
 username: NULL
 schemaname: NULL
 flagIN: 0
 client_addr: NULL
 proxy_addr: NULL
 proxy_port: NULL
 digest: NULL
 match_digest: ^SELECT
 match_pattern: NULL
 negate_match_pattern: 0
 re_modifiers: CASELESS
 flagOUT: NULL
 replace_pattern: NULL
destination_hostgroup: 20
 cache_ttl: NULL
 reconnect: NULL
 timeout: NULL
 retries: NULL
 delay: NULL
 next_query_flagIN: NULL
 mirror_flagOUT: NULL
 mirror_hostgroup: NULL
 error_msg: NULL
 OK_msg: NULL
 sticky_conn: NULL
 multiplex: NULL
 log: NULL
 apply: 1
 comment: NULL
2 rows in set (0.00 sec)

8.生效存盘

MySQL [(none)] LOAD MYSQL QUERY RULES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)
MySQL [(none)] SAVE MYSQL QUERY RULES TO DISK;
Query OK, 0 rows affected (0.00 sec)
四、在Client端测试

1.查询操作

[root@Client ~]# mysql -usqluser -pcentos -h192.168.73.112 -P6033 -e "SELECT @@server_id;"
+-------------+
| @@server_id |
+-------------+
| 2 |
+-------------+

2.写操作

[root@Client ~]# mysql -usqluser -pcentos -h192.168.73.112 -P6033 -e "BEGIN;INSERT hellodb.teachers VALUE(5,'Long',30,'M');SELECT @@server_id;commit;"
+-------------+
| @@server_id |
+-------------+
| 1 |
+-------------+

Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx

上一篇:Oracle报错ORA
友情链接
  • Salesforce闹剧:共享用户数据后陷入数小时的瘫痪
  • 韩国政府计划从Windows 7迁移到Linux
  • Redis作者antirez:开源维护者的挣扎
  • 最常用还是最近搜索?你的Chrome地址栏如何自动填充?
  • Stack Overflow安全事件新进展:部分用户私人信息遭窃
  • 英特尔发布Clear Linux开发者版本
  • 思科报告Nexus 9000数据中心交换机,PI软件和EPN管理器中的严重
  • Google AI工程师介绍Translatotron,一种端到端的直接语音翻译模
  • Racket 7.3 发布,改进了Racket
  • V8 7.5 Beta 发布,WebAssembly隐式缓存
  • 开发人员辩论是否为Fedora 31阻止基于密码的Root SSH登录
  • Mozilla,Cloudflare和其他人建议使用BinaryAST以加快JavaScript
  • GCC 10支持使用SSE指令模拟MMX
  • KDE Plasma 5.16 beta发布,许多增强功能
  • Microsoft开源VS Code扩展Web Template Studio,可轻松创建全栈W
  • NotePad++ 正则表达式替换高级用法
  • 针对Linux 5.1内核更新的MuQSS补丁
  • DXVK 1.2 发布,支持Direct3D 11特定于供应商的扩展
  • NVIDIA 430.14 Linux驱动程序发布,Vulkan性能优化
  • SMPlayer 19.5.0发布,如何在Ubuntu中安装它
  • Phoronix Test Suite 8.8 正式发布
  • LLVM Clang 9.0采用初始C2x语言模式
  • 使用Linux操作系统的网络安全:您需要知道的一切
  • SwiftWasm
  • Mesa 19.1
  • Wine 4.0.1 发布,44个修复
  • Flutter启用新的lint规则来构建更好的Chrome OS应用程序
  • Proton 4.2
  • 微软开源基于近邻图的最近邻搜索算法SPTAG
  • GitHub Package Registry测试版发布,新的软件包管理服务
  • Mozilla修复旧版Firefox插件bug,专用扩展现可用于Firefox 52
  • 如何在Mozilla Firefox中获得扩展建议
  • Google和Collabora为Chrome OS设备的Linux 5.1内核进行重大更改
  • Ant Design 3.18.1 发布,企业级UI设计语言
  • Atom 1.37 发布,跨平台文本编辑器
  • Apple发布iOS 12.3和tvOS 12.3,macOS Mojave 10.14.5,watchOS
  • G7国家将在6月模拟跨境网络攻击
  • VirtualBox 6.0.8 发布,Linux Kernel 3.16.35与共享文件夹一起
  • Linux kernel 5.1.1第一个点发布,可准备好进行大规模部署
  • KDE Frameworks 5.58 发布,许多修复及改进
  • 英特尔的Clear Linux OS为Linux开发人员提供量身定制的工作流程
  • KDE Plasma 5.16桌面环境将带来全新的通知
  • 微软确认WSL与WSL2共存,以及Windows Linux子系统的更多信息
  • Apple发布了iOS 12.4,macOS 10.14.6,watchOS 5.3和tvOS 12.4的
  • Clear Linux 准备新的内核选项
  • Linux新闻 第972页
  • 如何在Ubuntu 18.04上配置MySQL主从复制
  • ORA
  • 领航时时彩
  • 九龙图库下载
  • 现金开户
  • 宝盈娱乐注册-上鼎狐网
  • 老时时彩360