Skip to content
Advertisement

PDO cannot connect remote mysql server

server A(192.168.1.3)

mysql server(5.6.12) port 6603,socket /var/run/mysql/mysql.sock

php(5.5.0) php.ini pdo_mysql.default_socket = /var/run/mysql/mysql.sock

server B(192.168.1.4)

mysql server(5.5.11) port 3306,socket /var/run/mysql/mysql.sock

In server A is work when use

$conn = new PDO('mysql:hostname=localhost;dbname=DB_TEST','username','password');

but cannot connect to server B when use

$conn = new PDO('mysql:hostname=192.168.1.4;dbname=DB_TEST;port=3306','username','password');

ERROR:SQLSTATE[28000] [1045] Access denied for user ‘username’@’localhost’ (using password: YES)

but work on

$conn = mysql_connect('192.168.1.4:3306', 'username', 'password');

Advertisement

Answer

$conn = new PDO('mysql:hostname=192.168.1.4;dbname=DB_TEST;port=3306','username','password');

should be

$conn = new PDO('mysql:host=192.168.1.4;dbname=DB_TEST;port=3306','username','password');

hostname is invalid for dsn and so PDO is ignoring host and using default, which is localhost

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement