文章目录

  • 1.环境说明
  • 2.安装proxysql和安装mysql和配置主从
    • 2.1给四台mysql安装mariadb
    • 2.2安装proxysql
      • 2.2.1配置mysql-master和mysql-master2
      • 2.2.2登录proxysql管理端
      • 2.2.3使用 insert 语句添加 mysql 主从到 mysql_servers 表中,其中:hostgroup_id 1 表示写组,2表示读组
      • 2.2.4在mysql的 master和master2 端添加属于proxysql的只读账号
      • 2.2.5在proxysql主机端修改变量设置健康检测的账号
  • 3.验证读写分离

1.环境说明

ip 主机 服务
192.168.69.134 读写分离解析主机 proxysql
192.168.69.130 mysql-master mysql
192.168.69.132 mysql-master2 mysql
192.168.69.5 mysql-slave mysql
192.168.69.6 mysql-slave2 mysql


把所有主机关闭防火墙和setLinux

[root@proxysql ~]# setenforce 0
[root@proxysql ~]# systemctl stop firewalld

2.安装proxysql和安装mysql和配置主从

安装流程图

2.1给四台mysql安装mariadb

[root@mysql-master ~]# yum -y install mariadb mariadb-server
[root@mysql-master ~]# systemctl start mariadb
[root@mysql-master ~]# systemctl enable mariadb
[root@mysql-master ~]# ss -antl
State      Recv-Q Send-Q      Local Address:Port                     Peer Address:Port
LISTEN     0      50                      *:3306                                *:*
LISTEN     0      128                     *:22                                  *:*
LISTEN     0      100             127.0.0.1:25                                  *:*
LISTEN     0      128                    :::22                                 :::*
LISTEN     0      100                   ::1:25                                 :::*   

2.2安装proxysql

[root@proxysql ~]# cat <<EOF | tee /etc/yum.repos.d/proxysql.repo
[proxysql_repo]
name= ProxySQL
baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/7
gpgcheck=1
gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
EOF
[proxysql_repo]
name= ProxySQL
baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/7
gpgcheck=1
gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
[root@proxysql ~]# yum -y install proxysql
[root@proxysql ~]#  vi /etc/yum.repos.d/proxysql.repo
[proxysql_repo]
name= ProxySQL
baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/7
gpgcheck=1
enabled=1      \\设置开机启动
gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
[root@proxysql ~]# yum -y install mariadb-5.5.60-1.el7_5.x86_64
[root@proxysql ~]#  service proxysql start
Starting ProxySQL: 2019-03-14 15:23:54 [INFO] Using config file /etc/proxysql.cnf
DONE!
[root@proxysql ~]# ss -antl
State      Recv-Q Send-Q      Local Address:Port                     Peer Address:Port
LISTEN     0      128                     *:6032                                *:*
LISTEN     0      128                     *:6033                                *:*
LISTEN     0      128                     *:6033                                *:*
LISTEN     0      128                     *:6033                                *:*
LISTEN     0      128                     *:6033                                *:*
LISTEN     0      128                     *:22                                  *:*
LISTEN     0      100             127.0.0.1:25                                  *:*
LISTEN     0      128                    :::22                                 :::*
LISTEN     0      100                   ::1:25                                 :::*

2.2.1配置mysql-master和mysql-master2

