mysql部署模型_proxysql-单主模型MGR部署

1.proxysql-单主模型MGR部署

1.1 环境描述

4dbd97678fce9fce6c9e3b1a1fc3409c.png

主机

IP地址

角色

vm3

192.168.225.130

proxysql

vm4

192.168.225.131

mysql主

vm5

192.168.225.134

mysql从

vm6

192.168.225.140

mysql从

关闭防火墙,SELINUX

1.2 部署mysql主从

1.2.1 安装mariadb

[root@vm4 ~]# yum -y install mariadb*

[root@vm5 ~]# yum -y install mariadb*

[root@vm6 ~]# yum -y install mariadb*

1.2.2 主库授权账户给从库

##授权从库

[root@vm4 local]# mysql

MariaDB [(none)]> grant replication slave on *.* to 'repli'@'192.168.225.134' identified by '123456';

Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> grant replication slave on *.* to 'repli'@'192.168.225.140' identified by '123456';

Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> select user,repl_slave_priv from mysql.user where user='repli';

+-------+-----------------+

| user | repl_slave_priv |

+-------+-----------------+

| repli | Y |

| repli | Y |

+-------+-----------------+

2 rows in set (0.001 sec)

MariaDB [(none)]> flush privileges;

1.2.3 配置主数据库

[root@vm4 ~]# cd /etc/my.cnf.d/

[root@vm4 my.cnf.d]# vi mariadb-server.cnf

................................

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mariadb/mariadb.log

pid-file=/run/mariadb/mariadb.pid

server-id=10

log-bin=mysql-bin

...............................

[root@vm4 ~]# systemctl restart mariadb

[root@vm4 ~]# mysql

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 9

Server version: 10.3.17-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show master status;

+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000001 | 328 | | |

+------------------+----------+--------------+------------------+

1 row in set (0.000 sec)

1.2.4 配置从库

[root@vm5 ~]# vi /etc/my.cnf.d/mariadb-server.cnf

...............................

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mariadb/mariadb.log

pid-file=/run/mariadb/mariadb.pid

server-id=20

relay-log=mysql-relay-bin

.................................

[root@vm5 ~]# systemctl restart mariadb

[root@vm5 ~]# mysql

MariaDB [(none)]> change master to

-> master_host='192.168.225.131',

-> master_user='repli',

-> master_password='123456',

-> master_log_file='mysql-bin.000001',

-> master_log_pos=328;

Query OK, 0 rows affected (0.013 sec)

MariaDB [(none)]> start slave;

Query OK, 0 rows affected (0.003 sec)

MariaDB [(none)]> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.225.131

Master_User: repli

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 328

Relay_Log_File: mysql-relay-bin.000002

Relay_Log_Pos: 555

Relay_Master_Log_File: mysql-bin.000001

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

..............................................................

[root@vm6 ~]# vi /etc/my.cnf

...............................

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

server-id=21

relay-log=mysql-relay-bin

.................................

[root@vm6 ~]# systemctl restart mariadb

[root@vm6 ~]# mysql

MariaDB [(none)]> change master to

-> master_host='192.168.225.131',

-> master_user='repli',

-> master_password='123456',

-> master_log_file='mysql-bin.000001',

-> master_log_pos=328;

Query OK, 0 rows affected (0.013 sec)

MariaDB [(none)]> start slave;

Query OK, 0 rows affected (0.003 sec)

MariaDB [(none)]> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.225.131

Master_User: repli

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 328

Relay_Log_File: mysql-relay-bin.000002

Relay_Log_Pos: 540

Relay_Master_Log_File: mysql-bin.000001

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

1.3 proxysql部署

1.3.1 proxysql安装

配置proxysql源,地址

[root@vm3 ~]# vim /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

[root@vm3 ~]# yum clean all

[root@vm3 ~]# yum makecache

CentOS-8 - AppStream 1.5 MB/s | 6.3 MB 00:04

CentOS-8 - Base 1.0 MB/s | 2.3 MB 00:02

CentOS-8 - Extras 5.6 kB/s | 8.6 kB 00:01

