skip to Main Content

In the database migrations section, on Laravel Bootcamp’s Build Chirper with Inertia, after I entered the command:

$ php artisan migrate

it threw an error:

IlluminateDatabaseQueryException

SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = ‘BASE TABLE’)

I searched within Stack Overflow for previous instances of similar issue such as:

From these questions, all of the highest scored answers suggested to edit the DB_HOST in the .env file from 127.0.0.1 to localhost.

However, after I edited my .env file and ran the command:

$ php artisan migrate

it threw a different error:

IlluminateDatabaseQueryException

SQLSTATE[HY000] [2002] No such file or directory (Connection: mysql, SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = ‘BASE TABLE’)

Here’s a snippet of my .env file that might be related to Laravel database migrations:

...

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

...

Plus, some additional info:

  • Laravel: 10.29.0
  • PHP: 8.1.2
  • Composer: 2.6.5
  • Ubuntu: 22.04.3

Thus, why does "php artisan migrate" command resulted in "SQLSTATE[HY000] [2002] Connection refused" error? And what should I do to resolve this problem?

3

Answers


  1. The "Connection refused" happens when Laravel could not connect to a database. This means that the credentials in the .env file are not correct. For example, the password or port may be wrong. Maybe you even forgot to start the MySQL server?

    Since you’re just getting started, maybe it would be simpler to use SQLite as a development database instead of MySQL? Assuming you have all the needed extensions, simply swap out all the DB_* settings for a single DB_CONNECTION=mysql line. Next time you migrate, Laravel will create the needed database file for you. No authentication needed!

    Remember that reading and taking your time to understand the error messages (even the boring parts!) will usually let you know exactly what is wrong.

    Login or Signup to reply.
  2. it seems that your machine (operation system) doesn’t have a database management system. you need to install a DBMS on your machine so that you can use it.
    you can install mysql or postgresql or any other DBMS depends on your need.

    ubuntu docs: https://ubuntu.com/server/docs/databases-mysql (or install LAMP package: https://ubuntu.com/server/docs/lamp-applications )

    similar tutorial: https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-ubuntu-18-04#step-2-installing-mysql

    Login or Signup to reply.
  3. A colleague of mine had the same error. What worked for him was setting "root" as password for the db in the .env file.

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