PDOException SQLSTATE[HY000][2002]没有这样的文件或目录。

时间:2022-10-14 14:18:33

I believe that I've successfully deployed my (very basic) site to fortrabbit, but as soon as I connect to SSH to run some commands (such as php artisan migrate or php artisan db:seed) I get an error message:

我相信我已经成功地将我的(非常基本的)站点部署到fortrabbit,但是当我连接到SSH来运行一些命令(比如php artisan迁移或php artisan db:seed)时,我得到一个错误消息:

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

At some point the migration must have worked, because my tables are there - but this doesn't explain why it isn't working for me now.

在某种程度上,迁移一定起作用了,因为我的表在那里——但这并不能解释为什么它现在不能为我工作。

26 个解决方案

#1


73  

The error message indicates that a MySQL connection via socket is tried (which is not supported).

错误消息指出通过套接字连接MySQL连接(不支持)。

In the context of Laravel (artisan), you probably want to use a different / the correct environment. Eg: php artisan migrate --env=production (or whatever environment). See here.

在Laravel (artisan)的上下文中,您可能希望使用一个不同的/正确的环境。php artisan迁移—env=生产(或任何环境)。在这里看到的。

#2


475  

Laravel 4: Change "host" in the app/config/database.php file from "localhost" to "127.0.0.1"

Laravel 4:改变app/config/数据库中的“主机”。php文件从“localhost”到“127.0.0.1”

Laravel 5: Change "DB_HOST" in the .env file from "localhost" to "127.0.0.1"

Laravel 5:从“localhost”到“127.0.0.1”的.env文件中更改“DB_HOST”。

I had the exact same problem. None of the above solutions worked for me. I solved the problem by changing the "host" in the /app/config/database.php file from "localhost" to "127.0.0.1".

我也有同样的问题。以上的解决方案都不适合我。我通过改变/app/config/数据库中的“host”来解决这个问题。php文件从“localhost”到“127.0.0.1”。

Not sure why "localhost" doesn't work by default but I found this answer in a similar question solved in a symfony2 post. https://*.com/a/9251924/1231563

不知道为什么“localhost”在默认情况下不工作,但是我在一个类似的问题中找到了这个答案。https://*.com/a/9251924/1231563

Update: Some people have asked as to why this fix works so I have done a little bit of research into the topic. It seems as though they use different connection types as explained in this post https://*.com/a/9715164/1231563

更新:一些人问为什么这个修复工作这么做,所以我对这个主题做了一些研究。似乎他们使用了不同的连接类型,如本文中所解释的https://*.com/a/9715164/1231563。

The issue that arose here is that "localhost" uses a UNIX socket and can not find the database in the standard directory. However "127.0.0.1" uses TCP (Transmission Control Protocol), which essentially means it runs through the "local internet" on your computer being much more reliable than the UNIX socket in this case.

这里出现的问题是“localhost”使用UNIX套接字,在标准目录中找不到数据库。然而,“127.0.0.1”使用了TCP(传输控制协议),这意味着它在你的计算机上运行的“本地互联网”比在这种情况下的UNIX套接字要可靠得多。

#3


57  

I got the same problem and I'm running Mac OS X 10.10 Yosemite. I have enabled the Apache Server and PHP that already comes with the OS. Then I just configured the mCrypt library to get started. After that when I was working with models and DB I got the error:

我遇到了同样的问题,我正在运行Mac OS X 10.10 Yosemite。我已经启用了Apache服务器和PHP。然后,我刚刚配置了mCrypt库以启动。之后,当我使用模型和DB时,我得到了错误:

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

The reason I found is just because PHP and MySQL can't get connected themselves. To get this problem fixed, I follow the next steps:

我找到的原因是PHP和MySQL无法连接。为了解决这个问题,我遵循下面的步骤:

  1. Open a terminal and connect to the mysql with:

    打开终端,与mysql连接:

    mysql -u root -p

    mysql - u root - p

  2. It will ask you for the related password. Then once you get the mysql promt type the next command:

    它会问你相关的密码。一旦你得到了mysql promt类型下一个命令:

    mysql> show variables like '%sock%'

    mysql>显示了“%sock%”这样的变量

  3. You will get something like this:

    你会得到这样的东西:

    +-----------------------------------------+-----------------+ | Variable_name | Value | +-----------------------------------------+-----------------+ | performance_schema_max_socket_classes | 10 | | performance_schema_max_socket_instances | 322 | | socket | /tmp/mysql.sock | +-----------------------------------------+-----------------+

    + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - + | Variable_name | | +价值- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - + | performance_schema_max_socket_classes 10 | | | performance_schema_max_socket_instances | 322 | | |插座/ tmp / mysql。袜子| + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - +

  4. Keep the value of the last row:

    保持最后一行的值:

    /tmp/mysql.sock

    / tmp / mysql.sock

  5. In your laravel project folder, look for the database.php file there is where you configure the DB connection parameters. In the mysql section add the next line at the end:

    在您的laravel项目文件夹中,查找数据库。php文件中配置了DB连接参数。在mysql部分,添加下一行代码:

    'unix_socket' => '/tmp/mysql.sock'

    “unix_socket”= >“/ tmp / mysql.sock”

  6. You must have something like this:

    你必须有这样的东西:

