Laravel Unable to Connect to Database - Migrations - Error 2002
I've searched for a couple of hours and still can't find this.
I am getting 2 errors, if I use the database host like 'localhost'
I get this error:
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
and if i change the host of the database to '127.0.0.1'
i get this error:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
Things I've tried:
- change where apache / mysql server is running (user or `josh (Apache) / josh (MySQL)
- changing the port where MySQL is running in MAMP and placing that port in the mysql array in a file
database.php
- changing the host of the connection from
localhost
to127.0.0.1
and back. - creating a new user in
phpmyadmin
- turn off firewall
Any ideas how to fix this?
source to share
I got it, add this after 'host' => '127.0.0.1'
:
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'
Thus, the connection will 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' => '',
)
source to share
IF you are using a lamp. You can try this:
Open mysql config. (you can open the lamp control panel in / opt / lampp / manager -linux-x64.run, after that open "Configure Mysql Database" or open the file /opt/lampp/etc/my.cnf).
Search and see "socket = / opt / lampp / var / mysql / mysql.sock"
Add 'unix_socket' => '/opt/lampp/var/mysql/mysql.sock' to your database.php file 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,
],
The mysql.sock file depends on where the flashlight is installed. I let you know if you can find it. Good luck.
source to share
I needed 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 the database.php file in the app / config folder and add the following line under "host" => env ("DB_HOST", "localhost")
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock'
This solved my problem, could save someone else's. Note that it is VERY IMPORTANT that you first check the directory containing the mysql.sock file before setting the path.
source to share