在 PHP 中创建与 PDO 的连接时出错
今天,我删除并重新安装了最新版本的 lampp,以便迁移到 php 5.30,突然一个非常简单的应用程序无法连接到 mysql 数据库。我正在使用 PDO 进行连接,并收到以下错误:
Warning: PDO::__construct() [pdo.--construct]: [2002] Invalid argument (trying to connect
via unix://) in /home/raistlin/www/todoapp/home.php on line 9
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002]
Invalid argument' in /home/raistlin/www/todoapp/home.php:9 Stack trace: #0
/home/raistlin/www/todoapp/home.php(9): PDO->__construct('mysql:host=loca...', 'USER',
'PASSWORD') #1 {main} thrown in /home/raistlin/www/todoapp/home.php on line 9
为了调试它,我目前没有捕获错误。
以下代码足以在我的系统上复制该问题:
<?php
$DBACCESS = array(
"connstring"=>"mysql:host=localhost;dbname=todoapp",
"host"=>"localhost",
"user"=>"user",
"password"=>"password",
"todoapp"=>"todoapp"
);
echo implode('<br \>',$DBACCESS);
$dbh = new PDO($DBACCESS['connstring'],$DBACCESS['user'],$DBACCESS['password']);
$dbh = null;
?>
在网上查找,我发现其他一两个人也有同样的问题,但他们都没有收到回复,更不用说工作了。有谁知道发生了什么事?我在配置中遗漏了什么吗?我需要做什么来修复它?
Today, I removed and reinstalled the latest version of lampp in order to move to php 5.30, and suddenly a very simple app is failing to connect to the mysql database. I'm using PDO to connect, and receiving the following error:
Warning: PDO::__construct() [pdo.--construct]: [2002] Invalid argument (trying to connect
via unix://) in /home/raistlin/www/todoapp/home.php on line 9
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002]
Invalid argument' in /home/raistlin/www/todoapp/home.php:9 Stack trace: #0
/home/raistlin/www/todoapp/home.php(9): PDO->__construct('mysql:host=loca...', 'USER',
'PASSWORD') #1 {main} thrown in /home/raistlin/www/todoapp/home.php on line 9
I am not catching the error at the moment, for the sake of debugging it.
The following code is enough to replicate the issue on my system:
<?php
$DBACCESS = array(
"connstring"=>"mysql:host=localhost;dbname=todoapp",
"host"=>"localhost",
"user"=>"user",
"password"=>"password",
"todoapp"=>"todoapp"
);
echo implode('<br \>',$DBACCESS);
$dbh = new PDO($DBACCESS['connstring'],$DBACCESS['user'],$DBACCESS['password']);
$dbh = null;
?>
Looking online, I've found one or two other people with the same issue, but none of them have received a response, much less a working one. Does anyone know what is happening? Is there something I missed in the configuration? What do I need to do to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通常意味着您需要指定 TCP/IP (1),或告诉 MySQL 您的 Unix 套接字在哪里 (2):
Usually means that you need to specify TCP/IP (1), or tell MySQL where your Unix socket is (2):
您还可以在数据库连接字符串中使用 127.0.0.1,而不是指定“localhost”,以完全避免此问题。
You can also use 127.0.0.1, rather than specifying "localhost", in your db connection string to avoid this issue altogether.
您可能需要修改 php.ini,以便 PDO 可以通过指定 pdo_mysql.default_socket = /opt/lampp/var/mysql/mysql.sock(对于 xampp)来找到 mysql.sock。更改 php.ini 后不要忘记重新启动 Apache。
(抱歉,这似乎是一个重复的解决方案)。
You might want to modify php.ini so PDO can find mysql.sock by specifying the
pdo_mysql.default_socket = /opt/lampp/var/mysql/mysql.sock
(in the case of xampp). Don't forget to restart Apache after changing php.ini.(Sorry, this seems to be a repeated solution).
我在这个项目中使用 MAMP 2.0.1 和 Symfony 1.4 以及 Doctrine。
第三种选择对我来说很有效,只需稍作修改:
在/config/databases.yml中
I'm using MAMP 2.0.1 and Symfony 1.4 with Doctrine for this project.
Third option worked for me with a small modification:
in /config/databases.yml
出现此类错误的最常见原因是 MySQL 未运行。
The most common cause of an error like this would be that MySQL isn’t running.