基于nginx四层负载均衡(tcp)代理mysql集群
1、安装nginx所需依赖
[root@localhost ~]# yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
2、下载Nginx源码包
[root@localhost ~]# wget http://nginx.org/download/nginx-1.9.9.tar.gz
3、解压并编译安装nginx
[root@localhost ~]# tar -xzf nginx-1.9.9.tar.gz
[root@localhost ~]# mv nginx-1.9.9 nginx
[root@localhost local]# cd nginx
[root@localhost nginx]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream
[root@localhost nginx]# make -j 2 && make install
4、编辑nginx配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
在最后一行添加一下内容:
stream {
server {
listen 3305;
proxy_pass db;
}
upstream db {
server 10.3.131.22:3306;
server 10.3.131.23:3306;
server 10.3.131.24:3306;
server 10.3.131.25:3306;
}
}
5、启动nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx
[root@localhost ~]# ss -lntp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:3305 *:* users:(("nginx",pid=29487,fd=7),("nginx",pid=29486,fd=7))
LISTEN 0 128 *:80 *:* users:(("nginx",pid=29487,fd=6),("nginx",pid=29486,fd=6))
6、测试
[root@localhost ~]# mysql -ullf123 -p'123456' -h localhost -P 3305
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>