skip to Main Content

I have a quite unmodified new Laravel project, and I’m trying to get my first model up and running. I used ./vendor/bin/sail up to run the webserver and MySQL with the default settings. I can access the database from DBeaver, the migrations run fine and I can even access my database data from the terminal using tinker:

$ php artisan tinker
Psy Shell v0.10.12 (PHP 7.4.3 — cli) by Justin Hileman
>>> use AppModelsUser;
>>> User::all()
=> IlluminateDatabaseEloquentCollection {#4290
     all: [
       AppModelsUser {#4292
         id: 1,
         name: "Johannes Molenaar",
         email: "[email protected]",
         email_verified_at: "2022-01-10 13:51:35",
         #password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi",
         #remember_token: "bhuBvEAZ9c",
         created_at: "2022-01-10 13:51:35",
         updated_at: "2022-01-10 13:51:35",
       },
     ],
   }

Now when I try to do any of this in my routes/web.php file (or a controller for that matter), it does not work:

Route::get('/', function() {
    AppModelsUser::all();
});

The error I’m getting is:

IlluminateDatabaseQueryException

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `users`)

And it doesn’t matter what model or ORM function I use, they all give me this same error. I checked my .env config, but I would think that an error in that config would also mean that artisan would be unable to access the database.

.env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tgi_laravel
DB_USERNAME=sail
DB_PASSWORD=password

config/database.php:

'connections' => [
    'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        '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' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],
    // sqlite, pgsql and sqlsrv here
],

Is there anybody who knows what could go wrong here? Let me know if there is any other information that is necessary to assess this problem.


EDIT:

I’m using php 8.1.1 in the web project, but Tinker seems to run on 7.4.3. Changing the php version of the server to 7.4.27 does not resolve this (changed docker image to sail-7.4/app).

Clearing the config cache does not work (php artisan config:clear).

Changing DB_HOST to localhost (instead of 127.0.0.1) changes the error to ‘No such file or directory’.

4

Answers


  1. Chosen as BEST ANSWER

    Changing the DB_HOST to mysql solved the issue.

    The problem was that I was not able to reach the MySQL server from within the container, but I was able to reach it from outside of the container. This explains why DBeaver and Artisan Tinker (run from a commandline outside the container) were able to connect, but my Laravel server was not. Running php artisan tinker from withing the docker container (using docker-compose exec laravel.test bash) also didn't work.

    I cloned a new Laravel project and spotted a difference in the .env configuration:

    DB_CONNECTION=mysql
    DB_HOST=mysql             # Instead of 127.0.0.1
    DB_PORT=3306
    DB_DATABASE=example_app   # And this is a default that didn't
                              # contribute to the problem obviously
    DB_USERNAME=sail
    DB_PASSWORD=password
    

  2. So maybe use localhost instead of 127.0.0.1 on config file

    Login or Signup to reply.
  3. The fact that you’re getting different errors between ‘localhost’ and ‘127.0.0.1’ indicates that the 127.0.0.1 is the correct connection string; it can’t find localhost, but 127.0.0.1 isn’t reliant on any lookups.

    My guess would possibly be that the user ‘sail’ doesn’t have proper permissions in the DB to access that table. I would go into MySQL / Maria, and GRANT ALL PERMISSIONS ON forge.* TO ‘sail’@’localhost’ and try again.

    Also I’m not terribly familiar with sail, but a quick glance indicates besides "sail up" you may have to specify the DB, i.e. DB_DATABASE=forge.

    Looking at your config file, the password for the user is blank. I assume you simply redacted it, but ensure that config file is correct on your dev instance!

    Login or Signup to reply.
  4. The solution is to clear the config cache
    php artisan config:clear
    If clearing config does not work then you need to check if you have php driver for pdo or the connector you are using.

    For example in pdo in ubuntu:

    sudo apt-get install php*-mysql
    

    where * is your active php version like 5.6, 7.4, 7.2, 8.1 etc
    Restart your server i.e sudo service apache2 restart for apache

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search