简体   繁体   English

Digital Ocean将一个Droplet连接到另一个Droplet的MySQL DB

[英]Digital Ocean connect one droplet to the MySQL DB of another droplet

I have 2 droplets, both with a LAMP stack (Ubuntu 16.04). 我有2个液滴,两个液滴都带有LAMP堆栈(Ubuntu 16.04)。 Both also have PHPMyAdmin installed, run a PHP application, and have a self-signed SSH certificate that auto redirects to https. 两者均已安装PHPMyAdmin,运行PHP应用程序,并具有自动重定向到https的自签名SSH证书。

What I would like to do is connect to the MySQL database that's on Droplet 1 from the app on Droplet 2. I know you can do this by running a MySQL server on Droplet 1, but both need to run a full PHP app in this case. 我想做的是从Droplet 2上的应用程序连接到Droplet 1上的MySQL数据库。我知道您可以通过在Droplet 1上运行MySQL服务器来做到这一点,但是在这种情况下,两者都需要运行完整的PHP应用程序。

Here is what I've already done and tried: 这是我已经完成并尝试的操作:

  • Enabled private networking on both droplets (see this tutorial ) 在两个Droplet上都启用了专用网络(请参阅本教程
  • On Droplet 1 I've added port 3306 to the firewall with sudo ufw allow 3306/tcp and sudo service ufw restart 在Droplet 1上,我已使用sudo ufw allow 3306/tcp将端口3306添加到防火墙, sudo ufw allow 3306/tcpsudo service ufw restart
  • Both have the same firewall settings applied that are configured in the DO interface. 两者都应用了在DO界面中配置的相同防火墙设置。 I've added 'All TCP', 'All ports' from source 'Droplet 2' 我从源“ Droplet 2”添加了“所有TCP”,“所有端口”
  • Outbound rules on the firewall settings are 'ICMP', 'All TCP' and 'All UDP' from 'All ports' and 'All IPv4' and 'All IPv6' 防火墙设置的出站规则是“所有端口”,“所有IPv4”和“所有IPv6”中的“ ICMP”,“所有TCP”和“所有UDP”
  • I've created a MySQL user through PMA on Droplet 1 with hostname % 我已经在主机名为%的Droplet 1上通过PMA创建了一个MySQL用户

My PHP app connection settings (CodeIgniter 3.1.7) 我的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
);

Currently the app on Droplet 2 can not connect to the database that runs on Droplet 1. What am I missing? 当前,Droplet 2上的应用程序无法连接到在Droplet 1上运行的数据库。我缺少什么?

A few questions: 几个问题:

  • When installing both droplets I've ran mysql_secure_installation . 当安装两个mysql_secure_installation我已经运行了mysql_secure_installation If I remember correctly that disables remote access to the mysql databases, but not sure. 如果我没有记错的话,那会禁止对mysql数据库的远程访问,但是不确定。 I also don't know how to enable it again (if needed). 我也不知道如何再次启用它(如果需要)。
  • Do I need to do anything in the /etc/mysql/my.cnf file with bind-address? 我需要使用/ bind-address在/etc/mysql/my.cnf文件中执行任何操作吗?
  • When creating the user on Droplet 1 through PMA, did I use the right settings? 通过PMA在Droplet 1上创建用户时,我使用了正确的设置吗?
  • Any other setting that I'm missing? 我还缺少其他设置吗?

EDIT Running netstat -nltp gives 编辑运行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      -     

Make sure you have the correct bind address in your my.cnf file. 确保您的my.cnf文件中具有正确的绑定地址。

bind-address                   = << private IP here >>

Heads up, MySQL only supports binding to 0, 1 or all IP addresses on your server so if you don't want to listen on the private and public IP then you'll have to use the private IP and the settings of your PHP app on Droplet to connect to that instead of 127.0.0.1 . 请注意,MySQL仅支持绑定到服务器上的0、1或所有IP地址,因此,如果您不想侦听私有和公共IP,则必须使用私有IP和PHP应用程序的设置在Droplet上连接而不是127.0.0.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM