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

时间:2020-08-14 15:24:25

标签: php mysql laravel

我的Laravel项目有问题。我是在几个小时前启动的,但遇到了SQL错误:

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

我已经搜索了几个小时,但我只发现密码或端口错误,但是我检查了一下,对我来说很好。

这是.env文件:

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

这是.env.example文件:

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

这是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'),
        ]) : [],
    ],

并检查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

我从未在mysql中更改过USERNAME和PASSWORD,所以我不知道为什么会出现此错误

P.S:在另一个站点中,当我像这样使用PDO时,它是有效的:

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

1 个答案:

答案 0 :(得分:1)

MySQL的默认端口是3306而不是3302,除非已在php.ini中更改了端口,否则您必须这样使用

using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;

public class MyBuildPostprocessor {
    [PostProcessBuildAttribute(1)]
    public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
         XmlDocument xmlDoc = new XmlDocument();
         xmlDoc.Load($"{pathToBuiltProject}/{PROJECT NAME HERE}/Package.appxmanifest");
         // Find if the `broadFileSystemAccess` capability exists
         // add `broadFileSystemAccess` capability
    }
}

运行此命令

DB_PORT = 3306

而不是使用

php artisan config:clear

使用

php artisan serve 

这可能会有所帮助,因为如果您使用工匠服务并更改env,则必须杀死该服务并再次运行。