简体   繁体   中英

Connection denied to remote MySQL host through PHP but not through MySQL Workbench

The title really says it all. I am able to get into the remote server and run queries against the DB, but when I try to connect through PHP, I get this error:

SQLSTATE[HY000] [1045] Access denied for user 'user'@'blahblahblah' (using password: YES)

Here is the code I used to connect to the database:

<?php
function connectToDB() {
    $DBhost = 'remote.host.address.goes.here.com:3306';
    $DBname = 'dbName';
    $DBuser = 'myUserName';
    $DBpass = 'myPassword';
    $link = null;

    try
    {
        $link = new PDO("mysql:host=$DBhost;dbname=$DBname", $DBuser, $DBpass);
        $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }

    return $link;
}
?>

Like I said, I use the EXACT same credentials in MySQL Workbench and connect just fine, and I can query all I want. I am using XAMPP, could that be causing my problems?

Check pdo is loaded using the extension_loaded function.

if ( extension_loaded('pdo') ) {
   echo "pdo extensions loaded";
}

Check for database-specific PDO driver using:-

if ( extension_loaded('pdo_mysql') ) { // e.g., pdo_mysql
    echo "pdo_mysql driver loaded";
}

Add port to your connection string. Also added an attribute ATTR_EMULATE_PREPARES . Try.

$DBhost = "remote.host.address.goes.here.com:3306";
$DBPort = "3306";
$DBname = 'dbName';
$DBuser = 'myUserName';
$DBpass = 'myPassword';
$link   = null;


$link = new PDO("mysql:host=$DBhost;dbname=$DBname;port=$DBPort", $DBuser, $DBpass);
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$link->setAttribute( PDO::ATTR_EMULATE_PREPARES, false ); 

Not sure this will solve your problem.

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