1

I had a laravel 7 app I developed on my local (MAMP). I now want to set it up on my website hosting account (apache2, php7.4, mysql server). I did a git pull to pull down all the files and now when I do a

php artisan migrate

I get an error that says

 SQLSTATE[HY000] [2002] No such file or directory (SQL: create table `migrations` (`id` int unsigned not null auto_increment primary key, `migration` varchar(191) not null, `batch` int not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

I am able to login to the phpmyadmin on the hosting server and see my empty database there. My env file looks like this:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=commandcenterdb
DB_USERNAME=****
DB_PASSWORD=******

and my config/database.php file looks like this

    'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE',' commandcenterdb'),
        'username' => env('DB_USERNAME', '*****'),
        'password' => env('DB_PASSWORD', '********'),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => false,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
Jayreis
  • 253
  • 1
  • 7
  • 28
  • Have you already seen [this answer](https://stackoverflow.com/a/33303450)? It's an older Laravel version and they had to change `localhost` to `127.0.0.1` in database.php and env file (where you also have `localhost`). – Jeanne Dark Aug 26 '20 at 14:31
  • Yes I know how to use the search function on StackOverflow :) I tried what was in that post but then I get an error that says Connection refused so I thought maybe since that was for an older version of Laravel thats its not the correct answer for L7? If I make a regular php file that connects via a pdo approach to mysql it works so I know my credentials are correct. – Jayreis Aug 26 '20 at 14:47
  • I had a similar issue, although I am using Docker, I'm not sure if [this is](https://stackoverflow.com/questions/65688042/sqlstatehy000-connection-refused-laravel-with-docker) relevant. – tfantina Jan 12 '21 at 17:02

2 Answers2

3

I got the same error when using MAMP on a mac

SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = php-laravel and table_name = migrations)

I solved it by adding DB_SOCKET on .env file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your-database-name
DB_USERNAME=your-username
DB_PASSWORD=your-password
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

if it doesn't work try to modify the charset and collation on config/database.php file

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
]
Ger
  • 238
  • 4
  • 15
0

its also worth to cross check ,i had UNIX_SOCKET instead of DB_SOCKET in the .env