简体   繁体   中英

Laravel 7 SQLSTATE[HY000] [2002] Connection refused

I have a problem with my Laravel project. I started it a few hours ago but I'm stuck with an SQL error:

SQLSTATE[HY000] [2002] Connection refused

I've searched for hours now and I only found it would be wrong password or wrong port but I checked and it's fine for me.

Here's the.env file:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3302
DB_DATABASE=test_technique
DB_USERNAME=root
DB_PASSWORD=

Here's the.env.example file:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3302
DB_DATABASE=test_technique
DB_USERNAME=root
DB_PASSWORD=

And here's the database.php:

'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3302'),
        'database' => env('DB_DATABASE', 'test_technique'),
        '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' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],

And to check the port of mysql:

Test for TCP
Your port 3302 is used by a processus with PID = 2292
The processus of PID 2292 is 'mysqld.exe' Session: Services
The service of PID 2292 for 'mysqld.exe' is 'N/A'
N/A means that there are no service related to PID 2292
Processus 'mysqld.exe' is launched by service 'wampmysqld64' with PID 5048

Test for TCPv6
Your port 3302 is used by a processus with PID = 2292
The processus of PID 2292 is 'mysqld.exe' Session: Services
The service of PID 2292 for 'mysqld.exe' is 'N/A'
N/A means that there are no service related to PID 2292
Processus 'mysqld.exe' is launched by service 'wampmysqld64' with PID 5048

I've never changed the USERNAME and PASSWORD in mysql so I don't know why there's this error

PS: In another site, when I use PDO like this, it works:

new PDO("mysql:host=localhost:3302;dbname=test_technique;charset=utf8", "root", "",array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

The default port for MySQL is 3306 not 3302 you must use like this unless you have changed the port in php.ini

DB_PORT = 3306

Run this command

php artisan config:clear

And instead of using

php artisan serve 

Use

 php -S 127.0.0.1:8000 -t public/

This might help because if you use artisan serve and make changes to env you must kill the serve and run again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM