12monkeys
By:
12monkeys

phpMyAdmin One Click Installation

June 10, 2017 203 views
PHP MySQL

I just launched a phpMyAdmin droplet as a 1 click app. But I cannot connect to it from PHP on my local machine.

I've got it set up like so -

// database connection
$db_username = 'user';
$db_password = 'xxx';
$db_database = 'db_name';
$db_host = "123.456.789.100";

// connect to DB
try {
    $dbh = new PDO("mysql:host=$db_host;dbname=$db_database;charset=utf8", $db_username, $db_password);
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage(); die();
}

It works for my other databases, but not on digitalocean :/

3 Answers

Hi @12monkeys

Almost every one-click-app comes with firewall enabled, so you need to allow access to your MySQL from the outside if you want to use it remotely.

You can run this command to show the status of the firewall:

sudo ufw status

And run this command to allow access from anywhere to MySQL:

sudo ufw allow 3306

Please setup advanced, long users and password - or only allow connections from specific IP addresses. You can read more about the UFW firewall in this tutorial:
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-with-ufw-on-ubuntu-16-04

UFW, or Uncomplicated Firewall, is an interface to iptables that is geared towards simplifying the process of configuring a firewall. While iptables is a solid and flexible tool, it can be difficult for beginners to learn how to use it to properly configure a firewall. If you're looking to get started securing your network, and you're not sure which tool to use, UFW may be the right choice for you. This tutorial will show you how to set up a firewall with UFW on Ubuntu 16.04.

Hi @hansen, thanks for your suggestion. I tried the tutorial and now I get the following error -

Error!: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.

I tried googling it and tried a handful of seemingly related tutorials to no avail :( any ideas?

  • @12monkeys

    Where are you connecting from and to?
    The server you are connecting to has it's firewall blocking and/or MySQL is only listening on the internal interface.

    You need to allow the firewall with command sudo ufw status and then you need to edit the file /etc/mysql/my.cnf and change the parameter bind-address to make MySQL listen on all interfaces (including the external public interface):

    bind-address = 0.0.0.0
    

    And then restart MySQL with command sudo service mysql restart

Here's the result of sudo ufw status

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
443                        ALLOW       Anywhere
80                         ALLOW       Anywhere
3306                       ALLOW       Anywhere
Anywhere                   ALLOW       MY IP ADDRESS
3306/tcp                   ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)
80 (v6)                    ALLOW       Anywhere (v6)
3306 (v6)                  ALLOW       Anywhere (v6)
3306/tcp (v6)              ALLOW       Anywhere (v6)

I've edited the /etc/mysql/my.cnf file like so -


#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
bind-address = 0.0.0.0

Then I tried to restart the server and got this error -

root@phpmyadmin-512mb-sgp1-01:~# sudo service mysql restart
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.

So instead, I did a hard reset via the DO interface and I still get the following error:
Error!: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.

Have another answer? Share your knowledge.