Why does php PDO use a different host name that mysql_connect () uses when connecting to a remotely hosted mysql database?

I have a mysql and php / apache database on two different servers: say hostphp.domain.com and hostmysql.domain.com.

On mysql server, I installed the user "my_user" with permissions to connect to the "my_database" db from the specific host "hostphp.domain.com".

When I connect to it using mysql_connect, it does the right thing. But when I do this through php PDO, I get this error:

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

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

I also tried connecting using mysql_connect with the wrong password to see the error, and I get the following:

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

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

So, what is my question, why php pdo does not declare the host name when connecting to a remote host? (or does it wrong).

Thanks and sorry for my english.

Change In 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 fine:

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

You successfully connected to the host using mysql_connect, but you did not have errors because you did not try to select the database.

Your user may not have access to your database.

Try running mysql_select_db ("my_database"); after connecting to the host, and you should get the same error.

+2
source

Source: https://habr.com/ru/post/1441156/


All Articles