Digital Ocean 将一个 droplet 连接到另一个 droplet 的 MySQL DB
Digital Ocean connect one droplet to the MySQL DB of another droplet
我有 2 个液滴,都有一个 LAMP 堆栈 (Ubuntu 16.04)。两者都安装了 PHPMyAdmin,运行 一个 PHP 应用程序,并且有一个自动重定向到 https 的自签名 SSH 证书。
我想做的是从 Droplet 2 上的应用程序连接到 Droplet 1 上的 MySQL 数据库。我知道你可以通过 运行 宁一个 MySQL 服务器在 Droplet 1 上,但在这种情况下,两者都需要 运行 一个完整的 PHP 应用程序。
这是我已经做过和尝试过的:
- 在两个 droplet 上启用私有网络(参见 this tutorial)
- 在 Droplet 1 上,我使用
sudo ufw allow 3306/tcp
和 sudo service ufw restart
将端口 3306 添加到防火墙
- 两者都应用了在 DO 界面中配置的相同防火墙设置。我从来源 'Droplet 2'
添加了 'All TCP'、'All ports'
- 防火墙设置的出站规则是 'ICMP'、'All TCP' 和 'All UDP' 来自 'All ports' 和 'All IPv4' 以及 'All IPv6'
- 我在 Droplet 1 上通过 PMA 创建了一个 MySQL 用户,主机名为 %
我的 PHP 应用程序连接设置 (CodeIgniter 3.1.7)
$db['remotedb'] = array(
'dsn' => '',
'hostname' => 'PRIVATE IP OF DROPLET HERE', // in the format xx.xx.xx.xx so without https:// in front of it
'port' => 3306,
'username' => 'user',
'password' => 'password',
'database' => 'database',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'dbdebug' => FALSE,
'cacheon' => FALSE,
'cachedir' => '',
'charset' => 'utf8',
'dbcollat' => 'utf8generalci',
'swappre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'savequeries' => TRUE
);
目前 Droplet 2 上的应用程序无法连接到 运行 Droplet 1 上的数据库。我错过了什么?
几个问题:
- 安装两个 Droplet 时,我 运行
mysql_secure_installation
。如果我没记错的话,禁用对 mysql 数据库的远程访问,但不确定。我也不知道如何再次启用它(如果需要)。
- 我需要在 /etc/mysql/my.cnf 文件中使用绑定地址做任何事情吗?
- 通过 PMA 在 Droplet 1 上创建用户时,我是否使用了正确的设置?
- 我还缺少任何其他设置吗?
编辑
运行 netstat -nltp
给出
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
tcp6 0 0 :::25 :::* LISTEN -
tcp6 0 0 :::443 :::* LISTEN -
tcp6 0 0 :::80 :::* LISTEN -
确保您的 my.cnf
文件中有正确的绑定地址。
bind-address = << private IP here >>
注意,MySQL 仅支持绑定到您服务器上的 0、1 或所有 IP 地址,因此如果您不想监听私有 IP 和 public IP,那么您将必须在 Droplet 上使用私有 IP 和 PHP 应用程序的设置来连接到它,而不是 127.0.0.1
。
我有 2 个液滴,都有一个 LAMP 堆栈 (Ubuntu 16.04)。两者都安装了 PHPMyAdmin,运行 一个 PHP 应用程序,并且有一个自动重定向到 https 的自签名 SSH 证书。
我想做的是从 Droplet 2 上的应用程序连接到 Droplet 1 上的 MySQL 数据库。我知道你可以通过 运行 宁一个 MySQL 服务器在 Droplet 1 上,但在这种情况下,两者都需要 运行 一个完整的 PHP 应用程序。
这是我已经做过和尝试过的:
- 在两个 droplet 上启用私有网络(参见 this tutorial)
- 在 Droplet 1 上,我使用
sudo ufw allow 3306/tcp
和sudo service ufw restart
将端口 3306 添加到防火墙
- 两者都应用了在 DO 界面中配置的相同防火墙设置。我从来源 'Droplet 2' 添加了 'All TCP'、'All ports'
- 防火墙设置的出站规则是 'ICMP'、'All TCP' 和 'All UDP' 来自 'All ports' 和 'All IPv4' 以及 'All IPv6'
- 我在 Droplet 1 上通过 PMA 创建了一个 MySQL 用户,主机名为 %
我的 PHP 应用程序连接设置 (CodeIgniter 3.1.7)
$db['remotedb'] = array(
'dsn' => '',
'hostname' => 'PRIVATE IP OF DROPLET HERE', // in the format xx.xx.xx.xx so without https:// in front of it
'port' => 3306,
'username' => 'user',
'password' => 'password',
'database' => 'database',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'dbdebug' => FALSE,
'cacheon' => FALSE,
'cachedir' => '',
'charset' => 'utf8',
'dbcollat' => 'utf8generalci',
'swappre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'savequeries' => TRUE
);
目前 Droplet 2 上的应用程序无法连接到 运行 Droplet 1 上的数据库。我错过了什么?
几个问题:
- 安装两个 Droplet 时,我 运行
mysql_secure_installation
。如果我没记错的话,禁用对 mysql 数据库的远程访问,但不确定。我也不知道如何再次启用它(如果需要)。 - 我需要在 /etc/mysql/my.cnf 文件中使用绑定地址做任何事情吗?
- 通过 PMA 在 Droplet 1 上创建用户时,我是否使用了正确的设置?
- 我还缺少任何其他设置吗?
编辑
运行 netstat -nltp
给出
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
tcp6 0 0 :::25 :::* LISTEN -
tcp6 0 0 :::443 :::* LISTEN -
tcp6 0 0 :::80 :::* LISTEN -
确保您的 my.cnf
文件中有正确的绑定地址。
bind-address = << private IP here >>
注意,MySQL 仅支持绑定到您服务器上的 0、1 或所有 IP 地址,因此如果您不想监听私有 IP 和 public IP,那么您将必须在 Droplet 上使用私有 IP 和 PHP 应用程序的设置来连接到它,而不是 127.0.0.1
。