Maha Dev
Maha Dev

Reputation: 3965

Laravel 5.7 SQLSTATE[HY000] [2002] No connection could be made

I am using laravel 5.7 and i am getting this error. But its working fine for older versions. i am using xamp with php 7.3.* . Here is my code

.env

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3308
DB_DATABASE=project
DB_USERNAME=root
DB_PASSWORD=
DB_DEFAULT=mysql

database.php

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3308'),
            'database' => env('DB_DATABASE', 'project'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => false,
            'engine' => null,
            'default' => env('DB_DEFAULT', 'mysql')
        ],

Database is fine and i tried connecting with test.php file, its connecting fine but have problem with laravel 5.7

enter image description here

Upvotes: 0

Views: 207

Answers (3)

Travis Britz
Travis Britz

Reputation: 5552

This error usually means the target machine was reachable, but is not listening on that port.

Some things to check:

  • The default port for MySQL is 3306, not 3308. Was this change intentional?
  • XAMPP often requires that you start the database manually. Is it online?
  • If the database is on your local machine, is it also configured to listen on 3308 instead of 3306 like your Laravel code?
  • Make sure your config isn't cached: php artisan config:clear

If the database is not on your local machine, then it could easily be a firewall setting causing the problem.

Upvotes: 2

Maha Dev
Maha Dev

Reputation: 3965

After a lot of research about laravel 5.7 and xamp, i found the solution. It was basically the cache issue in the file bootstrap/cache/config.php .

It was reading this config cache file instead of my new file. I removed it and not its working fine. I am very thankful all of you.

Upvotes: 0

Golam Sorwar
Golam Sorwar

Reputation: 1615

XAMPP to check your PORT number then change DB_PORT. I hope it works

enter image description here

enter image description here

Upvotes: 0

Related Questions