ProxySQL 2.8 kB/s | 4.4 kB 00:01

Last metadata expiration chec

安装proxysql

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

1.3.2 配置proxysql

主库授权账号给proxysql

##授权一个账号给proxysql用于访问数据库

[root@vm4 ~]# mysql

MariaDB [(none)]> grant all on *.* to 'proxysql'@'192.168.225.130' identified by 'proxysql';

Query OK, 0 rows affected (0.001 sec)

##授权一个只读账号给proxysql用于健康检查

MariaDB [(none)]> grant select on *.* to 'monitor'@'192.168.225.130' identified by 'monitor';

Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> flush privileges;

登录proxysql管理接口

[root@vm3 ~]# export MYSQL_PS1="(\u@\h:\p) [\d]> "

[root@vm3 ~]# mysql -uadmin -padmin -h127.0.0.1 -P6666

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MySQL connection id is 2

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.

(admin@127.0.0.1:6666) [(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.001 sec)

在mysql_servers表中添加mysql主从主机

配置读组(vm5,vm6)、写组(vm4)

(admin@127.0.0.1:6666) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(10,'192.168.225.131',3306,1,'Write Group');

Query OK, 1 row affected (0.001 sec)

(admin@127.0.0.1:6666) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(20,'192.168.225.134',3306,1,'Read Group');

Query OK, 1 row affected (0.001 sec)

(admin@127.0.0.1:6666) [(none)]> insert into mysql_servers(hostgroup_id,hostname,port,weight,comment) values(20,'192.168.225.140',3306,1,'Read Group');

Query OK, 1 row affected (0.001 sec)

(admin@127.0.0.1:6666) [(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.225.131 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Write Group |

| 20 | 192.168.225.134 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Read Group |

| 20 | 192.168.225.140 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | Read Group |

+--------------+-----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+-------------+

3 rows in set (0.001 sec)

(admin@127.0.0.1:6666) [(none)]> load mysql servers to runtime;

Query OK, 0 rows affected (0.006 sec)

(admin@127.0.0.1:6666) [(none)]> save mysql servers to disk;

Query OK, 0 rows affected (0.035 sec)

在mysql_users添加主库上创建的proxysql账号

在 proxysql 主机的 mysql_users 表中添加刚才在 master 上创建的账号 proxysql,proxysql 客户端需要使用这个账号来访问数据库。

default_hostgroup默认组设置为写组,也就是10;当读写分离的路由规则不符合时,会访问默认组的数据库;

(admin@127.0.0.1:6666) [(none)]> insert into mysql_users(username,password,default_hostgroup,transaction_persistent) values('proxysql','proxysql',10,1);

Query OK, 1 row affected (0.001 sec)

(admin@127.0.0.1:6666) [(none)]> select * from mysql_users \G

*************************** 1. row ***************************

username: proxysql

password: proxysql

active: 1

use_ssl: 0

default_hostgroup: 10

default_schema: NULL

schema_locked: 0

transaction_persistent: 1

fast_forward: 0

backend: 1

frontend: 1

max_connections: 10000

1 row in set (0.001 sec)

(admin@127.0.0.1:6666) [(none)]> load mysql users to runtime;

Query OK, 0 rows affected (0.001 sec)

(admin@127.0.0.1:6666) [(none)]> save mysql users to disk;

Query OK, 0 rows affected (0.028 sec)

在proxysql端设置健康检查的账号

(admin@127.0.0.1:6666) [(none)]> set mysql-monitor_username='monitor';

Query OK, 1 row affected (0.001 sec)

(admin@127.0.0.1:6666) [(none)]> set mysql-monitor_password='monitor';

Query OK, 1 row affected (0.001 sec)

(admin@127.0.0.1:6666) [(none)]> load mysql variables to runtime;

Query OK, 0 rows affected (0.001 sec)

(admin@127.0.0.1:6666) [(none)]> save mysql variables to disk;

Query OK, 98 rows affected (0.006 sec)

添加读写分离的路由规则

注意:rule_id由小到大匹配,因此在设置rule_id时要预留足够的空间。

(admin@127.0.0.1:6666) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) values(10,1,'^SELECT.*FOR UPDATE$',10,1);

Query OK, 1 row affected (0.001 sec)

(admin@127.0.0.1:6666) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) values(20,1,'^SELECT',20,1);

Query OK, 1 row affected (0.001 sec)

(admin@127.0.0.1:6666) [(none)]> insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) values(30,1,'^SHOW',20,1);

