通過(guò)nginx做mysql的負(fù)載均衡實(shí)現(xiàn)過(guò)程
使用場(chǎng)景
一般用于dns或者已經(jīng)做好主從同步的mysql服務(wù)器的轉(zhuǎn)發(fā),做負(fù)載均衡的工作。
安裝stream模塊
stream模塊一般用于tcp/UDP數(shù)據(jù)流的代理和負(fù)載均衡,可以通過(guò)stream模塊代理轉(zhuǎn)發(fā)TCP消息。 ngx_stream_core_module模塊由1.9.0版提供。 默認(rèn)情況下,沒(méi)有構(gòu)建此模塊。
第一種安裝方法:
必須使用-with stream配置參數(shù)啟用。這個(gè)對(duì)于初學(xué)者來(lái)講稍微有些麻煩
第二種安裝方法:
首先使用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模塊,進(jìn)行安裝
yum -y install nginx-mod-stream.x86_64
安裝完畢。
配置nginx的轉(zhuǎn)發(fā)模塊
nginx需要配置一個(gè)stream的段(segment),就是剛才安裝的這個(gè)模塊的自帶的指令(directive),注意這個(gè)stream和http段是并列的,要寫(xiě)在/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服務(wù)器的地址192.168.10.240 作為要轉(zhuǎn)發(fā)的機(jī)器,配置如下:
root@hy conf.d]# cat mysql.stream
server{
listen 3306;
proxy_pass 192.168.10.240:3306;
}使用 systemctl reload nginx 重新啟動(dòng)nginx
也可以使用systemctl restart nginx啟動(dòng)
如果啟動(dòng)不了,可以使用nginx -t測(cè)試下配置文件是否有錯(cuò),如果有錯(cuò)誤的話,會(huì)有提示。
重啟服務(wù)器以后,看狀態(tài)
[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已經(jīng)偵聽(tīng)了3306端口。這個(gè)就是7層轉(zhuǎn)發(fā)的特點(diǎn)。
轉(zhuǎn)發(fā)測(cè)試
我們先到240上看看機(jī)器名稱(chēng)
[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)
機(jī)器名稱(chēng)是slave1
我們?cè)趎ginx機(jī)器上登錄,nginx的機(jī)器名稱(chēng)是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)
我們看到已經(jīng)成功到slave1上去了。
配置負(fù)載均衡
配置負(fù)載均衡,對(duì)于mysql來(lái)說(shuō),所有的被轉(zhuǎn)發(fā)的服務(wù)器必須先已經(jīng)做好了同步才可,否則數(shù)據(jù)有不對(duì)。
配置很簡(jiǎn)單,就是增加upstream模塊,二臺(tái)服務(wù)器輪詢(xún)
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;
}重新啟動(dòng)
[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
可以看到在輪詢(xún),只不過(guò)一臺(tái)機(jī)器是不能登錄而已。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Nginx中多種負(fù)載均衡策略配置的實(shí)戰(zhàn)指南
- Nginx負(fù)載均衡通用方案詳解
- Nginx負(fù)載均衡與健康檢查使用詳解
- 詳解Nginx中常見(jiàn)負(fù)載均衡策略配置與使用場(chǎng)景
- Nginx 負(fù)載均衡和緩存配置最佳實(shí)踐
- Nginx+Tomcat負(fù)載均衡群集全過(guò)程
- Nginx部署負(fù)載均衡服務(wù)的步驟全解析
- nginx負(fù)載均衡配置方式
- nginx負(fù)載均衡及詳細(xì)配置方法
- nginx實(shí)現(xiàn)負(fù)載均衡與實(shí)例解讀
- Nginx幾種負(fù)載均衡模式的實(shí)現(xiàn)示例
相關(guān)文章
Nginx中指令server_name的詳細(xì)使用指南
對(duì)于Web開(kāi)發(fā)者來(lái)說(shuō),Nginx是一個(gè)強(qiáng)大且靈活的Web服務(wù)器和反向代理服務(wù)器,下面這篇文章主要介紹了Nginx中指令server_name詳細(xì)使用的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-07-07
Nginx?反向代理緩存(proxy_cache)的實(shí)現(xiàn)
Nginx的緩存加速功能是由proxy_cache和fastcgi_cache兩個(gè)功能模塊完成,本文主要介紹了Nginx?反向代理緩存(proxy_cache)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
利用Nginx_geo模塊實(shí)現(xiàn)CDN調(diào)度的配置方法
今天小編就為大家分享一篇利用Nginx_geo模塊實(shí)現(xiàn)CDN調(diào)度的配置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
nginx通過(guò)四層代理實(shí)現(xiàn)端口轉(zhuǎn)發(fā)的示例代碼
本文主要介紹了nginx通過(guò)四層代理實(shí)現(xiàn)端口轉(zhuǎn)發(fā),將原IP的3306端口請(qǐng)求轉(zhuǎn)至新IP的3306端口,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-07-07
centos環(huán)境下nginx高可用集群的搭建指南
為了防止Nginx單點(diǎn)故障造成服務(wù)器癱瘓,本文介紹了Nginx實(shí)現(xiàn)高可用集群構(gòu)建,下面這篇文章主要給大家介紹了關(guān)于centos環(huán)境下nginx高可用集群的搭建指南,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07

