1

I have a problem with a Database Connection. It sounds really simple, but I have been searching for a solution for a while now...

My Server:

  • Ubuntu 16.04
  • Apache 2
  • PHP 7.0.15

I put this PHP script into /var/www/html and gave it chmod 744 / 755 for testing.

try {
    $pdo=new PDO ('mysql:dbname=test;host=ip-address', 'user', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'Verbindung fehlgeschlagen: ' . $e->getMessage();
}

But when I call this file in my browser, the following message shows up:

SQLSTATE[HY000] [2002] Connection refused

The Database User has every rights. I also tried the root user.

Can anyone help me with this one? What could be the problem? I can't find an answer.

Thanks - Flo!

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Flapy
  • 11
  • 3
  • Restart sql and apache. – danjbh May 29 '17 at 18:03
  • Try: `$pdo=new PDO ('mysql:host=ip-address;dbname=test', 'user', 'password');` – danjbh May 29 '17 at 18:07
  • Is MySQL running, and on the default 3306 port? – Qirel May 29 '17 at 18:09
  • Change for a moment `PDOException` and replace it by `Exception` and tell us what is giving. Apart of that, the error is saying that the Database is rejecting your connection. If you are not in a `localhost` then you must open ports and configuration to allow that. If you are in `localhost`, maybe you have wrong user permissions to that Database. About the rights on the user, you can add rights for any IP or for a fixed IP. – matiaslauriti May 29 '17 at 18:13
  • 1
    Do you have phpmyadmin installed and can that connect OK? – Nigel Ren May 29 '17 at 18:18
  • Can you also check PDOException **§e** should be $e – Nigel Ren May 29 '17 at 18:19
  • Is this on a Digital Ocen Droplet or similar VM? – danjbh May 29 '17 at 18:20
  • Thanks for your answers :) I already restarted ubuntu, mysql and apache several times. I also used your PDO statement and changed "PDOException" to "Exception" - the same thing all the time. I have phpmyadmin installed and it works fine. The § is a $, i just missspelled that here. But i am not sure about the port. I have a IPCop running between my WindowsVM (where i call the php) and the ubuntu server. I'll try to open the port now – Flapy May 29 '17 at 18:35
  • If you are not in the same network (**LOCALHOST**) follow [this](https://stackoverflow.com/a/14779244/1998801) tutorial. – matiaslauriti May 29 '17 at 18:43
  • But i tried to run this script on localhost with w3m, and i got the same error :( – Flapy May 29 '17 at 18:44
  • I already did all of the tutorial stuff. But it doesnt work :/ And if there were any port problems, the script would work on the localhost, but it doesn't – Flapy May 29 '17 at 19:07

1 Answers1

1

Your code is correct, I have tested it with my database. Only small typo error is there, catch(PDOException §e) have the wrong character '§' instead of '$'

Chandan Rg
  • 80
  • 1
  • 8
  • Sorry for that typo. I made this error here, but in the script on the server it is a $ – Flapy May 29 '17 at 18:43
  • see by providing port number also, `$pdo = new PDO('mysql:dbname=test;host=ip-address;port=xxxx;', $username, $password);` – Chandan Rg May 29 '17 at 18:57