4

I've mysql database and php/apache on two different servers: let's say hostphp.domain.com and hostmysql.domain.com.

On the mysql server I've set a user "my_user" with permissions to connect to "my_database" db from the specific host "hostphp.domain.com".

When I connect to it using mysql_connect it does right. But when I do it via php PDO I get this error:

SQLSTATE[42000] [1044] Access denied for user 'my_user'@'%' to database 'my_database'

I've done some tests and I found the problem is ...@'%', mysql is refusing that connection because "my_user" does not have permission to connect from any host.

Also I've tried to connect using mysql_connect with a wrong password to see the error and I get this:

Could not connect: Access denied for user 'my_user'@'hostphp.domain.com' (using password: YES).

The difference is in ..@'%' and ...@'hostphp.domain.com'.

So that's my question, why php pdo do not declare hostname when connecting to a remote host? (or is doing that wrong).

Thanks and sorry for my english.

Edit. Some code example, this does not work:

try {
    $pdo = new PDO(
        'mysql:host=hostmysql.domain.com;port=3306;dbname=my_database',
        'my_user',
        'my_pass'
    );
} catch (PDOException $e) {
    die($e->getMessage());
}

but this works ok:

$conn = mysql_connect('hostmysql.domain.com', 'my_user', 'my_pass');
if (!$conn) {
    die('Could not connect: ' . mysql_error());
} 
4

1 回答 1

2

您已使用 mysql_connect 成功连接到主机,但没有出现任何错误,因为您没有尝试选择数据库。

您的用户可能无权访问您的数据库。

尝试运行 mysql_select_db("my_database"); 连接到主机后,您应该会收到相同的错误。

于 2012-10-24T11:46:41.557 回答