正文:
Nginx
的stream
模块是处理TCP
/UDP
数据流代理和负载均衡的强大工具,能够高效地转发TCP
消息。自Nginx
1.9.0 版本起,ngx_stream_core_module
模块成为标准配置。不过,默认情况下,该模块并未被编译进Nginx
中。
安装 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
python
2-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 作为要转发的机器,配置如下:
要重新启动 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.240Welcome
to
the
MariaDB
monitor
. Commands
end
with
; or
\g
.Your
MySQL
connection
id
is
12Server
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.1Welcome
to
the
MariaDB
monitor
. Commands
end
with
; or
\g
.Your
MySQL
connection
id
is
13Server
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.1Welcome
to
the
MariaDB
monitor
. Commands
end
with
; or
\g
.Your
MySQL
connection
id
is
19Server
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
1MySQL
[(none
)]> exit
Bye
[
root
@hy
conf
.d
]# mysql
-uroot
-p123456
-h127
.0.0.1ERROR
1130 (HY000
): Host
‘php
.qq
.com
’ is
not
allowed
to
connect
to
this
MySQL
server
转载请注明:汇站网 » Nginx
实现 MySQL
负载均衡(提升网站性能大法)