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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1435445/
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 02:33:52  来源:igfitidea点击:

Error on creating connection to PDO in PHP

phppdolampp

提问by krdluzni

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:

今天为了迁移到php 5.30,我卸载重装了最新版的lampp,突然一个很简单的app连接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

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?

在网上查看,我发现另外一两个人有同样的问题,但他们都没有收到回复,更不用说有效的回复了。有谁知道发生了什么?我在配置中遗漏了什么吗?我需要做什么来修复它?

回答by TML

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

通常意味着你需要指定 TCP/IP (1),或者告诉 MySQL 你的 Unix socket 在哪里 (2):

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

回答by jefftulsa

You can also use 127.0.0.1, rather than specifying "localhost", in your db connection string to avoid this issue altogether.

您还可以在 db 连接字符串中使用 127.0.0.1,而不是指定“localhost”来完全避免此问题。

回答by reeserc

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.

您可能想要修改 php.ini,以便 PDO 可以通过指定pdo_mysql.default_socket = /opt/lampp/var/mysql/mysql.sock(在 xampp 的情况下)找到 mysql.sock 。更改 php.ini 后不要忘记重新启动 Apache。

(Sorry, this seems to be a repeated solution).

(对不起,这似乎是一个重复的解决方案)。

回答by tacheshun

I'm using MAMP 2.0.1 and Symfony 1.4 with Doctrine for this project.

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

Third option worked for me with a small modification: in /config/databases.yml

第三个选项对我有用,稍作修改:在 /config/databases.yml

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

回答by pilsetnieks

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

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