简体   繁体   中英

SQLSTATE[HY000] [2002] Connection timed out while accessing remote database, logs show a connection was attempted

I am currently writing a web service that will receive a POST request and write the data to a database that is hosted on a different server. When running the script to a database on the same server, it behaves as expected. However, once it tries to access the remote database the connection times out with that error.

I've read a lot of the answers that refer to configuring MySQL to allow remote root connections, but we've created a user with all privileges from any host.

The server logs of the MySQL database show that the user we created attempted to access the database but didn't use a password even though the password is programmed to be entered. I do not have access to the core MySQL files at this time.

Could it still be an issue with root remote access or the firewall even though the server log shows an attempt to connect to the database?

This is the PHP function that throws the error:

function webservice_db_connect() {
    try {
        global $db_servername, $db_username, $db_password, $db_name, $db_port;
        $db = new PDO("mysql:host=$db_servername;dbname=$db_name;port=$db_port", $db_username, $db_password);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $db;
    } catch(PDOException $error) {
        exit('Connection failed: '. $error->getMessage());
    }
}

Final Result:

Connection failed: SQLSTATE[HY000] [2002] Connection timed out 

I would start by trying to log into the remote mySQL server from the mysql client:

mysql -h your.remoteserver.com -p 3345 -u yourusername -p yourpassword

Try different usernames for a successful connection.

Did you reload the privileges on the remote mySQL database?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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