'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'SchoolBoard', 'username' => 'root', 'password' => 'venturaa', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'unix_socket' => '/tmp/mysql.sock', ),

“mysql”= >阵列(“司机”= >“mysql”,“主机”= >“localhost”、“数据库”= >“教育委员会”、“用户名”= >“根”,“密码”= >“venturaa”,“字符集”= >“use utf8”,“排序”= >“utf8_unicode_ci”,“前缀”= >“,“unix_socket”= >“/ tmp / mysql。袜子的),

Now just save changes, and reload the page and it must work! I hope it could be useful!

现在保存更改,并重新加载页面,它必须工作!我希望它会有用!

#4


37  

I encountered the [PDOException] SQLSTATE[HY000] [2002] No such file or directory error for a different reason. I had just finished building a brand new LAMP stack on Ubuntu 12.04 with Apache 2.4.7, PHP v5.5.10 and MySQL 5.6.16. I moved my sites back over and fired them up. But, I couldn't load my Laravel 4.2.x based site because of the [PDOException] above. So, I checked php -i | grep pdo and noticed this line:

我遇到了[PDOException] SQLSTATE[HY000][2002],没有这样的文件或目录错误,原因是不同的。我刚刚在Ubuntu 12.04上构建了一个全新的LAMP堆栈,Apache 2.4.7、PHP v5.5.10和MySQL 5.6.16。我把我的网站搬了过来,把它们都炒了。但是,我不能装载我的Laravel 4.2。基于以上的[PDOException]。因此,我检查了php - | grep pdo并注意到这一行:

pdo_mysql.default_socket => /tmp/mysql.sock => /tmp/mysql.sock

But, in my /etc/my.cnf the sock file is actually in /var/run/mysqld/mysqld.sock.

但是,在我的/etc/my.cnf中,sock文件实际上位于/var/run/mysqld/mysqld.sock。

So, I opened up my php.ini and set the value for pdo_mysql.default_socket:

我打开了php。ini设置了pdo_mysql.default_socket的值:

pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

Then, I restarted apache and checked php -i | grep pdo:

然后,我重新启动apache并检查php - | grep pdo:

pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock

That fixed it for me.

帮我修好了。

#5


35  

The answer from @stuyam solved the "No such file or directory" issue for me

@stuyam的回答为我解决了“没有这样的文件或目录”问题。

Short answer: Change "host" in the /app/config/database.php file from "localhost" to "127.0.0.1"

简单回答:在/app/config/数据库中更改“主机”。php文件从“localhost”到“127.0.0.1”

But then I had a "Connection refused" error. If anyone had the same issue, my solution for this was to update the app/config/local/database.php file so the port is 8889:

但是我有一个“拒绝连接”的错误。如果有人有同样的问题,我的解决方案是更新app/config/local/database。php文件,所以端口是8889:

'mysql' => array(
        'driver'    => 'mysql',
        'host'      => '127.0.0.1',
        'port'      => '8889',
        'database'  => 'databaseName',
        'username'  => 'root',
        'password'  => 'root',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

#6


19  

If you are using Laravel Homestead, make sure you're calling the commands on the server.

如果您正在使用Laravel Homestead,请确保您正在调用服务器上的命令。

homestead ssh

Then simply cd to the right directory and fire your command there.

然后简单的cd到正确的目录并在那里发射你的命令。

#7


15  

Mamp user enable option Allow network access to MYSQL

Mamp用户启用选项允许网络访问MYSQL。

PDOException SQLSTATE[HY000][2002]没有这样的文件或目录。

#8


13  

Add mysql.sock path in database.php file like below example

添加mysql。袜子在数据库路径。php文件,比如下面的例子。

'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

Eample

Eample

'mysql' => [
        'driver' => 'mysql',
        'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '8889'),

#9


12  

In my case i had no problem at all, just forgot to start the mysql service...

在我的情况下,我完全没有问题,只是忘记启动mysql服务…

sudo service mysqld start

sudo服务mysqld开始

#10


10  

Step 1

Find the path to your unix_socket, to do that just run netstat -ln | grep mysql

找到您的unix_socket的路径,只需运行netstat -ln | grep mysql。

You should get something like this

你应该得到这样的东西。

unix  2      [ ACC ]     STREAM     LISTENING     17397    /var/run/mysqld/mysqld.sock

Step 2

Take that and add it in your unix_socket param

将其添加到您的unix_socket param中。

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => '67.25.71.187',
            'database'  => 'dbname',
            'username'  => 'username',
            'password'  => '***',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'unix_socket'    => '/var/run/mysqld/mysqld.sock' <-----
            ),
        ),

