Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 cannot migrate the database

Im using MAMP for the web server:

Below is my code config/database.php

'mysql' => [
            'driver'    => 'mysql',
            'unix_socket'   => '/Application/MAMP/tmp/mysql/mysql.sock',
            'host'      => env('DB_HOST'),
            'database'  => env('DB_DATABASE'),
            'username'  => env('DB_USERNAME'),
            'password'  => env('DB_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

.env

APP_ENV=local
APP_DEBUG=true
APP_KEY=dfsfsdffsd

DB_HOST=localhost
DB_DATABASE=my_database
DB_USERNAME=my_db_username
DB_PASSWORD=my_db_password

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

I got this error when run php artisan migrate:

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

What could possibly wrong? thanks in advance

like image 657
Kris MP Avatar asked Apr 26 '26 10:04

Kris MP


1 Answers

First try to change your command to this

php artisan migrate --env=local

If that did not work make sure MySQL is up and running and make sure it is using the default port 3306.

Additionally, try to connect to MySQL from the command line to make sure you can connect to MySQL. Open up a command line the try to connect like this

mysql -hlocalhost -uroot -p

and see if you are able to connect. If that does not work you may want to change localhost to 127.0.0.1 or update your hosts file with this line

127.0.0.1 localhost
127.0.0.1 127.0.0.1

I would also add this line to your .env file

DB_CONNECTION=mysql

if all the above fails I would say your socket value is not setup correctly.

try changing your socket values from

/Application/MAMP/tmp/mysql/mysql.sock

to

/Applications/MAMP/tmp/mysql/mysql.sock

You may consider creating a php.ini file for PHP

open up php.ini file and look for pdo_mysql.default_socket and set is as:

pdo_mysql.default_socket=/Applications/MAMP/tmp/mysql/mysql.sock
like image 128
Jaylen Avatar answered Apr 28 '26 05:04

Jaylen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!