问题
I have searched for a couple of hours now, and still am unable to find this.
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
and if I change the database host to '127.0.0.1'
I get this error:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
Things I have tried:
- 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
to127.0.0.1
and back. - creating a new user in
phpmyadmin
- turning off the firewall
Any ideas how to fix this?
回答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' => '',
)
回答2:
IF you use lampp . You can try this:
Open file mysql config. (you can open lampp control panel in /opt/lampp/manager-linux-x64.run,after that open Configure of Mysql Database or open /opt/lampp/etc/my.cnf).
Find and see "socket =/opt/lampp/var/mysql/mysql.sock",
Add 'unix_socket' => '/opt/lampp/var/mysql/mysql.sock' to file database.php in laravel:
'mysql' => [
'driver' => 'mysql',
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'luanvan'),
'username' => env('DB_USERNAME', 'huuthang'),
'password' => env('DB_PASSWORD', '123456'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
File mysql.sock depend on your install location of lampp. I let you see if you can't find it. Good luck
回答3:
I had to do the following in ubuntu 15.10
copy the file or file contents from /opt/lampp/etc/my.cnf to /etc/mysql/my.cnf
Then open database.php file in your app/config folder and add the following line below 'host' => env('DB_HOST', 'localhost')
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock'
That solved my problem, could save someone else's. Please note that it is VERY IMPORTANT you first verify the directory which contains mysql.sock file before configuring the path.
来源:https://stackoverflow.com/questions/27693933/laravel-cant-connect-to-database-migrations-error-2002