通过 主从复制(Master-Slave)的方式来同步数据,再通过读写分离(MySQL-Proxy)来提升数据库的并发负载能力 。本文介绍mysql-proxy读写分离的实践。
首先配置主从同步,主从同步可以查看另一篇文章,主从配置:https://blog.csdn.net/jon_stark/article/details/89154976
环境分配:都是centos7.+虚拟机
mysql-proxy:10.88.62.8
mysql-master:10.88.62.100
mysql-slave:10.88.62.110
1、下载mysql-proxy
下载指定版本,下载地址:https://downloads.mysql.com/archives/proxy/
2、配置mysql-proxy
[root@mysql-proxy ~]# tar -xf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
[root@mysql-proxy ~]# mv mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit /usr/local/mysql-proxy
[root@mysql-proxy ~]# cd /usr/local/mysql-proxy/
[root@mysql-proxy mysql-proxy]# mkdir lua logs
#复制读写分离配置文件
[root@mysql-proxy mysql-proxy]# cp /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua /usr/local/mysql-proxy/lua/
#复制管理脚本
[root@mysql-proxy mysql-proxy]# cp /usr/local/mysql-proxy/share/doc/mysql-proxy/admin-sql.lua /usr/local/mysql-proxy/lua/
3、创建主配置文件
[root@mysql-proxy mysql-proxy]# vim /etc/mysql-proxy.cnf
[mysql-proxy]
user=root
admin-username=proxy
admin-password=123456
proxy-address=10.88.62.8:4000
proxy-read-only-backend-addresses=10.88.62.110
proxy-backend-addresses=10.88.62.100
proxy-lua-script=/usr/local/mysql-proxy/lua/rw-splitting.lua
admin-lua-script=/usr/local/mysql-proxy/lua/admin-sql.lua
log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log
log-level=info
daemon=true
keepalive=true
保存退出!
[root@mysql-proxy mysql-proxy]# chmod 660 /etc/mysql-proxy.cnf
user #运行mysql-proxy的用户
admin-username #主从mysql共有的用户
admin-password #主从mysql共有用户的密码
proxy-address #mysql-proxy运行ip和端口
proxy-read-only-backend-addresses #指定后端从slave读取数据
proxy-backend-addresses #指定后端主master写入数据
proxy-lua-script #指定读写分离配置文件位置
admin-lua-script #指定管理脚本
log-file #日志位置
log-level #定义log日志级别,由高到低分别有(error|warning|info|message|debug)
daemon=true #以守护进程方式运行
keepaliv=true #mysql-proxy崩溃时,尝试重启
4、修改读写分离配置文件
[root@mysql-proxy ~]# vim /usr/local/mysql-proxy/lua/rw-splitting.lua
35 --- config
36 --
37 -- connection pool
38 if not proxy.global.config.rwsplit then
39 proxy.global.config.rwsplit = {
40 min_idle_connections = 1,
41 max_idle_connections = 1,
42
43 is_debug = false
44 }
45 end
46
47 ---
min_idle_connections = 1, #默认超过4个连接数时,才开始读写分离,改为1
max_idle_connections = 1, #默认8,改为1
5、
启动mysql-proxy
[root@mysql-proxy mysql-proxy]# /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf
6、测试读写分离
连接mysql-proxy10.88.62.8
[root@mysql-proxy mysql-proxy]# mysql -uproxy -p123456 -P4000 -h10.88.62.8
MariaDB [(none)]> create database vida;
Query OK, 1 row affected (0.002 sec)
MariaDB [(none)]> use vida;
Database changed
MariaDB [vida]> create table proxy (id INT(11),name VARCHAR(255));
server default db:
client default db: vida
syncronizing
Query OK, 0 rows affected (0.015 sec)
MariaDB [vida]> insert into proxy values(01,'xixi');
Query OK, 1 row affected (0.010 sec)
MariaDB [vida]> insert into proxy values(02,'haha');
Query OK, 1 row affected (0.003 sec)
连接主库10.88.62.100
[root@mysql-master ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| vida |
+--------------------+
4 rows in set (0.000 sec)
MariaDB [(none)]> use vida;
Database changed
MariaDB [vida]> show tables;
+----------------+
| Tables_in_vida |
+----------------+
| proxy |
+----------------+
1 row in set (0.000 sec)
MariaDB [vida]> select * from proxy;
+------+------+
| id | name |
+------+------+
| 1 | xixi |
| 2 | haha |
+------+------+
2 rows in set (0.000 sec)
连接从库10.88.62.110
[root@mysql-slave ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| vida |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [(none)]> use vida;
Database changed
MariaDB [vida]> show tables;
+----------------+
| Tables_in_vida |
+----------------+
| proxy |
+----------------+
1 row in set (0.000 sec)
MariaDB [vida]> select * from proxy;
+------+------+
| id | name |
+------+------+
| 1 | xixi |
| 2 | haha |
+------+------+
2 rows in set (0.000 sec)
mysql-proxy端插入的数据库在主从端都能查到,证明插入的数据是写入到主10.88.62.100,然后从10.88.62.110进行了同步。
接下来我们修改从端的id为1的数据库值为hehe。
[root@mysql-slave ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> update vida.proxy set name='hehe' where id=1;
Query OK, 1 row affected (0.011 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [(none)]> select * from vida.proxy;
+------+------+
| id | name |
+------+------+
| 1 | hehe |
| 2 | haha |
+------+------+
2 rows in set (0.000 sec)
从主10.88.62.100查看该值
[root@mysql-master ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> select * from vida.proxy;
+------+------+
| id | name |
+------+------+
| 1 | xixi |
| 2 | haha |
+------+------+
2 rows in set (0.001 sec)
主从数据不一致了,再通过mysql-proxy10.88.62.8查看会查看到从库上的数据。
[root@mysql-proxy mysql-proxy]# mysql -uproxy -p123456 -P4000 -h10.88.62.8
MariaDB [(none)]> select * from vida.proxy;
+------+------+
| id | name |
+------+------+
| 1 | hehe |
| 2 | haha |
+------+------+
2 rows in set (0.002 sec)
再通过mysql-proxy10.88.62.8创建一个库还会去主库10.88.62.100创建,从库10.88.62.110进行同步。
[root@mysql-proxy mysql-proxy]# mysql -uproxy -p123456 -P4000 -h10.88.62.8
MariaDB [(none)]> create database vivi;
Query OK, 1 row affected (0.002 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| vida |
| vivi |
+--------------------+
5 rows in set (0.004 sec)
主库10.88.62.100查询
[root@mysql-master ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| vida |
| vivi |
+--------------------+
5 rows in set (0.001 sec)
从库10.88.62.110查询
[root@mysql-slave ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| vida |
| vivi |
+--------------------+
5 rows in set (0.001 sec)
到此可以证明通过mysql-proxy10.88.62.8写入的数据是写到主10.88.62.100,通过mysql-proxy10.88.62.8读的数据是在从10.88.62.110读取来的。
[root@mysql-proxy ~]# cat /usr/lib/systemd/system/mysql-proxy.service
[Unit]
Description=mysql-proxy
After=network.target
[Service]
ExecStart=/root/mysql-proxy.sh
ExecStop=/usr/bin/killall -9 mysql-proxy
Type=forking
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@mysql-proxy ~]# cat /root/mysql-proxy.sh
#!/bin/bash
/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf &
[root@mysql-proxy ~]# systemctl enable mysql-proxy.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mysql-proxy.service to /usr/lib/systemd/system/mysql-proxy.service.
[root@mysql-proxy ~]# systemctl stop mysql-proxy.service
[root@mysql-proxy ~]# systemctl start mysql-proxy.service