简体   繁体   English

Laravel 7 SQLSTATE[HY000] [2002] 连接被拒绝

[英]Laravel 7 SQLSTATE[HY000] [2002] Connection refused

I have a problem with my Laravel project.我的 Laravel 项目有问题。 I started it a few hours ago but I'm stuck with an SQL error:我是几个小时前开始的,但我遇到了 SQL 错误:

SQLSTATE[HY000] [2002] Connection refused SQLSTATE[HY000] [2002] 连接被拒绝

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:这是 .env 文件:

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

Here's the.env.example file:这是 .env.example 文件:

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

And here's the database.php:这是数据库。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:并检查 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我从未更改过 mysql 中的 USERNAME 和 PASSWORD 所以我不知道为什么会出现这个错误

PS: In another site, when I use PDO like this, it works: PS:在另一个站点中,当我像这样使用 PDO 时,它可以工作:

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 MySQL 的默认端口是 3306 而不是 3302 您必须像这样使用,除非您在 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.这可能会有所帮助,因为如果您使用 artisan serve 并对 env 进行更改,您必须终止服务并再次运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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