Hope it helps !!

希望它帮助! !

#11


8  

Building on the answer from @dcarrith ...

从@dcarrith的答案上构建…

Instead of editing the config files, I created an alias in the location that PHP is looking that connects to the real mysql.sock. (source)

我没有编辑配置文件,而是在PHP的位置创建了一个别名,它连接到真正的mysql.sock。(源)

Just run these two commands (no restart needed):

只需运行这两个命令(不需要重新启动):

mkdir /var/mysql
ln -s /tmp/mysql.sock /var/mysql/mysql.sock

#12


7  

I'm running on MAMP Pro and had this similar problem when trying to migrate (create db tables). Tried a few of these mentioned suggestions as well but didn't do it for me.

我在MAMP Pro上运行,在尝试迁移时遇到了类似的问题(创建db表)。我也尝试了一些建议,但并没有为我做。

So, simply (after an hour googling), I added two things to the /config/database.php.

因此,简单地(在google上一个小时之后),我在/config/database.php中添加了两项内容。

'port' => '1234',
'unix_socket' => '/path/to/my/socket/mysqld.sock'

Works fine now!

现在没问题了!

#13


2  

As of Laravel 5 the database username and password goes in the .env file that exists in the project directory, e.g.

在Laravel 5中,数据库用户名和密码进入了在项目目录中存在的.env文件中。

DB_HOST=127.0.0.1
DB_DATABASE=db1
DB_USERNAME=user1
DB_PASSWORD=pass1

As you can see these environment variables are overriding the 'forge' strings here so changing them has no effect:

