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!

11
  • Restart sql and apache. Commented May 29, 2017 at 18:03
  • Try: $pdo=new PDO ('mysql:host=ip-address;dbname=test', 'user', 'password'); Commented May 29, 2017 at 18:07
  • Is MySQL running, and on the default 3306 port? Commented May 29, 2017 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. Commented May 29, 2017 at 18:13
  • 1
    Do you have phpmyadmin installed and can that connect OK? Commented May 29, 2017 at 18:18

1 Answer 1

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 '$'

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for that typo. I made this error here, but in the script on the server it is a $
see by providing port number also, $pdo = new PDO('mysql:dbname=test;host=ip-address;port=xxxx;', $username, $password);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.