laravel PHP Artisan 迁移与 MAMP 和 Unix 套接字

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

PHP Artisan Migrate with MAMP and Unix Socket

phplaravellaravel-5mamp-prounix-socket

提问by user3732216

I was developing my application originally in Laravel 4.2 but have since decided to move it to the 5.0 version so that it covers a lot more changes and strengths that 5.0 has over 4.2.

我最初是在 Laravel 4.2 中开发我的应用程序,但后来决定将其移至 5.0 版本,以便它涵盖 5.0 比 4.2 具有的更多更改和优势。

I am trying to run my migratiosn however I am getting the error:

我正在尝试运行迁移,但是出现错误:

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

I looked into this and noticed how it is because I'm running MAMP for my server instead of vagrant and homestead. I'm not knocking the uses of those two but I at this point feel more comfortable with MAMP until it fails me. The reason I know its MAMP is because of needing to declare the unix socket value to be used.

我调查了这个并注意到它是怎么回事,因为我正在为我的服务器运行 MAMP,而不是 vagrant 和 homestead。我并没有否定这两个的用途,但在这一点上我对 MAMP 感觉更舒服,直到它让我失望为止。我知道它的 MAMP 的原因是因为需要声明要使用的 unix 套接字值。

Now on my 4.2 version of my application I have the following:

现在在我的应用程序的 4.2 版本上,我有以下内容:

'mysql' => array(
    'driver'    => 'mysql',
    'unix_socket'   => '/Applications/MAMP/tmp/mysql/mysql.sock',
    'host'      => getenv('DB_HOST'),
    ...
),

With my Laravel 5.0 version I am making use of the .env file for my Environment variables and not sure how I need to do this so that it knows to use the unix socket value.

在我的 Laravel 5.0 版本中,我将 .env 文件用于我的环境变量,但不确定我需要如何执行此操作以便它知道使用 unix 套接字值。

Cans someone clue me into how I should adopt this into the new version or a better way to add it into the settings so that I don't have to do that?

有人能告诉我我应该如何将它应用到新版本中,或者以更好的方式将它添加到设置中,这样我就不必这样做了吗?

回答by Vlad Slobodkin

Try this:

尝试这个:

'mysql' => array(
'driver'    => 'mysql',
'unix_socket'   => getenv('UNIX_SOCKET'),
'host'      => getenv('DB_HOST'),
...
),

In .env add

在 .env 添加

UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

回答by S. M. Ibrahim Lavlu

though quite old question, but still can help others. so adding answer.

虽然很老的问题,但仍然可以帮助别人。所以添加答案。

there is even simple solution. add this to ur .env file

甚至有简单的解决方案。将此添加到您的 .env 文件中

DB_HOST=localhost;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock

回答by DerekOBrien

Thanks, help fix migration issue for me using:

谢谢,使用以下方法帮助我解决迁移问题:

MAMP PRO 4.2
Laravel 5.5

inside .env file:

在 .env 文件中:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=<database name>
DB_USERNAME=<username - default root>
DB_PASSWORD=<password - default root>
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

inside config/database.php:

在 config/database.php 中:

'connections' => [

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', '<database name>'),
        'username' => env('DB_USERNAME', '<username - default root>'),
        'password' => env('DB_PASSWORD', '<password - default root>'),
        'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
],

Also don't forget to add to app/Providers/AppServiceProviders.php:

另外不要忘记添加到 app/Providers/AppServiceProviders.php:

use Illuminate\Support\Facades\Schema;
public function boot()
{
   Schema::defaultStringLength(191);
}

回答by Esteban Bautista

In laravel 5.5 the unix_socket changes to DB_SOCKET

在 laravel 5.5 中 unix_socket 更改为 DB_SOCKET

inside .env file :

在 .env 文件中:

DB_USERNAME=root
DB_PASSWORD=root
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

inside config/database.php:

在 config/database.php 中:

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),

回答by Ralph Ferguson

I created a StackOverflow account solely to answer this question, and maybe help prevent someone going through the pain that I went through.

我创建了一个 StackOverflow 帐户只是为了回答这个问题,也许有助于防止有人经历我所经历的痛苦。

Answers that I found online ranged from changing 127.0.0.1 to localhost, changing the port from 3306 to 33060 and vice-versa, and making sure the unix_socket was correct.

我在网上找到的答案包括将 127.0.0.1 更改为 localhost,将端口从 3306 更改为 33060,反之亦然,以及确保 unix_socket 正确。

The solution that solved my problem was changing:

解决我的问题的解决方案正在改变:

DB_CONNECTION=mysql
DB_HOST=localhost

to

DB_CONNECTION=mysql
DB_HOST=mysql

I hope this helps someone out there. It took me 4 hours to find this painfully obvious solution...and it was found 1min29s into an obscure YouTube videowith under 1000 views.

我希望这可以帮助那里的人。我花了 4 个小时才找到这个令人痛苦的显而易见的解决方案……它在 1 分钟 29 秒的时间内被发现到一个观看次数不足 1000 次的不起眼的 YouTube 视频中