nginx 代理mysql

安装nginx

nginx需要包含stream的module才可以代理tcp类型的请求,所以使用源码编译带–with-stream module的nginx

下载nginx安装包

cd /usr/local && wget http://nginx.org/download/nginx-1.9.9.tar.gz && tar -xf nginx-1.9.9.tar.gz 

编译安装nginx

cd /usr/local/nginx-1.9.9 && ./configure –prefix=/usr/local/nginx –with-stream && make -j4 && make install

配置nginx

>/usr/local/nginx/conf/nginx.conf && vi /usr/local/nginx/conf/nginx.conf

填写nginx.conf文件内容如下

#user nobody;
worker_processes auto;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 10240;
}

stream {
upstream dns {
server 10.0.0.14:3306;
# server 192.168.111.100:10086;
}

server {
listen 3307 ;
proxy_timeout 20s;
proxy_pass dns;
}
}

nginx注册为服务

vi /etc/systemd/system/nginx.service

nginx.service

[Unit]
Description=nginx – high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

load nginx服务

systemctl daemon-reload

启动nginx服务

systemctl start nginx

查看nginx服务状态

systemctl status nginx

开机自启动nginx、以及意外停止自动拉起nginx

systemctl enable nginx

验证mysql连接

mysql -h 127.0.0.1 -P 3307 -uroot -p  输入密码后连接成功、说明本机可以连接

mysql -h 139.186.197.10 -P 3307 -uroot -p   由于端口不通 ,等待客户调通安全组策略。

Leave a Reply