赞
踩
当用户在发送请求数据时,请求经过中间件,中间件将请求中的读和写操作分辨。将读请求发送给后端的从服务器;将写请求发送给后端的主服务器,然后主服务器通过主从复制将数据复制给其他从服务器;
- 主机 ip地址
- Master 192.168.14.210
- Slave 192.168.14.211
- Client 192.168.14.212
- ProxySQL 192.168.14.213
注意事项:ProxySQL在实现读写分离之前先要实现主从复制的共功能;主从复制时从节点在配置文件中必须要设置read_only,这是ProxySQL区分是用来作为读服务器还是写服务器的依据;
1、查看文章https://blog.csdn.net/tladagio/article/details/103137845
1、添加yum源
- [root@proxysql ~]# vi /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客户端
[root@proxysql ~]# yum install -y proxysql mariadb
3、启动ProxySQL服务
- [root@proxysql ~]# systemctl start proxysql
- [root@proxysql ~]# firewall-cmd --permanent --zone=public --add-port=6032/tcp
- [root@proxysql ~]# firewall-cmd --permanent --zone=public --add-port=6033/tcp
- [root@proxysql ~]# firewall-cmd --reload
ProxySQL所使用的端口为6032和6033
6032:用来配置ProxySQL,是个管理接口
6033:用来被远程用户连接端口
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)
5、将MySQL主从服务器信息添加入mysql_servers表中
先将主从服务器存放在同一组内,等指定好读写规则后,系统会根据配置文件中的read-only值自动将其分别添加至读组和写组。
- MySQL [(none)]> INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,'192.168.14.210',3306);
- MySQL [(none)]> INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,'192.168.14.211',3306);
- MySQL [(none)]> select * from mysql_servers;
6、在MySQL服务器的主节点(192.168.14.210)上为ProxySQL添加账号用来查看MySQL节点是主还是从
[root@master ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO 'monitor'@'192.168.14.%' IDENTIFIED BY 'centos';"
7、在Proxy上配置监控账号
- MySQL [(none)]> SET mysql-monitor_username='monitor';
- MySQL [(none)]> SET mysql-monitor_password='centos';
8、配置加载至内存,将配置保存至磁盘
- MySQL [(none)]> LOAD MYSQL VARIABLES TO RUNTIME;
- MySQL [(none)]> SAVE MYSQL VARIABLES TO DISK;
9、测试,查看连接状态
MySQL [(none)]> select * from mysql_server_connect_log;
10、设置读写分组
- MySQL [(none)]> INSERT INTO mysql_replication_hostgroups VALUES(10,20,"test");
- MySQL [(none)]> SELECT * FROM mysql_replication_hostgroups;
11、让读写表生效
MySQL [(none)]> LOAD MYSQL SERVERS TO RUNTIME;
12、查看mysql_server表此时已经将服务器分组
MySQL [(none)]> SELECT * FROM mysql_servers;
13、保存配置至磁盘
MySQL [(none)]> SAVE MYSQL SERVERS TO DISK;
至此读写分离配置完毕,接下来需要定义读写分离的规则
1、在主节点(192.168.14.210)上创建一个账户让客户端连接调度器去访问主从服务器(此处授予的权限较大,实际生产中可以根据需要定义指定的那张表)
[root@master ~]# mysql -e "GRANT ALL ON *.* TO 'sqluser'@'192.168.14.%' IDENTIFIED BY 'centos';"
2、在ProxySQL服务器上,将sqluser用户添加至mysql_users表中
MySQL [(none)]> INSERT INTO mysql_users(username,password,default_hostgroup) VALUES ('sqluser','centos',10);
3、查看mysql_user表信息
MySQL [(none)]> SELECT * FROM mysql_users;
4、生效存盘
- MySQL [(none)]> load mysql users to runtime;
- MySQL [(none)]> SAVE MYSQL USERS TO DISK;
5、Client测试
[root@client ~]# mysql -usqluser -pcentos -h192.168.14.213 -P6033 -e "SELECT @@server_id;"
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);
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;
- MySQL [(none)]> SAVE MYSQL QUERY RULES TO DISK;
1、客户端连接proxysql,写入数据;主从节点实现复制
mysql -usqluser -h 192.168.14.213 -P 6033 -p
2、客户端查询(客户端是proxysql读取slave的数据,单独在slave创建新的数据,然后对比master和client节点)
到此,读写分离成功完成
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。