最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何用nginx搭建MySQL負載均衡?域名解析與安裝部署詳解

 更新時間:2026年07月29日 09:29:53   作者:小黑沒那么黑  
想用nginx實現(xiàn)MySQL數(shù)據(jù)庫的負載均衡和反向代理嗎?本文手把手教你從環(huán)境準(zhǔn)備、域名解析到安裝部署nginx四層代理的全過程,包含配置實例和常見問題,讓你輕松掌握nginx四層負載均衡配置技巧

一、環(huán)境準(zhǔn)備

ip域名解析服務(wù)作用
10.11.59.38c.nginx.comnginx反向代理
10.11.59.37b.nginx.comMySQL(mariadb)后端數(shù)據(jù)庫
10.11.59.147a.nginx.comMySQL(mariadb)后端數(shù)據(jù)庫

二、部署

1、相互域名解析(可選項)

要是想域名解析的話,每一臺都需要配置

[root@c-nginx-com nginx]# vim /etc/hosts

10.11.59.38 c.nginx.com
10.11.59.147 a.nginx.com
10.11.59.37 b.nginx.com
[root@c-nginx-com nginx]# ping  -c1 a.nginx.com

PING a.nginx.com (10.11.59.147) 56(84) bytes of data.
64 bytes from a.nginx.com (10.11.59.147): icmp_seq=1 ttl=64 time=0.591 ms

--- a.nginx.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.591/0.591/0.591/0.000 ms

2、安裝數(shù)據(jù)庫

10.11.59.147、10.11.59.37安裝MySQL,如果覺得MySQL安裝太慢可以選擇mariadb,我選擇了MySQL

#!/bin/bash
# by xiaohei
# time 2020-4-28

echo "此腳本用于yum 安裝mysql"
if [[ $UID -ne 0 ]];then
	echo "使用root 執(zhí)行此腳本"
	exit 1
fi
if [[ $# != 1 ]];then
	echo "Usage script (5.5|5.6|5.7|8.0)"
	exit 123
fi
echo "清理環(huán)境"
systemctl stop mysqld mariadb &>/dev/null
yum erase -y `rpm -qa |grep mariadb` 2>/dev/null
yum erase -y `rpm -qa |grep mysql`  2>/dev/null
rm -rvf /etc/my.cnf /var/lib/mysql /var/log/mysql*
userdel -rf mysql &>/dev/null
ping -c1 -w1 www.baidu.com &>/dev/null
if [[ $? -eq 0 ]];then
	yum install -y wget yum-utils &>/dev/null
	if [[ $? -ne 0 ]];then
		echo "yum 配置錯誤"
		exit 110
	fi
	yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
	yum clean all
	yum makecache fast
else
	echo "網(wǎng)絡(luò)錯誤"
	exit 22
fi

case $1 in
5.5)
    yum-config-manager  --disable mysql80-community
    yum-config-manager  --enable mysql55-community
;;
5.6)
	yum-config-manager  --disable mysql80-community
	yum-config-manager  --enable mysql56-community
;;
5.7)
	yum-config-manager  --disable mysql80-community
	yum-config-manager  --enable mysql57-community
;;
8.0)
	:
;;
*)
	yum repolist all |grep mysql
	echo "其他版本可自行選擇下載并配置"
	exit 13
;;
esac
sleep 2
yum -y install mysql-community-server
systemctl start mysqld
echo "啟動成功,初始密碼如下(mysql5.7前版本沒有初始密碼)"
grep -o 'root@localhost.*' /var/log/mysqld.log
echo "安裝完成"

手動修改密碼

[root@b-nginx-com ~]# mysqladmin -u root -p"腳本顯示的密碼" password 'QianFeng@123'

3、創(chuàng)建數(shù)據(jù)庫和表

mysql> create database db1;
mysql> use db1
mysql> create table t1(id int,name char(50));
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1            |
+---------------+
1 row in set (0.00 sec)

mysql> into t1(id,name) values(1,'b.mysql.com');    #在10.11.59.37上面做的,為了效果需要把name數(shù)據(jù)換成二個不一樣的,其他一樣
mysql> select * from t1;
+------+-------------+
| id   | name        |
+------+-------------+
|    1 | b.mysql.com |
+------+-------------+
1 row in set (0.00 sec)


4、數(shù)據(jù)庫創(chuàng)建一個用戶

創(chuàng)建一個用戶,為了讓用戶通過代理機器能夠登錄后端數(shù)據(jù)庫服務(wù)器

mysql>grant all on db1.* to test@'%' identified by '密碼';

5、安裝部署nginx

版本最少要1.9以上,以為需要用到4層代理,我使用是1.18

[root@c-nginx-com nginx]# nginx -v
nginx version: nginx/1.18.0

配置yum源

[root@c-nginx-com ~]# vim /etc/yum.repos.d/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

安裝

[root@c-nginx-com ~]# yum -y install nginx

三、配置nginx

四層負載,四層的負載不在http模塊里面,他是和http模塊同級別的,所以需要在主配置文件去進行配置

[root@c-nginx-com ~]# vim /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}
#-----------------往下--------------------
#四層負載,四層的負載不在http模塊里面,他是和http模塊同級別的
stream {        
                upstream mysql {
                server 10.11.59.147:3306;  #后端數(shù)據(jù)庫的ip和端口,如果進行了域名解析,直接寫域名就好
                server 10.11.59.37:3306;   #同上
        }
        server {
            listen 3306;   #如果監(jiān)聽3306,遠程登錄的時候不用加-p參數(shù)
            proxy_connect_timeout 10s;
            proxy_timeout 30s;
            proxy_pass mysql;
        }
}
#------------------往上--------------------
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  0;
    #keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

