nginx四层负载均衡配置代理Mysql集群
环境如下:
- ip 192.168.6.203 Nginx
- ip 192.168.6.*(多台) Mysql
步骤一
查看Nginx是否安装stream模块
没安装则进行安装 操作步骤如下
pkill nginx
至此 已成功添加stream模块
步骤二
配置 mysql负载均衡案例
修改Nginx配置文件nginx.conf 内容如下图
测试步骤如下
- 后端Mysql需做好读写分离
- 创建好相应权限的用户
- 到客户端连接Nginx创建wuguiyunwei库进行测试
在客户端连接 创建测试库
连接3307读库查看 成功如下
当然为了高可用以下才是我们想要的效果
以上配置只是为了让大家了解stream模块。当然也可以用于生产环境,但还需完善工作如节点down剔除,完善的一些监控工作。。。
以下是实验环境的nginx主配文件
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | user www www; worker_processes auto; error_log /usr/local/nginx/logs/error_nginx.log crit; pid /var/run/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; multi_accept on; } stream { server { listen 3306; proxy_pass Mysql_write; } server { listen 3307; proxy_pass Mysql_read; } upstream Mysql_write { server 192.168.6.19:3306 weight=10; server 192.168.6.20:3306 weight=10; server 192.168.6.18:3306 weight=10; } upstream Mysql_read { server 192.168.6.175:3306 weight=10; server 192.168.6.176:3306 weight=10; server 192.168.6.177:3306 weight=10; } } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 1024m; client_body_buffer_size 10m; sendfile on; tcp_nopush on; keepalive_timeout 120; server_tokens off; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; fastcgi_intercept_errors on; gzip on; gzip_buffers 16 8k; gzip_comp_level 6; gzip_http_version 1.1; gzip_min_length 256; gzip_proxied any; gzip_vary on; gzip_types text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml text/javascript application/javascript application/x-javascript text/x-json application/json application/x-web-app-manifest+json text/css text/plain text/x-component font/opentype application/x-font-ttf application/vnd.ms-fontobject image/x-icon; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; ########################## vhost include vhost/*.conf; } |