Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mwhitted's avatar

Database problems on new Laravel install

I just installed Laravel and created my first app. Ai am getting this error: Illuminate\Database\QueryException SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select * from sessions where id =…

I think it’s most likely my missing MySQL password, but where in the installation/new app process am I supposed to supply that?

0 likes
5 replies
LaryAI's avatar
Level 58

It sounds like you're encountering a database connection issue in your new Laravel installation. This is typically due to incorrect database configuration settings. You need to provide the correct database credentials in your .env file.

Here’s how you can resolve this issue:

  1. Open your .env file: This file is located in the root directory of your Laravel project.

  2. Update the database configuration: Look for the following lines in the .env file and update them with your MySQL database credentials:

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=your_database_name
    DB_USERNAME=your_database_username
    DB_PASSWORD=your_database_password
    

    Replace your_database_name, your_database_username, and your_database_password with your actual database name, username, and password.

  3. Clear the configuration cache: After updating the .env file, you should clear the configuration cache to ensure Laravel picks up the new settings. Run the following command in your terminal:

    php artisan config:cache
    
  4. Verify MySQL is running: Ensure that your MySQL server is running and accessible. You can check this by trying to connect to your database using a tool like MySQL Workbench or the command line.

  5. Check the database connection: You can test the database connection by running a simple query using Laravel’s tinker:

    php artisan tinker
    

    Then, within the tinker environment, try running a simple query:

    DB::connection()->getPdo();
    

    If everything is set up correctly, this should return a PDO instance without any errors.

By following these steps, you should be able to resolve the database connection issue in your new Laravel installation. If you continue to experience problems, double-check your database credentials and ensure that your MySQL server is properly configured and running.

Mwhitted's avatar

@LaryAI Looks like lots of good information, but since LARAVEL created the database during the new app process, how would I KNOW the database password?

newbie360's avatar

@Mwhitted

but since LARAVEL created the database during the new app process

not 100% true, depends on how you setup the local environment, use Herd / Homestead / Valet / Sail?

Laravel Herd (Free) - not included MySql, you need manually download & install MySql

Laravel Homestead - will create the database when you add the database name in Homestead.yaml

check the .env database settings

and also you need run php artisan migrate before to visit the welcome page

Snapey's avatar

you can run php artisan db:show and it will reveal all the database settings in use.

1 like

Please or to participate in this conversation.