啟動

[root@c-nginx-com ~]# systemctl start nginx
[root@c-nginx-com ~]# systemctl enable nginx

四、訪問

[root@c-nginx-com ~]# mysql -u test -p'QianFeng@123' -h '10.11.59.38' -e "select * from db1.t1;"
+------+-------------+
| id   | name        |
+------+-------------+
|    1 | a.mysql.com |
+------+-------------+
[root@c-nginx-com ~]# mysql -u test -p'QianFeng@123' -h '10.11.59.38' -e "select * from db1.t1;"
+------+-------------+
| id   | name        |
+------+-------------+
|    1 | b.mysql.com |
+------+-------------+
[root@c-nginx-com ~]# mysql -u test -p'QianFeng@123' -h '10.11.59.38' -e "select * from db1.t1;"
+------+-------------+
| id   | name        |
+------+-------------+
|    1 | a.mysql.com |
+------+-------------+
[root@c-nginx-com ~]# mysql -u test -p'QianFeng@123' -h '10.11.59.38' -e "select * from db1.t1;"
+------+-------------+
| id   | name        |
+------+-------------+
|    1 | b.mysql.com |
+------+-------------+


總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Nginx使用if指令實現(xiàn)多個proxy_pass方式

    Nginx使用if指令實現(xiàn)多個proxy_pass方式

    這篇文章主要介紹了Nginx使用if指令實現(xiàn)多個proxy_pass方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • WordPress與Drupal的Nginx配置rewrite重寫規(guī)則示例

    WordPress與Drupal的Nginx配置rewrite重寫規(guī)則示例

    這篇文章主要介紹了WordPress與Drupal的Nginx配置重寫規(guī)則示例,文中介紹的rewrite寫法簡單而突出配置重點,需要的朋友可以參考下
    2016-01-01
  • Nginx+Tomcat+Https 服務(wù)器負載均衡配置實踐方案詳解

    Nginx+Tomcat+Https 服務(wù)器負載均衡配置實踐方案詳解

    這篇文章主要介紹了Nginx+Tomcat+Https 服務(wù)器負載均衡配置實踐方案的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • Nginx中g(shù)zip資源壓縮的5大場景配置實戰(zhàn)指南

    Nginx中g(shù)zip資源壓縮的5大場景配置實戰(zhàn)指南

    本文深度剖析了Nginx?gzip壓縮的五大場景優(yōu)化策略,從前端靜態(tài)資源到Java動態(tài)API,從字體到監(jiān)控接口,助你避開CPU過載、緩存沖突等常見陷阱,實現(xiàn)精準(zhǔn)壓縮,讓網(wǎng)站加載速度飛升
    2026-07-07
  • Nginx解決CORS跨域配置的指南

    Nginx解決CORS跨域配置的指南

    Nginx作為高性能的反向代理服務(wù)器和 Web 服務(wù)器,是解決CORS跨域問題的工具之一,本篇將帶你從零開始,深入理解 CORS 原理,掌握 Nginx 中 CORS 的完整配置方案,并結(jié)合 Java 后端實戰(zhàn)案例,構(gòu)建一個安全、靈活、可擴展的跨域解決方案
    2026-07-07
  • 解讀Nginx變量字段大全

    解讀Nginx變量字段大全

    這篇文章主要介紹了Nginx變量字段,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-07-07
  • 如何使用nginx映射靜態(tài)網(wǎng)頁

    如何使用nginx映射靜態(tài)網(wǎng)頁

    Nginx作為高性能的Web服務(wù)器和反向代理服務(wù)器,常用于托管靜態(tài)網(wǎng)頁,這篇文章給大家介紹如何使用nginx映射靜態(tài)網(wǎng)頁,感興趣的朋友跟隨小編一起看看吧
    2026-05-05
  • nginx隱藏響應(yīng)頭server信息和版本號信息的操作方法

    nginx隱藏響應(yīng)頭server信息和版本號信息的操作方法

    文章介紹了兩種隱藏或修改Nginx響應(yīng)頭中server信息的方法:一種是通過修改配置文件全局段添加`server_tokens off`,另一種是重新編譯Nginx并修改Banner信息,兩種方法分別適用于傳統(tǒng)部署和需要更靈活自定義的情況,需要的朋友可以參考下
    2025-02-02
  • Nginx?Socket代理的實現(xiàn)方法

    Nginx?Socket代理的實現(xiàn)方法

    Nginx的socket代理通常指的是Nginx通過stream模塊來處理非HTTP的?TCP?流量,本文就來介紹一下Nginx?Socket代理的實現(xiàn)方法,具有一定的參考價值,感興趣的可以了解一下
    2024-04-04
  • Nginx配合php實現(xiàn)生成實時縮略圖功能

    Nginx配合php實現(xiàn)生成實時縮略圖功能

    這篇文章主要介紹了Nginx配合php實現(xiàn)生成實時縮略圖功能,這在一些特殊場合可能會要用到,需要的朋友可以參考下
    2014-10-10

最新評論

西昌市| 双城市| 琼结县| 新乡市| 科技| 慈利县| 毕节市| 个旧市| 荣昌县| 佛山市| 彝良县| 揭阳市| 靖江市| 成安县| 岱山县| 灵丘县| 礼泉县| 新郑市| 尚义县| 江油市| 天水市| 那坡县| 大新县| 仲巴县| 枞阳县| 通化市| 西藏| 福建省| 阳春市| 鹤庆县| 满洲里市| 芮城县| 时尚| 新和县| 宁南县| 信阳市| 北安市| 怀来县| 图木舒克市| 安宁市| 尉氏县|