现在我还需要远程的数据库可在别的地方使用,地址如下这样,
https://mariadb.buyilaoluo.top/
https://postgres.buyilaoluo.top/
能做吗
可以实现!你完全可以通过以下两种方式,将 MariaDB / PostgreSQL 提供远程访问,并使用 https://xxx.buyilaoluo.top/
的形式进行连接,但需要明确:
一、数据库是 TCP 协议服务,不能直接通过 HTTPS 连接。
你提到的 https://mariadb.buyilaoluo.top/
实际上不能直接作为数据库连接地址使用(因为 MariaDB/PostgreSQL 使用 TCP,而 HTTPS 是 Web 协议)。
但你可以通过以下方式实现:
方案一:通过 Nginx + SSL + TCP 转发(推荐)
让你仍然可以用 mariadb.buyilaoluo.top
、postgres.buyilaoluo.top
域名连接数据库。
1. 安装并启用 Nginx 的 TCP 流模块
确认你的 Nginx 编译时支持 --with-stream
模块:
nginx -V 2>&1 | grep -- --with-stream
若有输出说明支持。
2. 配置 Nginx 作为 TCP 代理
编辑 /etc/nginx/nginx.conf
,加入:
stream {
map $ssl_preread_server_name $backend_name {
mariadb.buyilaoluo.top mariadb_backend;
postgres.buyilaoluo.top postgres_backend;
}
upstream mariadb_backend {
server 127.0.0.1:3306;
}
upstream postgres_backend {
server 127.0.0.1:5432;
}
server {
listen 33060 ssl;
proxy_pass $backend_name;
ssl_preread on;
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/key.pem;
}
}
3. 打开 Nginx TCP 端口
例如防火墙放行 33060
端口:
sudo ufw allow 33060
4. 连接方式
其他主机可使用:
mysql -h mariadb.buyilaoluo.top -P 33060 -u 用户名 -p
psql -h postgres.buyilaoluo.top -p 33060 -U 用户名 -d 数据库名
方案二:使用 SSH 隧道或 VPN(更安全但复杂)
在远程设备上建立隧道:
ssh -L 3306:localhost:3306 user@buyilaoluo.top
然后本地用 localhost:3306
连接。
你想让我现在帮你配置 Nginx + TCP
的方案吗?还是更倾向用 SSH 隧道?
好的,我会详细说明 两种方式:
- 方案一:通过 Nginx
stream
模块配置 TCP 代理,以域名方式访问数据库。 - 方案二:使用 SSH 隧道连接远程数据库,适合临时或更安全的访问。
方案一:Nginx Stream TCP 代理配置(使用域名访问数据库)
目标效果:
数据库类型 | 域名 | 访问端口 | 实际转发到 |
---|---|---|---|
MariaDB | mariadb.buyilaoluo.top |
33060 |
127.0.0.1:3306 |
PostgreSQL | postgres.buyilaoluo.top |
33060 |
127.0.0.1:5432 |
步骤一:确认 Nginx 支持 stream
模块
nginx -V 2>&1 | grep -- --with-stream
如输出包含 --with-stream
说明支持,若没有,需要重新编译或用支持的版本(如 nginx 官方或用 Docker 容器)。
步骤二:配置 Nginx TCP 代理
编辑 /etc/nginx/nginx.conf
,加入以下内容(或新建 /etc/nginx/conf.d/stream.conf
):
# nginx.conf 顶层添加(http 块外)
stream {
map $ssl_preread_server_name $backend_name {
mariadb.buyilaoluo.top mariadb_backend;
postgres.buyilaoluo.top postgres_backend;
}
upstream mariadb_backend {
server 127.0.0.1:3306;
}
upstream postgres_backend {
server 127.0.0.1:5432;
}
server {
listen 33060 ssl;
proxy_pass $backend_name;
ssl_preread on;
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/key.pem;
}
}
步骤三:重启 Nginx
sudo nginx -t # 检查配置
sudo systemctl restart nginx
步骤四:打开防火墙端口
sudo ufw allow 33060
远程连接示例:
-
MariaDB:
mysql -h mariadb.buyilaoluo.top -P 33060 -u your_user -p
-
PostgreSQL:
psql -h postgres.buyilaoluo.top -p 33060 -U your_user -d your_db
方案二:SSH 隧道方式连接数据库(安全性更高)
适合:
- 私有连接,临时连接
- 不暴露数据库端口到公网
使用方法:
1. 本地建立 SSH 隧道
ssh -L 3306:localhost:3306 youruser@buyilaoluo.top
ssh -L 5432:localhost:5432 youruser@buyilaoluo.top
说明:
- 第一个将你本机的
3306
映射到服务器的 MariaDB。 - 第二个将你本机的
5432
映射到 PostgreSQL。
2. 本地连接:
-
MariaDB:
mysql -h 127.0.0.1 -P 3306 -u your_user -p
-
PostgreSQL:
psql -h 127.0.0.1 -p 5432 -U your_user -d your_db
总结:
方式 | 适用场景 | 是否暴露端口 | 是否支持域名 |
---|---|---|---|
Nginx TCP Proxy | 需要跨服务器访问(远程) | 是(需开 33060) | 是,支持 SNI 域名区分 |
SSH 隧道 | 管理员访问、临时调试、安全性高 | 否 | 否(仅本地连接) |
如你希望我直接生成你完整的 /etc/nginx/nginx.conf
配置并调试,也可以告诉我当前的 Nginx 结构(是否已有 stream
块等)。需要我帮你生成当前配置文件吗?
ssh隧道怎么管理,查询和关闭,维护之类的
SSH 隧道本质上是一个运行中的 SSH 会话,管理方式依赖你是手动执行的隧道,还是使用工具(如 autossh
、systemd
)常驻运行的隧道。下面是全面的 SSH 隧道管理方法:
一、查看当前 SSH 隧道连接
方式 1:查看进程
ps aux | grep 'ssh -L'
示例输出:
tux 12345 0.0 0.1 123456 456 ? Ss 10:00 0:00 ssh -L 3306:localhost:3306 tux@buyilaoluo.top
方式 2:确认端口监听
ss -tnlp | grep 3306
或:
lsof -i :3306
二、关闭 SSH 隧道
方法 1:直接关闭进程
kill 12345
或者一次性杀所有本地转发的 ssh 会话:
pkill -f 'ssh -L'
方法 2:如果你在终端里手动打开的,直接按:
Ctrl + C
即可终止 SSH 隧道连接。
三、让 SSH 隧道自动启动并守护运行
如果你希望隧道长期在线、重启后自动连接,可以用:
方法一:autossh
sudo apt install autossh
写成 systemd 服务或脚本,例如:
autossh -f -M 0 -N -L 3306:localhost:3306 tux@buyilaoluo.top
方法二:systemd 管理 SSH 隧道(推荐)
新建服务文件,例如:
# /etc/systemd/system/ssh-mariadb-tunnel.service
[Unit]
Description=SSH Tunnel to MariaDB
After=network.target
[Service]
User=tux
ExecStart=/usr/bin/ssh -N -L 3306:localhost:3306 tux@buyilaoluo.top
Restart=always
[Install]
WantedBy=multi-user.target
启用并启动:
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable --now ssh-mariadb-tunnel
查看状态:
systemctl status ssh-mariadb-tunnel
四、安全建议
- 可设置公钥认证,避免输入密码。
- 如在公网,建议使用 fail2ban 限制暴力破解。
- 使用非标准 SSH 端口或配合 VPN 提升安全。
是否需要我帮你写好 systemd
启动文件,或配置公钥免密?
以下是为 MariaDB 和 PostgreSQL 的 SSH 隧道连接 分别创建的 systemd
服务文件配置,适用于远程端口映射为:
mariadb.buyilaoluo.top
=> 本地3306
postgres.buyilaoluo.top
=> 本地5432
前提要求:
- 远程服务器已允许 SSH 登录;
- 本机已配置 SSH 公钥免密登录远程服务器;
tux
是当前用户;- 使用的远程主机统一为
buyilaoluo.top
,不同子域名只是为了区分用途。
一、MariaDB SSH 隧道配置
文件:/etc/systemd/system/ssh-tunnel-mariadb.service
[Unit]
Description=SSH Tunnel to MariaDB on buyilaoluo.top
After=network.target
[Service]
User=tux
ExecStart=/usr/bin/ssh -o ServerAliveInterval=60 -N -L 3306:localhost:3306 tux@buyilaoluo.top
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target
二、PostgreSQL SSH 隧道配置
文件:/etc/systemd/system/ssh-tunnel-postgres.service
[Unit]
Description=SSH Tunnel to PostgreSQL on buyilaoluo.top
After=network.target
[Service]
User=tux
ExecStart=/usr/bin/ssh -o ServerAliveInterval=60 -N -L 5432:localhost:5432 tux@buyilaoluo.top
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target
三、启用并启动服务
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable --now ssh-tunnel-mariadb.service
sudo systemctl enable --now ssh-tunnel-postgres.service
四、查看服务状态
systemctl status ssh-tunnel-mariadb
systemctl status ssh-tunnel-postgres
五、重启或关闭服务
# 重启
sudo systemctl restart ssh-tunnel-mariadb
sudo systemctl restart ssh-tunnel-postgres
# 停止
sudo systemctl stop ssh-tunnel-mariadb
sudo systemctl stop ssh-tunnel-postgres
是否需要我一并帮你配置公钥免密登录远程服务器 buyilaoluo.top
?或者将上述两个服务的映射端口修改为更安全的非标准端口?