dpicx06888
2014-12-08 15:48
浏览 28
已采纳

PDO没有连接到远程服务器? [关闭]

When I use mysql_connect('remote_server', 'user', 'pass') it works fine but when I try the following:

$db = new PDO('mysql:dbname=dbname;mysql:host=remote_server', 'user', 'pass');

It doesn't work. I had mysql-client installed on the web server initially. When trying the PDO connection I was receiving this error:

Connection Failed: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

So I installed mysql-server and got the following error instead:

Connection Failed: SQLSTATE[28000] [1045] Access denied for user 'user'@'localhost' (using password: YES)

I'm assuming this is because PDO is trying to connect to the local mysql server instead of the remote server. I tried using the local credentials and it connected but this is obviously not what I want. How do I get a connection to the remote server using a PDO connection to a SQL database? Why is it trying to use user@localhost when I specified a remote server in the DSN?

图片转代码服务由CSDN问答提供 功能建议

当我使用mysql_connect('remote_server','user','pass')时它工作正常但是当我尝试 以下内容:

  $ db = new PDO('mysql:dbname = dbname; mysql:host = remote_server','user','pass'); 
   
 
 

它不起作用。 我最初在Web服务器上安装了mysql-client。 尝试PDO连接时,我收到此错误:

连接失败:SQLSTATE [HY000] [2002]无法通过socket'/ var连接到本地MySQL服务器 /run/mysqld/mysqld.sock'(2)

所以我安装了mysql-server并改为输入以下错误:

连接失败:SQLSTATE [28000] [1045]用户'用户'@'localhost'拒绝访问(使用密码:是) < 我假设这是因为PDO正在尝试连接到本地mysql服务器而不是远程服务器。 我尝试使用本地凭据并将其连接,但这显然不是我想要的。 如何使用PDO连接到SQL数据库来连接远程服务器? 当我在DSN中指定远程服务器时,为什么要尝试使用user @ localhost?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongleiqiao4906 2014-12-08 16:08
    已采纳

    Posted on behalf of the OP:

    Can't believe I didn't catch the error in the DSN. Fixed :)

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题