我们先配置好mysql的主从复制,可以查看《mysql主从复制 简单实践》
mysql读写分离
测试的三台服务器都是centos7.6
mysql-proxy:192.168.73.128
主服务器: 192.168.73.130
从服务器: 192.168.73.131
mysql-proxy配置
只需要配置mysql-proxy
服务器就可以了
1、下载 mysql-proxy
根据自己系统配置选择适合的版本,下载地址:https://downloads.mysql.com/archives/proxy/
wget https://cdn.mysql.com/archives/mysql-proxy/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
2、配置mysql-proxy,创建主配置文件
tar zxvf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
mv mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit /usr/local/mysql-proxy
cd /usr/local/mysql-proxy
mkdir lua
cp share/doc/mysql-proxy/rw-splitting.lua ./lua #复制读写分离配置文件
cp share/doc/mysql-proxy/admin-sql.lua ./lua #复制管理脚本
vi /etc/mysql-proxy.cnf #创建配置文件
# 注意这在写的时候把注释取了 不然会报错的
[mysql-proxy]
user=root #运行mysql-proxy用户
admin-username=root #主从mysql共有的用户
admin-password=123456 #用户的密码
proxy-address=192.168.73.128 #mysql-proxy运行ip和端口,不加端口,默认4040
proxy-read-only-backend-addresses=192.168.73.131 #指定后端从slave读取数据
proxy-backend-addresses=192.168.73.130 #指定后端主master写入数据
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 #定义log日志级别,由高到低分别有(error|warning|info|message|debug)
daemon=true #以守护进程方式运行
keepalive=true #mysql-proxy崩溃时,尝试重启
保存退出!
chmod 660 /etc/mysql-porxy.cnf
3、修改读写分离配置文件
vi /usr/local/mysql-proxy/lua/rw-splitting.lua
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = 1, #默认超过4个连接数时,才开始读写分离,改为1
max_idle_connections = 1, #默认8,改为1
is_debug = false
}
end
4、启动mysql-proxy
/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf
看下面报错了
[root@localhost www]# /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf
2019-02-20 14:51:30: (critical) Key file contains key 'daemon' which has value that cannot be interpreted.
2019-02-20 14:51:30: (message) Initiating shutdown, requested from mysql-proxy-cli.c:367
2019-02-20 14:51:30: (message) shutting down normally, exit code is: 1
在mysql论坛找到了解决办法,/etc/mysql-proxy.cnf
可能有非ASCII字符,删掉所有注释,启动成功了。
[mysql-proxy]
user=root
admin-username=root
admin-password=123456
proxy-address=192.168.73.128
proxy-read-only-backend-addresses=192.168.73.131
proxy-backend-addresses=192.168.73.130
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
查看启动情况,关闭可以使用killall -9 mysql-proxy
[root@localhost www]# netstat -tupln | grep 4000
tcp 0 0 192.168.73.128:4000 0.0.0.0:* LISTEN 8061/mysql-proxy
5、测试读写分离
使用客户端连接mysql-proxy
mysql -u root -h 192.168.73.128 -P 4000 -p123456
我们在mysql-proxy
中插入了两条数据,并且可以查看数据。
mysql> use codehui;
Database changed
mysql> create table proxy (id INT(11),name VARCHAR(255));
Query OK, 0 rows affected (0.06 sec)
mysql> insert into proxy values(001,'beijing');
Query OK, 1 row affected (0.02 sec)
mysql> insert into proxy values(002,'shanghai');
Query OK, 1 row affected (0.00 sec)
mysql> select * from proxy;
+------+----------+
| id | name |
+------+----------+
| 1 | beijing |
| 2 | shanghai |
+------+----------+
2 rows in set (0.00 sec)
要怎么知道启用了读写分离呢?我们进入从服务器192.168.73.131
,修改id为2的数据name值为xian。
mysql -u root -h 192.168.73.131 -p123456
mysql> use codehui;
Database changed
mysql> update proxy set name='xian' where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from proxy;
+------+---------+
| id | name |
+------+---------+
| 1 | beijing |
| 2 | xian |
+------+---------+
2 rows in set (0.00 sec)
然后登陆mysql-proxy,查看到的数据是从服务器192.168.73.131
的,说明读写分离成功。
mysql -u root -h 192.168.73.128 -P 4000 -p123456
mysql> use codehui;
Database changed
mysql> select * from proxy;
+------+---------+
| id | name |
+------+---------+
| 1 | beijing |
| 2 | xian |
+------+---------+
2 rows in set (0.00 sec)