2

So I have 3 docker containers, one with PHP+PhalconPHP, one with MySQL and another with the webserver. Now on my host system I can connect to the MySQL in the docker container and I can also connect with PDO using the following code

new PDO('mysql:host=mysql;port=1024;dbname=database', 'root', 'password');

But for some reason the phalcon framework is unable to connect to the MySQL database using the config.php file like so

return new \Phalcon\Config([
    'database' => [
        'adapter'     => 'mysql',
        'port'        => 1024,
        'host'        => 'mysql',
        'username'    => 'root',
        'password'    => 'password',
        'dbname'      => 'database',
        'charset'     => 'utf8',
    ],
]);

In this case, for some reason I am getting a Can't connect to MySQL server on 'mysql' (111 "Connection refused) even though I am 100% sure I can actually connect (as proven before with the PDO connection). I also tried hardcoding the actual MySQL container ip address but no luck either.

Any ideas?

Liam Martens
  • 731
  • 7
  • 22
  • Have you seen [this](http://stackoverflow.com/questions/1420839/cant-connect-to-mysql-server-error-111)? Seems similar. Was the error the same when you hard-coded the IP address? –  Dec 29 '16 at 21:39
  • @Terminus yes, exactly same error and fact is that it's not the same issue as you linked since I can connect to it using PDO in the same PHP file as well as connect to it from my host system. – Liam Martens Dec 29 '16 at 21:46

1 Answers1

1

So it appeared to be kind of a stupid "mistake" but here's the solution. Apparently when you use the Phalcon Developer Tools to scaffold your project, it does not initialize the database with the port configuration as it outright ignores it. You can easily fix this by going into the app/config/services.php file and changing the db service to also include the port configuration.

$connection = new $class([
    'host'     => $config->database->host,
    'port'     => $config->database->port,
    'username' => $config->database->username,
    'password' => $config->database->password,
    'dbname'   => $config->database->dbname,
    'charset'  => $config->database->charset
]);
Liam Martens
  • 731
  • 7
  • 22