当前位置: 代码网 > it编程>数据库>Mysql > 通过nginx做mysql的负载均衡实现过程

通过nginx做mysql的负载均衡实现过程

2025年07月20日 Mysql 我要评论
使用场景一般用于dns或者已经做好主从同步的mysql服务器的转发,做负载均衡的工作。安装stream模块stream模块一般用于tcp/udp数据流的代理和负载均衡,可以通过stream模块代理转发

使用场景

一般用于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.12.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              0.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         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 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

我们在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

可以看到在轮询,只不过一台机器是不能登录而已。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com