我们先配置好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/

  1. wget https://cdn.mysql.com/archives/mysql-proxy/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
2、配置mysql-proxy,创建主配置文件
  1. tar zxvf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
  2. mv mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit /usr/local/mysql-proxy
  3. cd /usr/local/mysql-proxy
  4. mkdir lua
  5. cp share/doc/mysql-proxy/rw-splitting.lua ./lua #复制读写分离配置文件
  6. cp share/doc/mysql-proxy/admin-sql.lua ./lua #复制管理脚本
  7. vi /etc/mysql-proxy.cnf #创建配置文件
  8. # 注意这在写的时候把注释取了 不然会报错的
  9. [mysql-proxy]
  10. user=root #运行mysql-proxy用户
  11. admin-username=root #主从mysql共有的用户
  12. admin-password=123456 #用户的密码
  13. proxy-address=192.168.73.128 #mysql-proxy运行ip和端口,不加端口,默认4040
  14. proxy-read-only-backend-addresses=192.168.73.131 #指定后端从slave读取数据
  15. proxy-backend-addresses=192.168.73.130 #指定后端主master写入数据
  16. proxy-lua-script=/usr/local/mysql-proxy/lua/rw-splitting.lua #指定读写分离配置文件位置
  17. admin-lua-script=/usr/local/mysql-proxy/lua/admin-sql.lua #指定管理脚本
  18. log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log #日志位置
  19. log-level=info #定义log日志级别,由高到低分别有(error|warning|info|message|debug)
  20. daemon=true #以守护进程方式运行
  21. keepalive=true #mysql-proxy崩溃时,尝试重启
  22. 保存退出!
  23. chmod 660 /etc/mysql-porxy.cnf
3、修改读写分离配置文件
  1. vi /usr/local/mysql-proxy/lua/rw-splitting.lua
  2. if not proxy.global.config.rwsplit then
  3. proxy.global.config.rwsplit = {
  4. min_idle_connections = 1, #默认超过4个连接数时,才开始读写分离,改为1
  5. max_idle_connections = 1, #默认8,改为1
  6. is_debug = false
  7. }
  8. end
4、启动mysql-proxy
  1. /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf

看下面报错了

  1. [root@localhost www]# /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf
  2. 2019-02-20 14:51:30: (critical) Key file contains key 'daemon' which has value that cannot be interpreted.
  3. 2019-02-20 14:51:30: (message) Initiating shutdown, requested from mysql-proxy-cli.c:367
  4. 2019-02-20 14:51:30: (message) shutting down normally, exit code is: 1

在mysql论坛找到了解决办法,/etc/mysql-proxy.cnf可能有非ASCII字符,删掉所有注释,启动成功了。twemoji-1f602

  1. [mysql-proxy]
  2. user=root
  3. admin-username=root
  4. admin-password=123456
  5. proxy-address=192.168.73.128
  6. proxy-read-only-backend-addresses=192.168.73.131
  7. proxy-backend-addresses=192.168.73.130
  8. proxy-lua-script=/usr/local/mysql-proxy/lua/rw-splitting.lua
  9. admin-lua-script=/usr/local/mysql-proxy/lua/admin-sql.lua
  10. log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log
  11. log-level=info
  12. daemon=true
  13. keepalive=true

查看启动情况,关闭可以使用killall -9 mysql-proxy

  1. [root@localhost www]# netstat -tupln | grep 4000
  2. tcp 0 0 192.168.73.128:4000 0.0.0.0:* LISTEN 8061/mysql-proxy
5、测试读写分离

使用客户端连接mysql-proxy

  1. mysql -u root -h 192.168.73.128 -P 4000 -p123456

我们在mysql-proxy中插入了两条数据,并且可以查看数据。

  1. mysql> use codehui;
  2. Database changed
  3. mysql> create table proxy (id INT(11),name VARCHAR(255));
  4. Query OK, 0 rows affected (0.06 sec)
  5. mysql> insert into proxy values(001,'beijing');
  6. Query OK, 1 row affected (0.02 sec)
  7. mysql> insert into proxy values(002,'shanghai');
  8. Query OK, 1 row affected (0.00 sec)
  9. mysql> select * from proxy;
  10. +------+----------+
  11. | id | name |
  12. +------+----------+
  13. | 1 | beijing |
  14. | 2 | shanghai |
  15. +------+----------+
  16. 2 rows in set (0.00 sec)

要怎么知道启用了读写分离呢?我们进入从服务器192.168.73.131,修改id为2的数据name值为xian。

  1. mysql -u root -h 192.168.73.131 -p123456
  2. mysql> use codehui;
  3. Database changed
  4. mysql> update proxy set name='xian' where id=2;
  5. Query OK, 1 row affected (0.00 sec)
  6. Rows matched: 1 Changed: 1 Warnings: 0
  7. mysql> select * from proxy;
  8. +------+---------+
  9. | id | name |
  10. +------+---------+
  11. | 1 | beijing |
  12. | 2 | xian |
  13. +------+---------+
  14. 2 rows in set (0.00 sec)

然后登陆mysql-proxy,查看到的数据是从服务器192.168.73.131的,说明读写分离成功。

  1. mysql -u root -h 192.168.73.128 -P 4000 -p123456
  2. mysql> use codehui;
  3. Database changed
  4. mysql> select * from proxy;
  5. +------+---------+
  6. | id | name |
  7. +------+---------+
  8. | 1 | beijing |
  9. | 2 | xian |
  10. +------+---------+
  11. 2 rows in set (0.00 sec)