通過nginx做mysql的負載均衡實現(xiàn)過程
使用場景
一般用于dns或者已經(jīng)做好主從同步的mysql服務器的轉(zhuǎn)發(fā),做負載均衡的工作。
安裝stream模塊
stream模塊一般用于tcp/UDP數(shù)據(jù)流的代理和負載均衡,可以通過stream模塊代理轉(zhuǎn)發(fā)TCP消息。 ngx_stream_core_module模塊由1.9.0版提供。 默認情況下,沒有構(gòu)建此模塊。
第一種安裝方法:
必須使用-with stream配置參數(shù)啟用。這個對于初學者來講稍微有些麻煩
第二種安裝方法:
首先使用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的轉(zhuǎn)發(fā)模塊
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 作為要轉(zhuǎn)發(fā)的機器,配置如下:
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測試下配置文件是否有錯,如果有錯誤的話,會有提示。
重啟服務器以后,看狀態(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端口。這個就是7層轉(zhuǎn)發(fā)的特點。
轉(zhuǎn)發(fā)測試
我們先到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)
我們看到已經(jīng)成功到slave1上去了。
配置負載均衡
配置負載均衡,對于mysql來說,所有的被轉(zhuǎn)發(fā)的服務器必須先已經(jīng)做好了同步才可,否則數(shù)據(jù)有不對。
配置很簡單,就是增加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
可以看到在輪詢,只不過一臺機器是不能登錄而已。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx服務器下使用rewrite重寫url以實現(xiàn)偽靜態(tài)的示例
這篇文章主要介紹了Nginx服務器下使用rewrite重寫url以實現(xiàn)偽靜態(tài)的示例,這里舉了Discuz!和WordPress這兩個常用的PHP程序,需要的朋友可以參考下2015-12-12
nginx 配置靜態(tài)緩存及靜態(tài)緩存文件沒有生成的問題及解決方案
這篇文章主要介紹了nginx 配置靜態(tài)緩存及靜態(tài)緩存文件沒有生成的問題及解決方案,本文分步驟結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-11-11
nginx代理參數(shù)proxy_pass的實現(xiàn)
proxy_pass參數(shù)用于配置反向代理,本文主要介紹了nginx代理參數(shù)proxy_pass的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-04-04
深入解析nginx路由location匹配規(guī)則及其優(yōu)先級
Nginx是一款高性能的Web服務器和反向代理服務器,它的路由功能是通過location指令來實現(xiàn)的,location指令用于匹配請求的URL,并將請求轉(zhuǎn)發(fā)到相應的處理程序或靜態(tài)文件,需要的朋友可以參考下2023-10-10
解讀nginx反向代理location和proxy_pass的映射關(guān)系
這篇文章主要介紹了解讀nginx反向代理location和proxy_pass的映射關(guān)系,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

