在了解《MySQL主从复制原理》和《MySQL主从同步配置》之后就要考虑的是,既然我MySQL的架构是一主两从,那么该如何做到负载均衡,是写操作在Master上执行,读操作在Slave上操作。
--------
这种负载均衡的解决方案有:
MySQL-Proxy 这是MySQL原生的解决方案,但配置复杂。
Atlas Proxy
-------
Atlas官方链接:https://github.com/Qihoo360/Atlas/blob/master/README_ZH.md
Atlas下载链接: https://github.com/Qihoo360/Atlas/releases
我选择的版本为:Atlas-2.2.1.el6.x86_64.rpm 我将它传到了百度云,这里。
---------
部署规划
这个规划是基于《MySQL主从同步配置》
name | ip | 端口 | 说明 |
Master | 192.168.160.130 | 3306 | 主节点 |
Slave1 | 192.168.160.131 | 3306 | 从节点 |
Proxy | 192.168.160.130 | 1234 | 代理节点 |
---------
安装与卸载Atlas
我选择的版本为:
将Atlas-2.2.1.el6.x86_64.rpm传到192.168.160.130。
帮助:
#安装 shell> rpm -i Atlas-2.2.1.el6.x86_64.rpm #卸载 shell> rpm -e Atlas-2.2.1.el6.x86_64.rpm |
如下,我的操作
[root@localhost software]# ll
total 878356
-rw-r--r--. 1 root root 4963681 Jun 26 00:51 Atlas-2.2.1.el6.x86_64.rpm
drwxrwxrwx. 9 root root 4096 May 8 08:05 elasticsearch-6.2.2
-rw-r--r--. 1 root root 29049540 May 7 01:01 elasticsearch-6.2.2.tar.gz
drwxr-xr-x. 8 uucp 143 4096 Jul 21 2017 jdk8
drwxrwxrwx. 12 1000 1000 4096 Feb 16 11:20 kibana-6.2.2-linux-x86_64
-rw-r--r--. 1 root root 83415765 May 8 00:24 kibana-6.2.2-linux-x86_64.tar.gz
drwxrwxrwx. 12 root root 4096 May 8 20:20 logstash-6.2.2
-rw-r--r--. 1 root root 139464029 May 8 00:52 logstash-6.2.2.tar.gz
drwxr-xr-x. 10 mysql mysql 4096 Jun 25 19:57 mysql
-rw-r--r--. 1 root root 641127384 Oct 31 2017 mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
drwxrwxr-x. 6 root root 4096 Dec 18 2015 redis-3.0.6
-rw-r--r--. 1 root root 1372648 May 25 2017 redis-3.0.6.tar.gz
[root@localhost software]# pwd
/usr/local/software
[root@localhost software]# rpm -i Atlas-2.2.1.el6.x86_64.rpm
[root@localhost software]#
此时Atlas被安装到了/usr/local/mysql-proxy当中。
PS、这是我不喜欢rmp安装的原因,不能自定义。
配置Atlas
Atlas的配置有中文注释,非常方便。
只需要主要下面几个吧。而我只修改了红色部分。
|
#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
proxy-read-only-backend-addresses = 192.168.160.131:3306@1
#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密。用户密码所有主库和从库都必须一至
pwds = root:DAJnl8cVzy8=
#Atlas监听的工作接口IP和端口
proxy-address = 0.0.0.0:1234
[root@localhost bin]# ./encrypt root DAJnl8cVzy8=
启动Atlas
./mysql-proxyd test start ./mysql-proxyd test stop |
测试
[root@localhost bin]# pwd
/usr/local/software/mysql/bin
[root@localhost bin]# ./mysql -h127.0.0.1 -P1234 -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.81-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| rorodb |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql>
为了测试读写分离我需要打开配置
#SQL日志的开关,可设置为OFF、ON、REALTIME,OFF代表不记录SQL日志,ON代表记录SQL日志,REALTIME代表记录SQL日志且实时写入磁盘,默认为OFF sql-log = REALTIME |
这样在执行插入查询的时候就能在sql_test.log里清晰的看到被sql被分配到master还是slave从而实现了读写分离。
参考资料
https://github.com/Qihoo360/Atlas/blob/master/README_ZH.md