在 Mac 上设置 Laravel php artisan migrate 错误:没有这样的文件或目录

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

Setting up Laravel on a Mac php artisan migrate error: No such file or directory

phpmysqllaravelmmap

提问by Mwirabua Tim

  1. Pulled a perfectly working laravel project from a git into a mac running MAMP. Project ran perfectly on a linux machine.
  2. composer install
  3. php artisan migrate, got the following error:

    [PDOException]                                    
    SQLSTATE[HY000] [2002] No such file or directory 
    
  1. 将一个完美运行的 laravel 项目从 git 拉到运行 MAMP 的 mac 中。项目在 linux 机器上完美运行。
  2. 作曲家安装
  3. php artisan migrate,得到以下错误:

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

NB: php -v is 5.5 and mysql -v is 5.5 from the terminal Here is part of my config/database.php

注意:来自终端的 php -v 是 5.5 和 mysql -v 是 5.5 这是我的 config/database.php 的一部分

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'essays',
        'username'  => 'root',
        'password'  => 'root',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

I tried replacing localhost with 127.0.0.1 with no avail. Kindly help..

我尝试用 127.0.0.1 替换 localhost 无济于事。请帮助..

Edit:I added these three lines in my php.ini

编辑:我在 php.ini 中添加了这三行

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

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

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

I also added this symlink:

我还添加了这个符号链接:

sudo mkdir /var/mysql
cd /var/mysql && sudo ln -s /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock

But that didnt solve. I also pulled a fresh new laravel project from git and ran into the same error after composer installthen php artisan migrate

但这并没有解决。我也从拉混帐并运行了一个全新的laravel项目到相同的错误后,composer installphp artisan migrate

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

The mac version is 10.7.4

mac 版本是 10.7.4

回答by keithics

If you are using MAMP be sure to add the unix_socketkey with a value of the path that the mysql.sockresides in MAMP.

如果您使用 MAMP,请确保添加unix_socket带有mysql.sockMAMP 中路径值的键。

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

回答by lijinma

Do not assume your unix_socket which would be different from one to another, try to find it.

不要假设您的 unix_socket 会彼此不同,请尝试找到它。

First of all, get your unix_socket location.

首先,获取您的 unix_socket location

$ mysql -uroot -p

Enter your mysql password and login your mysql server from command line.

输入您的 mysql 密码并从命令行登录您的 mysql 服务器。

mysql> show variables like '%sock%';
+---------------+---------------------------------------+
| Variable_name | Value                                 |
+---------------+---------------------------------------+
| socket        | /opt/local/var/run/mysql5/mysqld.sock |
+---------------+---------------------------------------+

Your unix_soket could be diffrent.

您的 unix_soket 可能有所不同。

Then you got 2 solution to solve your issue:

然后你有 2 个解决方案来解决你的问题:

(1) Change your config/database.php

(1) 改变你的 config/database.php

    'mysql' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'essays',
    'username'  => 'root',
    'password'  => 'root',
    'unix_socket'   => '/opt/local/var/run/mysql5/mysqld.sock', //Your sock got from above
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

(2) Change your php.ini, find your php.inifile from

(2) 更改你的 php.ini,php.ini

<? phpinfo();

You maybe install many php with different version, so please don't assume your php.ini file location, get it from your 'phpinfo';

您可能安装了许多不同版本的 php,所以请不要假设您的 php.ini 文件位置,请从您的 'phpinfo' 中获取;

Change your php.ini:

更改您的 php.ini:

mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock

mysqli.default_socket = /opt/local/var/run/mysql5/mysqld.sock

pdo_mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock

Then restart your apache or php-fpm.

然后重新启动您的 apache 或 php-fpm。

回答by Nick

Had the same problem, but it's working for me now.

有同样的问题,但它现在对我有用。

If anybody is still having problems, try this:

如果有人仍然遇到问题,请尝试以下操作:

  • Make sure your bootstrap/start.phpcontains your actual hostname, not the name of your virtual host. Enter hostnamein the terminal to get your hostname. As it's an array I believe you can enter both your hostname and the name(s) of your virtual host(s).
  • Replace "localhost" with "127.0.0.1".
  • 确保您bootstrap/start.php包含您的实际主机名,而不是您的虚拟主机的名称。hostname在终端中输入以获取您的主机名。由于它是一个数组,我相信您可以输入您的主机名和虚拟主机的名称。
  • 将“localhost”替换为“127.0.0.1”。

回答by cristianojeda

If you are using XAMPP, the solution is:

如果您使用的是 XAMPP,解决方案是:

'mysql' => array(
        'driver'      => 'mysql',
        'unix_socket' => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock',
        'host'        => 'localhost'
)

回答by Igor Trindade

This works for me in Laravel 5.0, change the DB_HOST=127.0.0.1:33060in .env file.

这在 Laravel 5.0 中对我有用,更改.env 文件中的DB_HOST=127.0.0.1:33060

Others answers don't work...

其他答案无效...

回答by brianjlennon

For Laravel 5.0+ change localhostto 127.0.0.1in your .env file as well before you go playing around with Unix Sockets etc - this worked for me.

对于 Laravel 5.0+ localhost127.0.0.1在你使用 Unix Sockets 等之前,也要在你的 .env 文件中更改为- 这对我有用。

Noobs beware: For anyone using Laravel 5 and using older learning material be aware that there was quite a marked shift in folder structure from previous versions though this seems to be for the better- check out this article https://mattstauffer.co/blog/laravel-5.0-directory-structure-and-namespace

新手要注意:对于使用 Laravel 5 和使用较旧学习材料的任何人,请注意与以前版本相比,文件夹结构发生了相当大的变化,尽管这似乎更好 - 请查看这篇文章https://mattstauffer.co/blog /laravel-5.0-directory-structure-and-namespace

回答by jai

If you are using Laravle 5.1.11 version + MAC + MAMPP

如果你使用的是 Laravle 5.1.11 版本 + MAC + MAMPP

you have to add "Unix_socket " in file "yourapp"/app/config/database.php

您必须在文件“yourapp”/app/config/database.php 中添加“Unix_socket”

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

Unix_socket param was added to above mysql config drive.

Unix_socket 参数被添加到上面的 mysql 配置驱动器。

回答by sveggiani

Another solution is to add the port number in the host key. In this case MAMP uses the 8889 by default:

另一种解决方案是在主机密钥中添加端口号。在这种情况下,MAMP 默认使用 8889:

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost:8889',
    'database'  => 'essays',
    'username'  => 'root',
    'password'  => 'root',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),