你可以看到,这些环境变量覆盖了“伪造”字符串,所以改变它们是无效的:

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'forge'),
        'username'  => env('DB_USERNAME', 'forge'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

More information is here https://mattstauffer.co/blog/laravel-5.0-environment-detection-and-environment-variables

这里有更多的信息,https://mattstauffer.co/blog/laravel-5.0-environment- detectionand -environment- detectionand -environment-variables。

#14


2  

I ran into this problem when running PHPUnit in Elixir/Gulp, and Homestead as my Vagrant enviroment.

我在Elixir/Gulp中运行PHPUnit时遇到了这个问题,而Homestead是我的流浪环境。

In my case I edited the .env file from DB_HOST=localhost to DB_HOST=192.168.10.10 where 192.168.10.10 is the IP of my Vagrant/Homestead host.

在我的例子中,我从DB_HOST=localhost到DB_HOST=192.168.10.10编辑了.env文件,其中192.168.10.10是我的游子/Homestead主机的IP。

#15


2  

This is because PDO treats "localhost" host specially:

这是因为PDO特别对待“localhost”主机:

Note: Unix only: When the host name is set to "localhost", then the connection to the server is made thru a domain socket. If PDO_MYSQL is compiled against libmysqlclient then the location of the socket file is at libmysqlclient's compiled in location. If PDO_MYSQL is compiled against mysqlnd a default socket can be set thru the pdo_mysql.default_socket setting.

注意:只有Unix:当主机名被设置为“localhost”时,连接到服务器的连接是通过一个域套接字完成的。如果PDO_MYSQL是针对libmysqlclient编译的,那么套接字文件的位置在libmysqlclient编译的位置。如果PDO_MYSQL是针对mysqlnd编译的,那么可以通过PDO_MYSQL .default_socket设置设置默认的套接字。

(from http://php.net/manual/en/ref.pdo-mysql.connection.php)

(来自http://php.net/manual/en/ref.pdo-mysql.connection.php)

Changing localhost to 127.0.0.1 will "force" the use of TCP.

将localhost更改为127.0.0.1将“强制”使用TCP。

Note: mysqli_connect is working fine with localhost.

注意:mysqli_connect在本地主机上运行良好。

#16


1  

If you are using Laravel Homestead, here is settings

如果你正在使用Laravel Homestead,这里是设置。

(include Vagrant-Virtual Machine)

(包括Vagrant-Virtual机)

.bash-profile

.bash-profile

alias vm="ssh vagrant@127.0.0.1 -p 2222"

database.php

database.php

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', '127.0.0.1'),
        'database'  => env('DB_DATABASE', 'homestead'),
        'username'  => env('DB_USERNAME', 'homestead'),
        'password'  => env('DB_PASSWORD', 'secret'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

Terminal

终端

vm

vagrant@homestead:~/Code/projectFolder  php artisan migrate:install

#17


1  

Attempt to connect to localhost:

尝试连接到本地主机:

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

Attempt to connect to 127.0.0.1:

尝试连接到127.0.0.1:

SQLSTATE[HY000] [2002] Connection refused

OK, just comment / remove the following setting from my.cnf (on OS X 10.5: /opt/local/etc/mysqlxx/my.cnf) to obtain:

好的,请在my.cnf (OS X 10.5: /opt/local/etc/mysqlxx/my.cnf)中注释/删除以下设置:

[mysqld]
# skip-networking

Of course, stop and start MySQL Server.

当然,停止并启动MySQL服务器。

#18


1  

If anyone are still looking for the answer, just check your .env file. For some reason laravel create a .env.example file, so all this answers didn't work for me. I fixed my issue renamming .env.example to .env

如果有人还在寻找答案,请检查你的。env文件。由于某种原因,laravel创造了一个。env。示例文件,所以所有这些答案对我都不起作用。我修正了我的问题,renamming .env。例子.env

#19


0  

it's the correct response for me. I disable the option pdo_mysql.default_socket in my php.ini and I design the host at 1.27.0.0.1

这是对我的正确回应。我在php中禁用了pdo_mysql.default_socket选项。我和ini设计的主机为1.27.0.0.1。

#20


0  

When using a VirtualMachine make sure you ssh into that machine and navigate to your App folder and call the php artisan migrate command from there.

当使用虚拟机时,确保您ssh到该机器并导航到您的应用程序文件夹,并从那里调用php artisan迁移命令。

#21


0  

I had problems accessing my drupal website and I had a problem exactly like that I fixed it within less than 2 hours. Try on your command line and restart your mysql server or service. Type this when logged in as root or super user, - service mysqld restart

我在访问drupal网站时遇到了一些问题,我遇到了一个问题,就像我在不到2小时内就把它修好了一样。尝试您的命令行并重新启动mysql服务器或服务。在作为根用户或超级用户登录时键入此命令,-服务mysqld重新启动。

It should work. If it doesn't restart your local webserver, - service httpd restart

它应该工作。如果它没有重启您的本地webserver, -服务httpd重启。

That should be enough. Hope it works for other environments too. Cheers.

这应该足够了。希望它也适用于其他环境。欢呼。

#22


0  

In may case, I'd simply used

在5月份,我只是简单地使用了。

vagrant up

instead of

而不是

homestead up

for my forge larval setup using homestead. I'm assuming this meant the site was getting served, but the MySQL server wasn't ever booted. When I used the latter command to launch my vagrant box, the error went away.

为我的锻造幼虫设置使用宅地。我假设这意味着站点已经被服务了,但是MySQL服务器并没有被引导。当我使用后者的命令来启动我的流浪箱时,错误就消失了。

#23


0  

All these answers seem like heavy lifting...

所有这些答案似乎都是沉重的负担……

I just created a .env file; edited my bootstrap/app.php file, and uncommented the following line...

我刚刚创建了一个。env文件;编辑我的引导/应用程序。php文件,未注释的下面一行…

Dotenv::load(__DIR__.'/../');

Dotenv:加载(__DIR__。“/ . . /”);

Hope this helps someone

希望这可以帮助别人

#24


0  

[This is outdated, database info is stored in .env now] More easier way to solve the issue by is editing the

[这是过时的,数据库信息存储在。env现在]更简单的方式解决问题的是编辑。

app/config/local/database.php

:)

:)

Change the database configuration here and it should work

更改这里的数据库配置,它应该可以工作。

#25


0  

This happened to me because MySQL wasn't running. MySQL was failing to start because I had a missing /usr/local/etc/my.cnf.d/ directory.

这发生在我身上,因为MySQL没有运行。MySQL未能启动,因为我有一个缺失的/usr/local/etc/my.cnf.d/目录。

This was being required by my /usr/local/etc/my.cnf config file as a glob include (include /usr/local/etc/my.cnf.d/*.cnf).

这是我的/usr/local/etc/my.cnf配置文件所要求的,作为一个glob包括(包括/usr/local/etc/my.cnf)。

Running mkdir /usr/local/etc/my.cnf.d, and then starting MySQL, fixed the issue.

运行mkdir /usr/local/etc/my.cnf.d,然后启动MySQL,解决了这个问题。

#26


0  

Check your port carefully . In my case it was 8889 and i am using 8888. change "DB_HOST" from "localhost" to "127.0.0.1" and vice versa

仔细检查你的港口。在我的例子中是8889,我用的是8888。将“DB_HOST”从“localhost”改为“127.0.0.1”,反之亦然。

#1


73  

The error message indicates that a MySQL connection via socket is tried (which is not supported).

错误消息指出通过套接字连接MySQL连接(不支持)。

In the context of Laravel (artisan), you probably want to use a different / the correct environment. Eg: php artisan migrate --env=production (or whatever environment). See here.

在Laravel (artisan)的上下文中,您可能希望使用一个不同的/正确的环境。php artisan迁移—env=生产(或任何环境)。在这里看到的。

#2


475  

Laravel 4: Change "host" in the app/config/database.php file from "localhost" to "127.0.0.1"

Laravel 4:改变app/config/数据库中的“主机”。php文件从“localhost”到“127.0.0.1”

Laravel 5: Change "DB_HOST" in the .env file from "localhost" to "127.0.0.1"

Laravel 5:从“localhost”到“127.0.0.1”的.env文件中更改“DB_HOST”。

I had the exact same problem. None of the above solutions worked for me. I solved the problem by changing the "host" in the /app/config/database.php file from "localhost" to "127.0.0.1".

我也有同样的问题。以上的解决方案都不适合我。我通过改变/app/config/数据库中的“host”来解决这个问题。php文件从“localhost”到“127.0.0.1”。

Not sure why "localhost" doesn't work by default but I found this answer in a similar question solved in a symfony2 post. https://*.com/a/9251924/1231563

不知道为什么“localhost”在默认情况下不工作,但是我在一个类似的问题中找到了这个答案。https://*.com/a/9251924/1231563

Update: Some people have asked as to why this fix works so I have done a little bit of research into the topic. It seems as though they use different connection types as explained in this post https://*.com/a/9715164/1231563

更新:一些人问为什么这个修复工作这么做,所以我对这个主题做了一些研究。似乎他们使用了不同的连接类型,如本文中所解释的https://*.com/a/9715164/1231563。

The issue that arose here is that "localhost" uses a UNIX socket and can not find the database in the standard directory. However "127.0.0.1" uses TCP (Transmission Control Protocol), which essentially means it runs through the "local internet" on your computer being much more reliable than the UNIX socket in this case.

这里出现的问题是“localhost”使用UNIX套接字,在标准目录中找不到数据库。然而,“127.0.0.1”使用了TCP(传输控制协议),这意味着它在你的计算机上运行的“本地互联网”比在这种情况下的UNIX套接字要可靠得多。

#3


57  

I got the same problem and I'm running Mac OS X 10.10 Yosemite. I have enabled the Apache Server and PHP that already comes with the OS. Then I just configured the mCrypt library to get started. After that when I was working with models and DB I got the error:

我遇到了同样的问题,我正在运行Mac OS X 10.10 Yosemite。我已经启用了Apache服务器和PHP。然后,我刚刚配置了mCrypt库以启动。之后,当我使用模型和DB时,我得到了错误:

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

The reason I found is just because PHP and MySQL can't get connected themselves. To get this problem fixed, I follow the next steps:

我找到的原因是PHP和MySQL无法连接。为了解决这个问题,我遵循下面的步骤:

  1. Open a terminal and connect to the mysql with:

    打开终端,与mysql连接:

    mysql -u root -p

    mysql - u root - p

  2. It will ask you for the related password. Then once you get the mysql promt type the next command:

    它会问你相关的密码。一旦你得到了mysql promt类型下一个命令:

    mysql> show variables like '%sock%'

    mysql>显示了“%sock%”这样的变量

  3. You will get something like this:

    你会得到这样的东西:

    +-----------------------------------------+-----------------+ | Variable_name | Value | +-----------------------------------------+-----------------+ | performance_schema_max_socket_classes | 10 | | performance_schema_max_socket_instances | 322 | | socket | /tmp/mysql.sock | +-----------------------------------------+-----------------+

    + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - + | Variable_name | | +价值- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - + | performance_schema_max_socket_classes 10 | | | performance_schema_max_socket_instances | 322 | | |插座/ tmp / mysql。袜子| + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - +

  4. Keep the value of the last row:

    保持最后一行的值:

    /tmp/mysql.sock

    / tmp / mysql.sock

  5. In your laravel project folder, look for the database.php file there is where you configure the DB connection parameters. In the mysql section add the next line at the end:

    在您的laravel项目文件夹中,查找数据库。php文件中配置了DB连接参数。在mysql部分,添加下一行代码:

    'unix_socket' => '/tmp/mysql.sock'

    “unix_socket”= >“/ tmp / mysql.sock”

  6. You must have something like this:

    你必须有这样的东西:

'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'SchoolBoard', 'username' => 'root', 'password' => 'venturaa', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'unix_socket' => '/tmp/mysql.sock', ),

“mysql”= >阵列(“司机”= >“mysql”,“主机”= >“localhost”、“数据库”= >“教育委员会”、“用户名”= >“根”,“密码”= >“venturaa”,“字符集”= >“use utf8”,“排序”= >“utf8_unicode_ci”,“前缀”= >“,“unix_socket”= >“/ tmp / mysql。袜子的),

Now just save changes, and reload the page and it must work! I hope it could be useful!

现在保存更改,并重新加载页面,它必须工作!我希望它会有用!

#4


37  

I encountered the [PDOException] SQLSTATE[HY000] [2002] No such file or directory error for a different reason. I had just finished building a brand new LAMP stack on Ubuntu 12.04 with Apache 2.4.7, PHP v5.5.10 and MySQL 5.6.16. I moved my sites back over and fired them up. But, I couldn't load my Laravel 4.2.x based site because of the [PDOException] above. So, I checked php -i | grep pdo and noticed this line:

我遇到了[PDOException] SQLSTATE[HY000][2002],没有这样的文件或目录错误,原因是不同的。我刚刚在Ubuntu 12.04上构建了一个全新的LAMP堆栈,Apache 2.4.7、PHP v5.5.10和MySQL 5.6.16。我把我的网站搬了过来,把它们都炒了。但是,我不能装载我的Laravel 4.2。基于以上的[PDOException]。因此,我检查了php - | grep pdo并注意到这一行:

pdo_mysql.default_socket => /tmp/mysql.sock => /tmp/mysql.sock

But, in my /etc/my.cnf the sock file is actually in /var/run/mysqld/mysqld.sock.

但是,在我的/etc/my.cnf中,sock文件实际上位于/var/run/mysqld/mysqld.sock。

So, I opened up my php.ini and set the value for pdo_mysql.default_socket:

我打开了php。ini设置了pdo_mysql.default_socket的值:

pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

Then, I restarted apache and checked php -i | grep pdo:

然后,我重新启动apache并检查php - | grep pdo:

pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock

That fixed it for me.

帮我修好了。

#5


35  

The answer from @stuyam solved the "No such file or directory" issue for me

@stuyam的回答为我解决了“没有这样的文件或目录”问题。

Short answer: Change "host" in the /app/config/database.php file from "localhost" to "127.0.0.1"

简单回答:在/app/config/数据库中更改“主机”。php文件从“localhost”到“127.0.0.1”

But then I had a "Connection refused" error. If anyone had the same issue, my solution for this was to update the app/config/local/database.php file so the port is 8889:

但是我有一个“拒绝连接”的错误。如果有人有同样的问题,我的解决方案是更新app/config/local/database。php文件,所以端口是8889:

'mysql' => array(
        'driver'    => 'mysql',
        'host'      => '127.0.0.1',
        'port'      => '8889',
        'database'  => 'databaseName',
        'username'  => 'root',
        'password'  => 'root',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

#6


19  

If you are using Laravel Homestead, make sure you're calling the commands on the server.

如果您正在使用Laravel Homestead,请确保您正在调用服务器上的命令。

homestead ssh

Then simply cd to the right directory and fire your command there.

然后简单的cd到正确的目录并在那里发射你的命令。

#7


15  

Mamp user enable option Allow network access to MYSQL

Mamp用户启用选项允许网络访问MYSQL。

PDOException SQLSTATE[HY000][2002]没有这样的文件或目录。

#8


13  

Add mysql.sock path in database.php file like below example

添加mysql。袜子在数据库路径。php文件,比如下面的例子。

'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

Eample

Eample

'mysql' => [
        'driver' => 'mysql',
        'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '8889'),

#9


12  

In my case i had no problem at all, just forgot to start the mysql service...

在我的情况下,我完全没有问题,只是忘记启动mysql服务…

sudo service mysqld start

sudo服务mysqld开始

#10


10  

Step 1

Find the path to your unix_socket, to do that just run netstat -ln | grep mysql

找到您的unix_socket的路径,只需运行netstat -ln | grep mysql。

You should get something like this

你应该得到这样的东西。

unix  2      [ ACC ]     STREAM     LISTENING     17397    /var/run/mysqld/mysqld.sock

Step 2

Take that and add it in your unix_socket param

将其添加到您的unix_socket param中。

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => '67.25.71.187',
            'database'  => 'dbname',
            'username'  => 'username',
            'password'  => '***',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'unix_socket'    => '/var/run/mysqld/mysqld.sock' <-----
            ),
        ),

Hope it helps !!

希望它帮助! !

#11


8  

Building on the answer from @dcarrith ...

从@dcarrith的答案上构建…

Instead of editing the config files, I created an alias in the location that PHP is looking that connects to the real mysql.sock. (source)

我没有编辑配置文件,而是在PHP的位置创建了一个别名,它连接到真正的mysql.sock。(源)

Just run these two commands (no restart needed):

只需运行这两个命令(不需要重新启动):

mkdir /var/mysql
ln -s /tmp/mysql.sock /var/mysql/mysql.sock

#12


7  

I'm running on MAMP Pro and had this similar problem when trying to migrate (create db tables). Tried a few of these mentioned suggestions as well but didn't do it for me.

我在MAMP Pro上运行,在尝试迁移时遇到了类似的问题(创建db表)。我也尝试了一些建议,但并没有为我做。

So, simply (after an hour googling), I added two things to the /config/database.php.

因此,简单地(在google上一个小时之后),我在/config/database.php中添加了两项内容。

'port' => '1234',
'unix_socket' => '/path/to/my/socket/mysqld.sock'

Works fine now!

现在没问题了!

#13


2  

As of Laravel 5 the database username and password goes in the .env file that exists in the project directory, e.g.

在Laravel 5中,数据库用户名和密码进入了在项目目录中存在的.env文件中。

DB_HOST=127.0.0.1
DB_DATABASE=db1
DB_USERNAME=user1
DB_PASSWORD=pass1

As you can see these environment variables are overriding the 'forge' strings here so changing them has no effect:

你可以看到,这些环境变量覆盖了“伪造”字符串,所以改变它们是无效的:

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'forge'),
        'username'  => env('DB_USERNAME', 'forge'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

More information is here https://mattstauffer.co/blog/laravel-5.0-environment-detection-and-environment-variables

这里有更多的信息,https://mattstauffer.co/blog/laravel-5.0-environment- detectionand -environment- detectionand -environment-variables。

#14


2  

I ran into this problem when running PHPUnit in Elixir/Gulp, and Homestead as my Vagrant enviroment.

我在Elixir/Gulp中运行PHPUnit时遇到了这个问题,而Homestead是我的流浪环境。

In my case I edited the .env file from DB_HOST=localhost to DB_HOST=192.168.10.10 where 192.168.10.10 is the IP of my Vagrant/Homestead host.

在我的例子中,我从DB_HOST=localhost到DB_HOST=192.168.10.10编辑了.env文件,其中192.168.10.10是我的游子/Homestead主机的IP。

#15


2  

This is because PDO treats "localhost" host specially:

这是因为PDO特别对待“localhost”主机:

Note: Unix only: When the host name is set to "localhost", then the connection to the server is made thru a domain socket. If PDO_MYSQL is compiled against libmysqlclient then the location of the socket file is at libmysqlclient's compiled in location. If PDO_MYSQL is compiled against mysqlnd a default socket can be set thru the pdo_mysql.default_socket setting.

注意:只有Unix:当主机名被设置为“localhost”时,连接到服务器的连接是通过一个域套接字完成的。如果PDO_MYSQL是针对libmysqlclient编译的,那么套接字文件的位置在libmysqlclient编译的位置。如果PDO_MYSQL是针对mysqlnd编译的,那么可以通过PDO_MYSQL .default_socket设置设置默认的套接字。

(from http://php.net/manual/en/ref.pdo-mysql.connection.php)

(来自http://php.net/manual/en/ref.pdo-mysql.connection.php)

Changing localhost to 127.0.0.1 will "force" the use of TCP.

将localhost更改为127.0.0.1将“强制”使用TCP。

Note: mysqli_connect is working fine with localhost.

注意:mysqli_connect在本地主机上运行良好。

#16


1  

If you are using Laravel Homestead, here is settings

如果你正在使用Laravel Homestead,这里是设置。

(include Vagrant-Virtual Machine)

(包括Vagrant-Virtual机)

.bash-profile

.bash-profile

alias vm="ssh vagrant@127.0.0.1 -p 2222"

database.php

database.php

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', '127.0.0.1'),
        'database'  => env('DB_DATABASE', 'homestead'),
        'username'  => env('DB_USERNAME', 'homestead'),
        'password'  => env('DB_PASSWORD', 'secret'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

Terminal

终端

vm

vagrant@homestead:~/Code/projectFolder  php artisan migrate:install

#17


1  

Attempt to connect to localhost:

尝试连接到本地主机:

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

Attempt to connect to 127.0.0.1:

尝试连接到127.0.0.1:

SQLSTATE[HY000] [2002] Connection refused

OK, just comment / remove the following setting from my.cnf (on OS X 10.5: /opt/local/etc/mysqlxx/my.cnf) to obtain:

好的,请在my.cnf (OS X 10.5: /opt/local/etc/mysqlxx/my.cnf)中注释/删除以下设置:

[mysqld]
# skip-networking

Of course, stop and start MySQL Server.

当然,停止并启动MySQL服务器。

#18


1  

If anyone are still looking for the answer, just check your .env file. For some reason laravel create a .env.example file, so all this answers didn't work for me. I fixed my issue renamming .env.example to .env

如果有人还在寻找答案,请检查你的。env文件。由于某种原因,laravel创造了一个。env。示例文件,所以所有这些答案对我都不起作用。我修正了我的问题,renamming .env。例子.env

#19


0  

it's the correct response for me. I disable the option pdo_mysql.default_socket in my php.ini and I design the host at 1.27.0.0.1

这是对我的正确回应。我在php中禁用了pdo_mysql.default_socket选项。我和ini设计的主机为1.27.0.0.1。

#20


0  

When using a VirtualMachine make sure you ssh into that machine and navigate to your App folder and call the php artisan migrate command from there.

当使用虚拟机时,确保您ssh到该机器并导航到您的应用程序文件夹,并从那里调用php artisan迁移命令。

#21


0  

I had problems accessing my drupal website and I had a problem exactly like that I fixed it within less than 2 hours. Try on your command line and restart your mysql server or service. Type this when logged in as root or super user, - service mysqld restart

我在访问drupal网站时遇到了一些问题,我遇到了一个问题,就像我在不到2小时内就把它修好了一样。尝试您的命令行并重新启动mysql服务器或服务。在作为根用户或超级用户登录时键入此命令,-服务mysqld重新启动。

It should work. If it doesn't restart your local webserver, - service httpd restart

它应该工作。如果它没有重启您的本地webserver, -服务httpd重启。

That should be enough. Hope it works for other environments too. Cheers.

这应该足够了。希望它也适用于其他环境。欢呼。

#22


0  

In may case, I'd simply used

在5月份,我只是简单地使用了。

vagrant up

instead of

而不是

homestead up

for my forge larval setup using homestead. I'm assuming this meant the site was getting served, but the MySQL server wasn't ever booted. When I used the latter command to launch my vagrant box, the error went away.

为我的锻造幼虫设置使用宅地。我假设这意味着站点已经被服务了,但是MySQL服务器并没有被引导。当我使用后者的命令来启动我的流浪箱时,错误就消失了。

#23


0  

All these answers seem like heavy lifting...

所有这些答案似乎都是沉重的负担……

I just created a .env file; edited my bootstrap/app.php file, and uncommented the following line...

我刚刚创建了一个。env文件;编辑我的引导/应用程序。php文件,未注释的下面一行…

Dotenv::load(__DIR__.'/../');

Dotenv:加载(__DIR__。“/ . . /”);

Hope this helps someone

希望这可以帮助别人

#24


0  

[This is outdated, database info is stored in .env now] More easier way to solve the issue by is editing the

[这是过时的,数据库信息存储在。env现在]更简单的方式解决问题的是编辑。

app/config/local/database.php

:)

:)

Change the database configuration here and it should work

更改这里的数据库配置,它应该可以工作。

#25


0  

This happened to me because MySQL wasn't running. MySQL was failing to start because I had a missing /usr/local/etc/my.cnf.d/ directory.

这发生在我身上,因为MySQL没有运行。MySQL未能启动,因为我有一个缺失的/usr/local/etc/my.cnf.d/目录。

This was being required by my /usr/local/etc/my.cnf config file as a glob include (include /usr/local/etc/my.cnf.d/*.cnf).

这是我的/usr/local/etc/my.cnf配置文件所要求的,作为一个glob包括(包括/usr/local/etc/my.cnf)。

Running mkdir /usr/local/etc/my.cnf.d, and then starting MySQL, fixed the issue.

运行mkdir /usr/local/etc/my.cnf.d,然后启动MySQL,解决了这个问题。

#26


0  

Check your port carefully . In my case it was 8889 and i am using 8888. change "DB_HOST" from "localhost" to "127.0.0.1" and vice versa

仔细检查你的港口。在我的例子中是8889,我用的是8888。将“DB_HOST”从“localhost”改为“127.0.0.1”,反之亦然。