当前位置:   article > 正文

搭建nginx服务器来代理mysql_nginx代理mysql

nginx代理mysql
一、下载nginx

nginx-1.8.0.tar.gz

二、安装&编译
yum -y install gcc gcc-c++ autocon
yum -y install pcre-devel openssl openssl-devel
tar -xvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module
make
make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
三、配置
  • /usr/local/nginx/conf/nginx.conf(可配置多个server)
#user  nobody;
worker_processes  2;

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

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

stream {
	upstream mysql1 {
        hash $remote_addr consistent;
        server $db_host1:$db_port1 max_fails=3 fail_timeout=30s;
    }
    server {
        listen 3021;
        proxy_connect_timeout 30s;
        proxy_pass mysql1;
    }

	upstream  mysql2 {
        hash $remote_addr consistent;
        server $db_host2:$db_port2 max_fails=3 fail_timeout=30s;
    }
    server {
        listen 3022;
        proxy_connect_timeout 30s;
        proxy_pass mysql2;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
四、启Nginx进程

1、启动:/usr/local/nginx/sbin/nginx
2、启动后会看到(ps -ef |grep nginx)一个master进程和两个worker进程
3、此时便可通过nginx的ip+server_port来连接mysql服务

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/230724
推荐阅读
相关标签
  

闽ICP备14008679号