Query OK, 1 row affected (0.000 sec)

(admin@127.0.0.1:6666) [(none)]> select rule_id,active,match_digest,destination_hostgroup,apply from mysql_query_rules;

+---------+--------+----------------------+-----------------------+-------+

| rule_id | active | match_digest | destination_hostgroup | apply |

+---------+--------+----------------------+-----------------------+-------+

| 10 | 1 | ^SELECT.*FOR UPDATE$ | 10 | 1 |

| 20 | 1 | ^SELECT | 20 | 1 |

| 30 | 1 | ^SHOW | 20 | 1 |

+---------+--------+----------------------+-----------------------+-------+

3 rows in set (0.000 sec)

(admin@127.0.0.1:6666) [(none)]> load mysql query rules to runtime;

Query OK, 0 rows affected (0.001 sec)

(admin@127.0.0.1:6666) [(none)]> save mysql query rules to disk;

Query OK, 0 rows affected (0.037 sec)

1.4 验证读写分离

1.4.1 登录proxysql模拟读写操作

##模拟写操作

[root@vm3 ~]# mysql -uproxysql -pproxysql -h127.0.0.1 -P6033

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.5.30 (ProxySQL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

(proxysql@127.0.0.1:6033) [(none)]> create database haha;

##模拟查询操作

(proxysql@127.0.0.1:6033) [(none)]> select * from mysql.user \G;

*************************** 1. row ***************************

Host: localhost

User: root

Password:

Select_priv: Y

Insert_priv: Y

Update_priv: Y

Delete_priv: Y

Create_priv: Y

Drop_priv: Y

Reload_priv: Y

.....................................

##模拟show操作

(proxysql@127.0.0.1:6033) [(none)]> show databases;

+--------------------+

| Database |

+--------------------+

| haha |

| information_schema |

| mysql |

| performance_schema |

| student |

+--------------------+

5 rows in set (0.004 sec)

1.4.2 验证读写分离是否成功

[root@vm3 ~]# mysql -uadmin -padmin -h127.0.0.1 -P6666

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MySQL connection id is 6

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.

(admin@127.0.0.1:6666) [(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 |

+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+

| 20 | information_schema | proxysql | 0x40B75DE8A4AD05EE | select * from mysql.user | 1 | 1609241562 | 1609241562 | 1740 | 1740 | 1740 |

| 20 | information_schema | proxysql | 0x65309B9B84E893C5 | select * from mysql.users | 2 | 1609241387 | 1609241558 | 31809 | 1660 | 30149 |

| 20 | information_schema | proxysql | 0x0F02B330C823D739 | select user,host from mysql.user | 1 | 1609240782 | 1609240782 | 35222 | 35222 | 35222 |

| 10 | information_schema | proxysql | 0xEB4549A4A010C504 | create database haha | 1 | 1609240749 | 1609240749 | 4299 | 4299 | 4299 |

| 20 | information_schema | proxysql | 0x02033E45904D3DF0 | show databases | 3 | 1609240723 | 1609241544 | 70697 | 3764 | 34361 |

| 10 | information_schema | proxysql | 0x594F2C744B698066 | select USER() | 2 | 1609240717 | 1609241353 | 0 | 0 | 0 |

| 10 | information_schema | proxysql | 0x226CD90D52A2BA0B | select @@version_comment limit ? | 2 | 1609240717 | 1609241353 | 0 | 0 | 0 |

+-----------+--------------------+----------+--------------------+----------------------------------+------------+------------+------------+----------+----------+----------+

7 rows in set (0.004 sec)

可知写操作被路由至10组(写组),读操作被路由至20组(读组)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值