Laravel 4 中的 php artisan 迁移错误 PDOException

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

php artisan migrate error PDOException in Laravel 4

phpmysqllaravel

提问by gon250

I have a problem to execute this:

我有一个问题来执行这个:

$ php artisan migrate

I'm getting an error

我收到一个错误

[PDOException]                             
SQLSTATE[HY000] [2002] Connection refused 

I'm using MAMP and it's working ok.

我正在使用 MAMP,它工作正常。

I have tried to set the localhost to 127.0.0.1 and I've added 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'but nothing helps.

我试图将本地主机设置为 127.0.0.1 并且我已经添加'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'但没有任何帮助。

[Edit] also I've added the port and I have checked the user and the password are correct and the server is running properly.. any suggestion??

[编辑] 我还添加了端口,并检查了用户和密码是否正确并且服务器运行正常.. 有什么建议吗??

Thanks

谢谢

回答by The Alpha

Not sure but make sure your mysql serveris running and it's running on the right port as expected because, Laravel's default port value is 3306(it's the default port of mysqlserver) and most probably mysqlis running on a different port. Here is a discussionand also check this answer, it's almost similar.

不确定,但请确保您mysql server正在运行并且按预期在正确的端口上运行,因为Laravel的默认端口值是3306(它是mysql服务器的默认端口)并且很可能mysql在不同的端口上运行。这是一个讨论,也检查这个答案,它几乎相似。

Also follow this threadwhich discussed and solved the same error you mentioned in your question:

还请遵循此线程该线程讨论并解决了您在问题中提到的相同错误:

[SOLVED] SQLSTATE[HY000] [2002] Connection refused

回答by JrBaconCheez

I know this is an old question, but it came up as the first result in Google when I had the same problem, so I wanted to add my solution here in case it helps someone else.

我知道这是一个老问题,但是当我遇到同样的问题时,它是 Google 中的第一个结果,所以我想在这里添加我的解决方案,以防它对其他人有所帮助。

In MAMP when you click on MySQL, there is a setting to "Allow network access to MySQL" that needs to be enabled for Laravel to access the database (it wasn't enabled by default for me). This allows your application to connect to MySQL without using a Unix socket.

在 MAMP 中,当您单击 MySQL 时,有一个“允许网络访问 MySQL”的设置,需要启用 Laravel 才能访问数据库(默认情况下我没有启用)。这允许您的应用程序在不使用 Unix 套接字的情况下连接到 MySQL。

回答by jeff roussel

change .env file in laravel : add MAMP mysql port

更改 Laravel 中的 .env 文件:添加 MAMP mysql 端口

'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'    => '',
        'port'      => '8889',
        'strict'    => false,
    ],

Jeff

杰夫

回答by ChrisH

Yes, had exactly this problem, with MAMP need to add to config/database 'port' => '8889' (assuming yu are using this port, rather than 80 which can cause conflicts with others apps like Skype!)

是的,确实有这个问题,MAMP 需要添加到配置/数据库 'port' => '8889'(假设你正在使用这个端口,而不是 80,这会导致与其他应用程序(如 Skype)发生冲突!)