问题
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');
回答1:
$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
回答2:
ok i had the same problem too. the solutions is the space between
mysql: host
--> this work very well!!!
this way you can connect to remote mysql
回答3:
The problem on remote PDO mysql conex are on the db string. The correct statement is:
$conn = new PDO('mysql:host=192.168.1.4:3306;dbname=DB_TEST','username','password');
Regards,
回答4:
The hostname is invalid instead use host. The correct statement is:
$conn = new PDO('mysql:host=192.168.1.4:3306;dbname=DB_TEST','username','password');
来源:https://stackoverflow.com/questions/17630772/pdo-cannot-connect-remote-mysql-server