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

 更新时间:2025年07月19日 15:42:02   作者:老骥又出发  
Nginx stream模块用于DNS和主从MySQL的TCP/UDP代理与负载均衡,安装需编译启用或通过yum安装,配置stream块及upstream实现转发和轮询,测试后验证端口监听状态

使用场景

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

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 详解nginx反向代理配置及优化

    详解nginx反向代理配置及优化

    本篇文章主要介绍了详解nginx反向代理配置及优化,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • Nginx四层负载均衡的实现示例

    Nginx四层负载均衡的实现示例

    Nginx 不支持传统的四层负载均衡,但可以通过stream模块配合TCP实现类似的功能,本文主要介绍了Nginx四层负载均衡的实现示例,具有一定的参考价值,感兴趣的可以了解一下
    2024-04-04
  • Nginx配置移动端和PC端自动跳转方式

    Nginx配置移动端和PC端自动跳转方式

    文章介绍了如何通过Nginx配置PC端和移动端自动跳转,PC端和移动端各有独立的域名,PC端访问任何域名时会跳转到www.yxf.com,移动端访问任何域名时会跳转到m.yxf.com,配置时需要修改Nginx的conf文件,使用302或301重定向
    2025-11-11
  • nginx设置资源缓存实战详解

    nginx设置资源缓存实战详解

    这篇文章主要介绍了nginx设置资源缓存实战详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • 浅谈Nginx七层反向代理和负载均衡

    浅谈Nginx七层反向代理和负载均衡

    这篇文章主要介绍了浅谈Nginx七层反向代理和负载均衡,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • 一次说清Nginx反向代理及参数配置

    一次说清Nginx反向代理及参数配置

    我们在配置服务时常常会用到Nginx来设置反向代理,虽然常用,但是我们真的了解各个参数的意思吗,本文就给大家一次说清Nginx反向代理及参数配置,需要的朋友可以参考下
    2023-05-05
  • 一文读懂Ingress-Nginx的实践

    一文读懂Ingress-Nginx的实践

    Ingress-Nginx是Kubernetes中管理HTTP和HTTPS流量的重要工具,本文深入探讨Ingress-Nginx工作原理、配置及最佳实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-11-11
  • Nginx上传文件出现“ 413 (499 502 404) Request Entity Too Large错误解决

    Nginx上传文件出现“ 413 (499 502 404) Requ

    HTTP 413 Request Entity Too Large错误常常出现在客户端发送的请求体超过服务器允许的大小限制时,本文主要介绍了Nginx上传文件出现“ 413 (499 502 404) Request Entity Too Large错误解决,感兴趣的可以了解一下
    2024-07-07
  • 使用nginx正向代理实现访问外网

    使用nginx正向代理实现访问外网

    这篇文章主要介绍了使用nginx正向代理实现让内网主机通过外网主机访问外网,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-12-12
  • Linux下用Nginx作Perl程序服务器及其中Perl模块的配置

    Linux下用Nginx作Perl程序服务器及其中Perl模块的配置

    这篇文章主要介绍了Linux下用Nginx作Perl程序服务器及其中Perl模块的配置,文中使用到了FastCGI中间件进行连接,需要的朋友可以参考下
    2016-02-02

最新评论