Nginx实现MySQL负载均衡(提升网站性能大法) - 汇站网

Nginx实现MySQL负载均衡(提升网站性能大法)

2024-10-22 0 294

正文:

Nginx的 stream 模块是处理 TCP/UDP 数据流代理和负载均衡的强大工具,能够高效地转发 TCP 消息。自 Nginx 1.9.0 版本起,ngx_stream_core_module 模块成为标准配置。不过,默认情况下,该模块并未被编译进 Nginx 中。

Nginx实现MySQL负载均衡(提升网站性能大法)
安装 Nginx 并启用 stream 模块,有两种主流方法:

方法一:在编译安装 Nginx 时,必须添加-with-stream 配置参数来启用 stream 模块。对于新手来说,这一步骤可能稍显复杂。

方法二:通过包管理器安装
1. 首先,使用 yum -y install epel-release 命令安装 EPEL 仓库,为安装 Nginx 做准备。
2. 然后,执行 yum -y install nginx 命令,安装包含 stream 模块的 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 需要配置一个 stream 的段(segment),就是刚才安装的这个模块的自带的指令(directive),注意这个 stream 和 http 段是并列的,要写在/etc/nginx/nginx.conf 的最后。

/*网站名称:汇站网*/
/*原文链接:https://www.huizhanii.com/?p=38720*/

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;
}

要重新启动 Nginx 服务,您可以在命令行中输入以下命令:

systemctl reload nginx

或者,您也可以使用以下命令来启动 Nginx 服务:

systemctl restart nginx

如果在启动过程中遇到任何问题,您可以使用以下命令来检查 Nginx 的配置文件是否正确:

nginx -t

如果配置文件存在错误,命令将返回相应的错误提示。

完成服务器重启后,您可以使用以下命令来查看 Nginx 服务的状态,以确保其正常运行:

[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 模块,二台服务器轮询

/*网站名称:汇站网*/
/*原文链接:https://www.huizhanii.com/?p=38720*/

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 实现 MySQL 负载均衡(提升网站性能大法)

收藏 (0)

微信扫一扫

支付宝扫一扫

点赞 (0)

免责声明

本资源仅用于个人学习和研究使用,禁止用于任何商业环境!

 1.  本网站名称:汇站网
 2.  本站永久网址:https://www.huizhanii.com/
 3.  本站所有资源来源于网友投稿和高价购买,所有资源仅对编程人员及源代码爱好者开放下载做参考和研究及学习,本站不提供任何技术服务!
 4.  本站所有资源的展示图片和信息不代表本站的立场!本站只是储蓄平台及搬运
 5.  下载者禁止在服务器和虚拟机下进行搭建运营,本站所有资源不支持联网运行!只允许调试,参考和研究!!!!
 6.  未经原版权作者许可,禁止用于任何商业环境,任何人不得擅作它用,下载者不得用于违反国家法律,否则发生的一切法律后果自行承担!
 7.  为尊重作者版权,请在下载24小时内删除!请购买原版授权作品,支持你喜欢的作者,谢谢!
 8.  若资源侵犯了您的合法权益, 请持您的版权证书和相关原作品信息来信通知我们请来信     通知我们 我们会及时删除,给您带来的不便,我们深表歉意!
 9.  如下载链接失效、广告或者压缩包问题请联系站长处理!
 10.  如果你也有好源码或者教程,可以发布到网站,分享有金币奖励和额外收入!
 11.  本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
 12.  因源码具有可复制性,一经赞助 ,不得以任何形式退款。
 13.  更多详情请点击查看

汇站网 技术安全 Nginx实现MySQL负载均衡(提升网站性能大法) https://www.huizhanii.com/38724.html

汇站

站长资源下载中心-找源码上汇站

下一篇:

已经没有下一篇了!

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

发表评论
暂无评论
  随机评论 表情开关按钮图片
表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情 表情表情表情表情表情表情表情 表情表情表情表情表情表情表情表情
登录后评论
联系官方客服

为您解决烦忧 - 24小时在线 专业服务

(汇站网)一个专注站长资源的平台网站,提供最新的网站模板和整站源码,内容包含各类精品网页模板,企业网站模板,网站模板,DIV+CSS模板,织梦模板,帝国cms模板,discuz模板,wordpress模板,个人博客论坛模板,上千种免费网页模板下载尽在汇站网.找源码上汇站.huizhanii.com