通過nginx做mysql的負(fù)載均衡實(shí)現(xiàn)過程
使用場(chǎng)景
一般用于dns或者已經(jīng)做好主從同步的mysql服務(wù)器的轉(zhuǎn)發(fā),做負(fù)載均衡的工作。
安裝stream模塊
stream模塊一般用于tcp/UDP數(shù)據(jù)流的代理和負(fù)載均衡,可以通過stream模塊代理轉(zhuǎn)發(fā)TCP消息。 ngx_stream_core_module模塊由1.9.0版提供。 默認(rèn)情況下,沒有構(gòu)建此模塊。
第一種安裝方法:
必須使用-with stream配置參數(shù)啟用。這個(gè)對(duì)于初學(xué)者來講稍微有些麻煩
第二種安裝方法:
首先使用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段是并列的,要寫在/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)偵聽了3306端口。這個(gè)就是7層轉(zhuǎn)發(fā)的特點(diǎn)。
轉(zhuǎn)發(fā)測(cè)試
我們先到240上看看機(jī)器名稱
[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ī)器名稱是slave1
我們?cè)趎ginx機(jī)器上登錄,nginx的機(jī)器名稱是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來說,所有的被轉(zhuǎn)發(fā)的服務(wù)器必須先已經(jīng)做好了同步才可,否則數(shù)據(jù)有不對(duì)。
配置很簡(jiǎn)單,就是增加upstream模塊,二臺(tái)服務(wù)器輪詢
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
可以看到在輪詢,只不過一臺(tái)機(jī)器是不能登錄而已。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Nginx中多種負(fù)載均衡策略配置的實(shí)戰(zhàn)指南
- Nginx負(fù)載均衡通用方案詳解
- Nginx負(fù)載均衡與健康檢查使用詳解
- 詳解Nginx中常見負(fù)載均衡策略配置與使用場(chǎng)景
- Nginx 負(fù)載均衡和緩存配置最佳實(shí)踐
- Nginx+Tomcat負(fù)載均衡群集全過程
- Nginx部署負(fù)載均衡服務(wù)的步驟全解析
- nginx負(fù)載均衡配置方式
- nginx負(fù)載均衡及詳細(xì)配置方法
- nginx實(shí)現(xiàn)負(fù)載均衡與實(shí)例解讀
- Nginx幾種負(fù)載均衡模式的實(shí)現(xiàn)示例
相關(guān)文章
Nginx四層負(fù)載均衡的實(shí)現(xiàn)示例
Nginx?不支持傳統(tǒng)的四層負(fù)載均衡,但可以通過stream模塊配合TCP實(shí)現(xiàn)類似的功能,本文主要介紹了Nginx四層負(fù)載均衡的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
Nginx配置移動(dòng)端和PC端自動(dòng)跳轉(zhuǎn)方式
文章介紹了如何通過Nginx配置PC端和移動(dòng)端自動(dòng)跳轉(zhuǎn),PC端和移動(dòng)端各有獨(dú)立的域名,PC端訪問任何域名時(shí)會(huì)跳轉(zhuǎn)到www.yxf.com,移動(dòng)端訪問任何域名時(shí)會(huì)跳轉(zhuǎn)到m.yxf.com,配置時(shí)需要修改Nginx的conf文件,使用302或301重定向2025-11-11
nginx設(shè)置資源緩存實(shí)戰(zhàn)詳解
這篇文章主要介紹了nginx設(shè)置資源緩存實(shí)戰(zhàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Nginx上傳文件出現(xiàn)“ 413 (499 502 404) Requ
HTTP 413 Request Entity Too Large錯(cuò)誤常常出現(xiàn)在客戶端發(fā)送的請(qǐng)求體超過服務(wù)器允許的大小限制時(shí),本文主要介紹了Nginx上傳文件出現(xiàn)“ 413 (499 502 404) Request Entity Too Large錯(cuò)誤解決,感興趣的可以了解一下2024-07-07
使用nginx正向代理實(shí)現(xiàn)訪問外網(wǎng)
這篇文章主要介紹了使用nginx正向代理實(shí)現(xiàn)讓內(nèi)網(wǎng)主機(jī)通過外網(wǎng)主機(jī)訪問外網(wǎng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12
Linux下用Nginx作Perl程序服務(wù)器及其中Perl模塊的配置
這篇文章主要介紹了Linux下用Nginx作Perl程序服務(wù)器及其中Perl模塊的配置,文中使用到了FastCGI中間件進(jìn)行連接,需要的朋友可以參考下2016-02-02

