php PDO无法连接远程mysql服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17630772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:12:15  来源:igfitidea点击:

PDO cannot connect remote mysql server

phpmysqlpdodatabase-connection

提问by consatan

server A(192.168.1.3)

服务器 A(192.168.1.3)

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

mysql 服务器(5.6.12) 6603 端口,socket /var/run/mysql/mysql.sock

php(5.5.0) php.ini pdo_mysql.default_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)

服务器 B(192.168.1.4)

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

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

In server A is work when use

在服务器 A 中使用时工作

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

but cannot connect to server B when use

但使用时无法连接到服务器B

$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)

错误:SQLSTATE[28000] [1045] 用户 'username'@'localhost' 访问被拒绝(使用密码:YES)

but work on

但继续努力

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

回答by bansi

$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');

hostnameis invalid for dsnand so PDOis ignoring host and using default, which is localhost

hostname无效dsn,因此PDO忽略主机并使用默认值,即localhost

回答by Val Luis

ok i had the same problem too. the solutions is the space between

好的,我也有同样的问题。解决方案是之间的空间

mysql: host--> this work very well!!!

mysql: host--> 这项工作非常好!!!

this way you can connect to remote mysql

这样你就可以连接到远程mysql

回答by Luis Morales

The problem on remote PDO mysql conex are on the db string. The correct statement is:

远程 PDO mysql conex 上的问题出在 db 字符串上。正确的说法是:

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

Regards,

问候,

回答by Sani Kamal

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');