[root@mysql-master ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
MariaDB [(none)]> grant all on *.* to 'proxysql'@'192.168.69.134' identified by 'pwproxysql';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

2.2.2登录proxysql管理端

[root@proxysql ~]# export MYSQL_PS1="(\u@\h:\p) [\d]> "
[root@proxysql ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
(admin@127.0.0.1:6032) [(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.00 sec)(admin@127.0.0.1:6032) [(none)]> show tables from main;
+--------------------------------------------+
| tables                                     |
+--------------------------------------------+
| global_variables                           |
| mysql_collations                           |
| mysql_group_replication_hostgroups         |
| mysql_query_rules                          |
| mysql_query_rules_fast_routing             |
| mysql_replication_hostgroups               |
| mysql_servers                              |
| mysql_users                                |
| proxysql_servers                           |
| runtime_checksums_values                   |
| runtime_global_variables                   |
| runtime_mysql_group_replication_hostgroups |
| runtime_mysql_query_rules                  |
| runtime_mysql_query_rules_fast_routing     |
| runtime_mysql_replication_hostgroups       |
| runtime_mysql_servers                      |
| runtime_mysql_users                        |
| runtime_proxysql_servers                   |
| runtime_scheduler                          |
| scheduler                                  |
+--------------------------------------------+
20 rows in set (0.00 sec)

2.2.3使用 insert 语句添加 mysql 主从到 mysql_servers 表中,其中:hostgroup_id 1 表示写组,2表示读组

(admin@127.0.0.1:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'192.168.69.130',3306,1,'Write Group');
Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(1,'192.168.69.132',3306,1,'Write Group');
Query OK, 1 row affected (0.00 sec)(admin@127.0.0.1:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'192.168.69.5',3306,1,'Read Group');
Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(2,'192.168.69.6',3306,1,'Read Group');
Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> select * from mysql_servers;
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
| hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment     |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
| 1            | 192.168.69.130 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Write Group |
| 1            | 192.168.69.132 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Write Group |
| 2            | 192.168.69.5   | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Read Group  |
| 2            | 192.168.69.6   | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Read Group  |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+
4 rows in set (0.00 sec)######修改后,需要加载到RUNTIME,并保存到disk######(admin@127.0.0.1:6032) [(none)]> load mysql servers to runtime;
Query OK, 0 rows affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> save mysql servers to disk;
Query OK, 0 rows affected (0.03 sec)
(admin@127.0.0.1:6032) [(none)]> insert into mysql_users(username,password,default_hostgroup,transaction_persistent)values('proxysql','pwproxysql',1,1);
Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> select * from mysql_users \G
*************************** 1. row ***************************username: proxysqlpassword: pwproxysqlactive: 1use_ssl: 0default_hostgroup: 1default_schema: NULLschema_locked: 0
transaction_persistent: 1fast_forward: 0backend: 1frontend: 1max_connections: 1
(admin@127.0.0.1:6032) [(none)]> load mysql users to runtime;
Query OK, 0 rows affected (0.00 sec)(admin@127.0.0.1:6032) [(none)]> save mysql users to disk;
Query OK, 0 rows affected (0.00 sec)

2.2.4在mysql的 master和master2 端添加属于proxysql的只读账号

[root@mysql-master ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
MariaDB [(none)]> GRANT SELECT ON *.* TO 'monitor'@'192.168.69.%' IDENTIFIED BY 'monitor';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

2.2.5在proxysql主机端修改变量设置健康检测的账号

(admin@127.0.0.1:6032) [(none)]> set mysql-monitor_username='monitor';
Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> set mysql-monitor_password='monitor';
Query OK, 1 row affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> load mysql variables to runtime;
Query OK, 0 rows affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> load mysql variables to runtime;
Query OK, 0 rows affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> save mysql variables to disk;
Query OK, 97 rows affected (0.01 sec)

添加读写分离的路由规则
需求:
将 select 查询语句全部路由至 hostgroup_id=2 的组(也就是读组)
但是 select * from tb for update 这样的语句是会修改数据的,所以需要单独定义,将它路由至 hostgroup_id=1 的组(也就是写组)
其他没有被规则匹配到的组将会被路由至用户默认的组(mysql_users 表中的 default_hostgroup)

(admin@127.0.0.1:6032) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',1,1);
Query OK, 1 row affected (0.00 sec)(admin@127.0.0.1:6032) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(2,1,'^SELECT',2,1);
Query OK, 1 row affected (0.00 sec)(admin@127.0.0.1:6032) [(none)]> select rule_id,active,match_digest,destination_hostgroup,apply from mysql_query_rules;
+---------+--------+----------------------+-----------------------+-------+
| rule_id | active | match_digest         | destination_hostgroup | apply |
+---------+--------+----------------------+-----------------------+-------+
| 1       | 1      | ^SELECT.*FOR UPDATE$ | 1                     | 1     |
| 2       | 1      | ^SELECT              | 2                     | 1     |
+---------+--------+----------------------+-----------------------+-------+
2 rows in set (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> load mysql query rules to runtime;
Query OK, 0 rows affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.00 sec)
(admin@127.0.0.1:6032) [(none)]> save mysql query rules to disk;
Query OK, 0 rows affected (0.01 sec)
(admin@127.0.0.1:6032) [(none)]> save admin variables to disk;
Query OK, 31 rows affected (0.00 sec)

3.验证读写分离

登录用户是刚才我们在 mysql_user 表中创建的用户,端口为6033

[root@proxysql ~]# mysql -uproxysql -ppwproxysql -h127.0.0.1 -P6033
Welcome to the MariaDB monitor.  Commands end with ; or \g.
(proxysql@127.0.0.1:6033) [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
(proxysql@127.0.0.1:6033) [(none)]> create database momoda;
Query OK, 1 row affected (0.00 sec)
(proxysql@127.0.0.1:6033) [(none)]> select user,host from mysql.user;
+----------+-----------------+
| user     | host            |
+----------+-----------------+
| root     | 127.0.0.1       |
| proxysql | 192.168.69.134 |
| root     | ::1             |
|          | localhost       |
| root     | localhost       |
|          | server01        |
| root     | server01        |
+----------+-----------------+
7 rows in set (0.00 sec)
[root@proxysql ~]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
(admin@127.0.0.1:6032) [(none)]> select * from stats_mysql_query_digest;
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| hostgroup | schemaname         | username | digest             | digest_text                      | count_star | first_seen | last_seen  | sum_time | min_time | max_time |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
| 2         | information_schema | proxysql | 0x581BB90874C061B7 | select mysql,user from user      | 1          | 1552551000 | 1552551000 | 1169816  | 1169816  | 1169816  |
| 2         | information_schema | proxysql | 0x2387402BA97B0B3F | select ?.?.?.?,user from user    | 1          | 1552550928 | 1552550928 | 1813598  | 1813598  | 1813598  |
| 1         | information_schema | proxysql | 0xF8AA204B58F9D7C3 | create database momoda           | 1          | 1552550078 | 1552550078 | 2627     | 2627     | 2627     |
| 2         | information_schema | proxysql | 0x0F02B330C823D739 | select user,host from mysql.user | 7          | 1552550180 | 1552550887 | 14445411 | 1174085  | 3458309  |
| 1         | information_schema | proxysql | 0x02033E45904D3DF0 | show databases                   | 2          | 1552550063 | 1552550978 | 8040     | 2241     | 5799     |
| 1         | information_schema | proxysql | 0x594F2C744B698066 | select USER()                    | 3          | 1552550050 | 1552550882 | 0        | 0        | 0        |
| 2         | information_schema | proxysql | 0x57E16F2FF7887CFE | select host,user from user       | 1          | 1552550910 | 1552550910 | 1786983  | 1786983  | 1786983  |
| 1         | information_schema | proxysql | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 3          | 1552550050 | 1552550882 | 0        | 0        | 0        |
+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+
8 rows in set (0.00 sec)

ProxySQL实现双主双从读写分离部署相关推荐

  1. MySQL集群系列2:通过keepalived实现双主集群读写分离

    在上一节基础上,通过添加keepalived实现读写分离. 首先关闭防火墙 安装keepalived keepalived 2台机器都要安装 rpm -q openssl kernel-devel y ...

  2. 高可用Mysql架构_Mysql主从复制、Mysql双主热备、Mysql双主双从、Mysql读写分离(Mycat中间件)、Mysql分库分表架构(Mycat中间件)的演变...

    [Mysql主从复制] 解决的问题 数据分布:比如一共150台机器,分别往电信.网通.移动各放50台,这样无论在哪个网络访问都很快.其次按照地域,比如国内国外,北方南方,这样地域性访问解决了. 负载均 ...

  3. Mycat实现Mysql的一主一从和双主双从读写分离

    前提是: 已经有了一主一从的Mysql服务架构. 已经安装启动好了mycat.并对mycat有一定了解. 实现 修改mycat的schema配置文件: <!--schema 标签用于定义 MyC ...

  4. MyCat分布式数据库集群架构工作笔记0015---高可用_负载均衡_Mycat双主双从读写分离

    技术交流QQ群[JAVA,C++,Python,.NET,BigData,AI]:170933152 好的,要配置读写分离,咱们要修改,mycat的配置文件 咱们去host97这台主机上去看看配置文件 ...

  5. 数据库读写分离(单主单从+双主双从)

    单主单从 name ip node1 192.168.44.100 安装mycat node5 192.168.44.33 主 node3 192.168.44.11 从 读写分离的前提是首先要先将数 ...

  6. MyCat实现MySQL读写分离(双主双从多库)

    在数据库集群架构中,主数据库负责处理事务性查询(写入操作),而从数据库只负责处理select查询(读操作),这样可以提高数据库整体读写性能.主数据库另外一个功能就是负责将数据变更同步到从数据库中. 读 ...

  7. Keepalived+LVS+MySQL双主复制实现读写负载均衡及高可用

    目录 一.Keepalived和LVS简介 1. Keepalived简介 2. LVS简介 二.安装配置 1. 下载安装LVS 2. 下载安装Keepalived 3. Keepalived配置 5 ...

  8. MyCat学习:使用MySQL搭建主从复制(双主双从模式)

    首先,上一篇我们建立了一主一丛模式的MySQL读写分离 点击这里 那么接下来,我们搭建双主双从模式的读写分离,并且使用MyCat登录访问 原理:master1,master2,slave1,slave ...

  9. mycat与mysql proxy_高可用Mysql架构_Mycat集群部署(HAProxy + 两台Mycat+Mysql双主双从)...

    既然大家都知道了Mysql分布式在大型网站架构中的作用,在这里就不再阐述.本片博客文章是基于我曾经搭建过的一个Mysql集群基础上实现的,实现过双主热备.读写分离.分库分表. 环境 centos6.4 ...

  10. Mysql 集群双主双从安装使用详细讲解

    文章目录 下载Mysql 安装单机Mysql 配置Mysql集群 双Master配置 master1配置 master2配置 配置说明 双Slave配置 Slave1配置 Slave2配置 双 Mas ...

最新文章

  1. 【linux】CentOS启动后网络自动配置过程
  2. linux系统中find怎么用,linux系统中‘find’的详细用法
  3. mysql中取出的时间日期多个.0
  4. soj1209- 最短的距离(精度问题)
  5. 【Windows环境】Fiddler发送POST请求携带文件问题记录
  6. Python基础之:数字字符串和列表
  7. 使用PDB调试Python程序的完整实践
  8. numa节点_极致性能(1):以NUMA为起点
  9. 使用JDK 13查看TLS配置
  10. VS 如何修改C++编译标准
  11. 动态规划求一个序列的最长回文子序列(Longest Palindromic Substring )
  12. 作者:孔新川,杭州迈宁数据科技有限公司创始人、CEO。
  13. CentOS下调整VolGroup-lv_root分区大小
  14. 逾期怎么处理_信用卡3万逾期三个月,催收说已经起诉生效,立案处理了,该怎么办?...
  15. 小鱼易连推出肩膀计划,打造云+端双师课堂解决方案
  16. 【Linux】bash: groupadd: command not found a
  17. UVALIve 5987 素数
  18. java JDK安装及环境变量配置
  19. 如何使新浪播放器自动播放
  20. 老板要我开发一个OA多级审批流(干货满满)

热门文章

  1. QMT量化交易软件使用攻略(二)-策略编写
  2. 关于同一个网络A电脑可以ping通B电脑,而B电脑却ping不通A电脑,但A可以远程连接上B解决方法
  3. java 设计模式 路由器_Java设计模式——工厂模式
  4. 2020中国机器人公司排行榜TOP10揭晓
  5. 勒索软件对企业的重大威胁分析
  6. 浅析:OMS和ERP、WMS、TMS之间的关系?
  7. 2012年度IT博客大赛50强报道:宋守炯
  8. 2013手机游戏引擎趋势:竞争升级
  9. 从删库到跑路or恢复,记一次MySQL数据库文件损坏恢复经历
  10. 数据中心技术最新发展趋势