通过nginx做mysql的负载均衡实现过程
目录
- 使用场景
- 安装stream模块
- 配置nginx的转发模块
- 转发测试
- 配置负载均衡
- 总结
使用场景
一般用于dns或者已经做好主从同步的mysql服务器的转发,做负载均衡的工作。
安装stream模块
stream模块一般用于tcp/UDP数据流的代理和负载均衡,可以通过stream模块代理转发TCP消息。 ngx_stream_core_module模块由1.9.0版提供。 默认情况下,没有构建此模块。
第一种安装方法:
必须使用-with stream配置参数启用。这个对于初学者来讲稍微有些麻烦
第二种安装方法:
首先使用yum -y install epel-release nginx 安装 nginx模块
然后查找nginx的模块,如下图
[root@hy ~]# yum list |grep nginx nginx.x86_64 1:1.20.1-10.el7 @epel nginx-filesystem.noarch 1:1.20.1-10.el7 @epel nginx-mod-stream.x86_64 1:1.20.1-10.el7 @epel collectd-nginx.x86_64 5.8.1-1.el7 epel munin-nginx.noarch 2.0.69-5.el7 epel nginx-all-modules.noarch 1:1.20.1-10.el7 epel nginx-mod-devel.x86_64 1:1.20.1-10.el7 epel nginx-mod-http-image-filter.x86_64 1:1.20.1-10.el7 epel nginx-mod-http-perl.x86_64 1:1.20.1-10.el7 epel nginx-mod-http-XSLT-filter.x86_64 1:1.20.1-10.el7 epel nginx-mod-mail.x86_64 1:1.20.1-10.el7 epel nginx1w.x86_64 1.12.1-1.w7 webtatic nginx1w-module-headers-more.x86_64 1.1编程2.1-1.w7 webtatic nginx1w-module-http-geoip.x86_64 1.12.1-1.w7 webtatic nginx1w-module-http-image-filter.x86_64 1.12.1-1.w7 webtatic nginx1w-module-http-perl.x86_64 1.12.1-1.w7 webtatic nginx1w-module-http-xslt.x86_64 1.12.1-1.w7 webtatic nginx1w-module-mail.x86_64 1.12.1-1.w7 webtatic nginx1w-module-pagespeed.x86_64 1.12.1-1.w7 webtatic nginx1w-module-stream.x86_64 1.12.1-1.w7 webtatic pagure-web-nginx.noarch 5.13.3-2.el7 epel pcp-pmda-nginx.x86_64 4.3.2-13.el7_9 updates python2-certbot-nginx.noarch 1.11.0-1.el7 epel sympa-nginx.x86_64 6.2.68-1.el7 epel
找到stream模块,进行安装
yum -y install nginx-mod-stream.x86_64
安装完毕。
配置nginx的转发模块
nginx需要配置一个stream的段(segment),就是刚才安装的这个模块的自带的指令(directive),注意这个stream和http段是并列的,要写在/etc/nginx/nginx.conf的最后。
stream { log_format basic '$remote_addr [$time_local] ' '$protocol $status $bytes_sent $bytes_received 编程客栈' '$session_time'; Access_log /var/log/nginx/stream-access.log basic buffer=32k; include /etc/nginx/conf.d/*.stream; }
最后的include是要是将conf.d中的stream文件作为配置的一部分。
我们将有mysql服务器的地址192.168.10.240 作为要转发的机器,配置如下:
root@hy conf.d]# cat mysql.stream server{ listen 3306; proxy_pass 192.168.10.240:3306; }
使用 systemctl reload nginx 重新启动nginx
也可以使用systemctl restart nginx启动
如果启动不了,可以使用nginx -t测试下配置文件是否有错,如果有错误的话,会有提示。
重启服务器以后,看状态
[root@hy conf.d]# systemctl restart nginx [root@hy conf.d]# netstat -ant Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 www.devze.com0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 192.168.11.9:22 http://www.devze.com 192.168.10.16:48540 ESTABLISHED tcp 0 0 192.168.11.9:22 172.16.10.20:55362 ESTABLISHED tcp6 0 0 :::80 :::* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:25
看到nginx已经侦听了3306端口。这个就是7层转发的特点。
转发测试
我们先到240上看看机器名称
[root@hy conf.d]# mysql -uroot -p123456 -h192.168.10.240 Welcome to the mariadb monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server http://www.devze.comversion: 5.7.40 MySQL Community Server (GPL) Copyright (c) 2000, 2018, oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> select @@hostname; +------------+ | @@hostname | +------------+ | slave1 | +------------+ 1 row in set (0.00 sec)
机器名称是slave1
我们在nginx机器上登录,nginx的机器名称是hy
[root@hy nginx]# mysql -uroot -p123456 -h127.0.0.1 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 13 Server version: 5.7.40 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> select @@hostname; +------------+ | @@hostname | +------------+ | slave1 | +------------+ 1 row in set (0.00 sec)
我们看到已经成功到slave1上去了。
配置负载均衡
配置负载均衡,对于mysql来说,所有的被转发的服务器必须先已经做好了同步才可,否则数据有不对。
配置很简单,就是增加upstream模块,二台服务器轮询
upstream backend { # hash $remote_addr consistent; server 192.168.10.240:3306 weight=1; server 192.168.10.251:3306 weight=1 max_fails=3 fail_timeout=30s; } server{ listen 3306; proxy_pass backend; }
重新启动
[root@hy conf.d]# mysql -uroot -p123456 -h127.0.0.1 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 19 Server version: 5.7.40 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> exot exit; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'exot exit' at line 1 MySQL [(none)]> exit Bye [root@hy conf.d]# mysql -uroot -p123456 -h127.0.0.1 ERROR 1130 (HY000): Host 'php.qq.com' is not allowed to connect to this MySQL server
可以看到在轮询,只不过一台机器是不能登录而已。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
精彩评论