Laravel无法连接到数据库-迁移-错误2002 [英] Laravel Can't Connect to database - Migrations - Error 2002

查看:461
本文介绍了Laravel无法连接到数据库-迁移-错误2002的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了几个小时,但仍然找不到.

I have searched for a couple of hours now, and still am unable to find this.

我收到2个错误,如果我将数据库主机用作'localhost',则会收到此错误:

I get 2 errors, if I use the database host as 'localhost', I get this error:

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

,如果我将数据库主机更改为'127.0.0.1',则会收到此错误:

and if I change the database host to '127.0.0.1' I get this error:

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

我尝试过的事情:

  • 更改apache/mysql服务器的运行位置(用户或`josh(Apache)/josh(MySQL)
  • 更改MySQL在MAMP中运行的端口,并将该端口放在database.php文件中的mysql数组中
  • 将连接的主机从localhost更改为127.0.0.1,然后再更改.
  • phpmyadmin
  • 中创建新用户
  • 关闭防火墙
  • changing where the apache / mysql server is run (either user or `josh (Apache) / josh (MySQL)
  • changing the port that MySQL runs on in MAMP, and putting that port in the mysql array in the database.php file
  • changing the host of the connection from localhost to 127.0.0.1 and back.
  • creating a new user in phpmyadmin
  • turning off the firewall

有什么办法解决这个问题吗?

Any ideas how to fix this?

推荐答案

我想通了,将其添加到'host' => '127.0.0.1'之后:

I figured it out, add this after the 'host' => '127.0.0.1':

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

所以连接看起来像这样:

So the connection would look like this:

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

这篇关于Laravel无法连接到数据库-迁移-错误2002的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