php Symfony3 - SQLSTATE[HY000] [2002] 没有这样的文件或目录

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

Symfony3 - SQLSTATE[HY000] [2002] No such file or directory

phpmysqlsymfony

提问by M. Douglas

I am new to Symfony and started doing some tutorials in order to get some things done. I am working on a Linux Mint 18 machine and a standard Symfony3-project.

我是 Symfony 的新手,并开始做一些教程以完成一些事情。我正在使用 Linux Mint 18 机器和标准的 Symfony3 项目。

I'd like to create a simple database to play around with it, but I am encountering an error I can't find the right solution for.

我想创建一个简单的数据库来使用它,但我遇到了一个错误,我找不到正确的解决方案。

My parameters.yml file looks like this:

我的 parameters.yml 文件如下所示:

parameters:
   database_host: localhost
   database_port: null
   database_name: symfony
   database_user: root
   database_password: null
   mailer_transport: smtp
   mailer_host: 127.0.0.1
   mailer_user: null
   mailer_password: null
   secret: (not relevant)

My Genus.php file looks like this:

我的 Genus.php 文件如下所示:

namespace AppBundle\Genus;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="genus")
 */
class Genus
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;
    /**
     * @ORM\Column(type="string")
     */
    private $name;

}

When I execute the command dpkg -l php*mysqlI get the following result:

当我执行命令dpkg -l php*mysql 时,我得到以下结果:

dpkg -l php*mysql
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name              Version       Architecture  Description
+++-=================-=============-=============-============================
un  php-pdo-mysql     <none>        <none>        (no description available)
ii  php7.0-mysql      7.0.8-0ubuntu amd64         MySQL module for PHP
un  php7.0-pdo-mysql  <none>        <none>        (no description available)

When I try to create a local database using the command prompt using

当我尝试使用命令提示符创建本地数据库时

XXX@XXX-pc ~/PhpstormProjects/test_symfony $ php bin/console doctrine:database:create

This is what I get:

这就是我得到的:

[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory
[Doctrine\DBAL\Driver\PDOException] SQLSTATE[HY000] [2002] No such file or directory
[PDOException] SQLSTATE[HY000] [2002] No such file or directory

[Doctrine\DBAL\Exception\ConnectionException]
驱动程序中发生异常:SQLSTATE[HY000] [2002] 没有
那个文件或目录[Doctrine\DBAL\Driver\PDOException] SQLSTATE[HY000] [2002] 没有
那个文件或目录[PDOException] ] SQLSTATE[HY000] [2002] 没有那个文件或目录

I've came across many other topics but wasn't able to find the right answer. Could anyone tell me what I am missing here? Any help is much appreciated :)

我遇到了许多其他主题,但无法找到正确的答案。谁能告诉我我在这里缺少什么?任何帮助深表感谢 :)

回答by Mari Y

In my case, I only changed localhost->127.0.0.1 and it worked!

就我而言,我只更改了 localhost->127.0.0.1 并且它起作用了!

回答by Ahmed Msafri

if you use mamp test this:

如果你使用 mamp 测试这个:

sudo ln -s /Applications/MAMP/tmp/mysql /var/mysql

回答by aina

May be It's too late but I had the same issue "SQLSTATE[HY000] [2002] No such file or directory" on my symfony 3.2.9 deployment

可能为时已晚,但我在 symfony 3.2.9 部署中遇到了同样的问题“SQLSTATE[HY000] [2002] No such file or directory”

I fixed it by changing the database_host value from "localhost" to my server IP on my parameters.yml file

我通过将 database_host 值从“localhost”更改为我的 parameters.yml 文件中的服务器 IP 来修复它

but I had this another error message when trying to run commandline by SSH:

但是在尝试通过 SSH 运行命令行时,我收到了另一条错误消息:

[Doctrine\DBAL\Exception\ConnectionException]
 An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused

I finaly fix it by adding these 2 lines on my config.yml file in Doctrine configuration section :

我最终通过在 Doctrine 配置部分的 config.yml 文件中添加这两行来修复它:

unix_socket: /var/lib/mysql/mysql.sock
server_version: '5.5'

all my final doctrine configuration is like this:

我所有的最终学说配置是这样的:

doctrine:
    dbal:
        driver: pdo_mysql
        host: '%database_host%'
        port: '%database_port%'
        dbname: '%database_name%'
        user: '%database_user%'
        password: '%database_password%'
        unix_socket: /var/lib/mysql/mysql.sock
        server_version: '5.5'
        charset: UTF8

hopefully this helps someone

希望这可以帮助某人

回答by Александар Ристи?

Had the same error.

有同样的错误。

OS: macOS High Sierra. MAMP 5.0.1

操作系统:macOS High Sierra。MAMP 5.0.1

The problem was that the default MySQL port on MAMP was 8889and in parameters.yml it was 3306

问题是 MAMP 上的默认 MySQL 端口是8889,而在 parameters.yml 中是3306

回答by Yash Sharma

[PDOException] SQLSTATE[HY000] [2002] No such file or directory

[PDOException] SQLSTATE[HY000] [2002] 没有那个文件或目录

This error occurs generally when your web application is not connected to the database. As per your parameter.ymlit's searching for mysql server & wasn't getting connected because at that time mysqlwasn't installed there at your machine.

当您的 Web 应用程序未连接到数据库时,通常会发生此错误。根据您的parameter.yml,它正在搜索 mysql 服务器并且没有连接,因为当时您的机器上没有安装mysql

回答by noonex

My knowledge of MySQL suggests one variant which might happen and explain this:

我对 MySQL 的了解表明了一种可能发生的变体并解释了这一点:

You had (old) mysqld running, then deleted out all files from datadir. Then probably new server wasn't able to start up Then you connected to that old mysqld, which is still running, but getting 'No such file or directory' error trying to access own system files (which were deleted).

您运行了(旧)mysqld,然后从 datadir 中删除了所有文件。然后可能新服务器无法启动然后您连接到仍在运行的旧 mysqld,但在尝试访问自己的系统文件(已删除)时出现“没有此类文件或目录”错误。