适用于RDS不能绑定公网IP地址。
yum install nginx
systemctl start nginx
systemctl enable nginx
vim /etc/nginx/nginx.conf
在配置文件中在 http{} 结构体外新加如下配置:
注:
1. 10.128.40.128是RDS的内网地址,可以通过和ECS在同一VPC下的ECS去ping RDS的连接地址,能够解析到内网IP地址。
2. ECS所在的安全组需开放对应的数据库监听端口,这里是监听3306端口。
stream {
upstream cloudsocket {
hash $remote_addr consistent;
# $binary_remote_addr;
server 10.128.40.14:3306 weight=5 max_fails=3 fail_timeout=30s;
}
server {
listen 3306; #数据库服务器监听端口
proxy_connect_timeout 10s;
proxy_timeout 300s; #设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
proxy_pass cloudsocket;
}
}
完整配置:
[root@iZe3e05p9rga88h9vwl5hcZ ~]# vim /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 8088 default_server;
listen [::]:8088 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /404.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
stream {
upstream cloudsocket {
hash $remote_addr consistent;
# $binary_remote_addr;
server 10.128.40.14:3306 weight=5 max_fails=3 fail_timeout=30s;
}
server {
listen 3306;#数据库服务器监听端口
proxy_connect_timeout 10s;
proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
proxy_pass cloudsocket;
}
}
systemctl restart nginx
netstat -lntp
主机名为ECS的弹性IP。
账号为管控台上创建的账号,密码为账号对应的密码。
已成功连接RDS数据库。
yum install mysql -y
mysql -hIP地址 -P3306 -u账号 -p密码 -Dmytest
注:h表示主机,P表示端口,u表示账号,p表示密码,D表示数据库,-D可省略。
https://dev.mysql.com/downloads/mysql/
我的电脑--》右键属性--》高级系统设置--》环境变量--》系统变量
找到path编辑,新建环境变量。
mysql -hIP地址 -P3306 -u账号 -p密码 -Dmytest