环境描述:
- 操作系统:Red Hat Enterprise Linux Server release 7.6 (Maipo)
- 数据库:MySQL 5.7.22
- 中间件:nginx 1.9.8
- IP地址:192.168.1.50–生产环境为公网,这里用内网IP代替
- 实现目标:nginx代理mysql
操作步骤:
1.安装mysql
相信大家都会,过程略
2.编译安装nginx,这一步是关键
2.1.下载并解压nginx安装包,这里使用 nginx 1.9.8
cd /soft
tar zxvf nginx-1.19.8.tar.gz
2.2.编译安装,记得创建nginx用户哦!useradd nginx -s /bin/nologin
cd nginx-1.19.8.tar.gz
./configure --prefix=/usr/local/nginx \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-http_slice_module \
--with-file-aio --with-http_v2_module
3.最后安装
make && make install
4.配置反向代理,添加
#默认配置文件的路径为/usr/local/nginx/conf/nginx.conf
stream {
upstream mysql {
server 192.168.1.50:3306 max_fails=3 fail_timeout=30s;
}
server {
listen 33060;
proxy_pass mysql;
}
}
5.检查 nginx.conf是否有问题
可使用nginx自带的命令
/usr/local/nginx/sbin/nginx -t
6.启动nginx
/usr/local/nginx/sbin/nginx
7.检查端口,测试
# netstat -an |grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:33060 0.0.0.0:* LISTEN
看到这,相信你已经成功了,又不对的地方欢迎批评指正!