在 PHP 中创建与 PDO 的连接时出错

发布于 2024-08-05 03:39:36 字数 1129 浏览 12 评论 0原文

今天,我删除并重新安装了最新版本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请爱~陌生人 2024-08-12 03:39:36

通常意味着您需要指定 TCP/IP (1),或告诉 MySQL 您的 Unix 套接字在哪里 (2):

  1. "mysql:host=127.0.0.1" 或 "mysql:host=localhost;port=3306"
  2. "mysql: unix_socket=/var/run/mysqld/mysqld.sock"

Usually means that you need to specify TCP/IP (1), or tell MySQL where your Unix socket is (2):

  1. "mysql:host=127.0.0.1" or "mysql:host=localhost;port=3306"
  2. "mysql:unix_socket=/var/run/mysqld/mysqld.sock"
宫墨修音 2024-08-12 03:39:36

您还可以在数据库连接字符串中使用 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.

此生挚爱伱 2024-08-12 03:39:36

您可能需要修改 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).

没有心的人 2024-08-12 03:39:36

我在这个项目中使用 MAMP 2.0.1 和 Symfony 1.4 以及 Doctrine。

第三种选择对我来说很有效,只需稍作修改:
在/config/databases.yml中

dsn: 'mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=MY_DB_NAME;'

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

dsn: 'mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=MY_DB_NAME;'
忆沫 2024-08-12 03:39:36

出现此类错误的最常见原因是 MySQL 未运行。

The most common cause of an error like this would be that MySQL isn’t running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文