简体   繁体   English

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

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

I am new in Laravel development.我是 Laravel 开发的新手。 I have updated Xampp to 7.3.11 on my Mac Mojave 10.14.6.我已将我的 Mac Mojave 10.14.6 上的 Xampp 更新为 7.3.11。 In Laravel project when I hit php artisan migrate command I got following error.在 Laravel 项目中,当我点击 php artisan migrate 命令时,出现以下错误。

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE') SQLSTATE[HY000] [2002] 连接被拒绝(SQL:select * 来自 information_schema.tables,其中 table_schema = laravel 和 table_name = migrations 和 table_type = 'BASE TABLE')

When I start Xampp service, my admin panel run on http://127.0.0.1:8080/phpmyadmin .当我启动 Xampp 服务时,我的管理面板在http://127.0.0.1:8080/phpmyadmin上运行。 My working project in Laravel is also not connecting with database saying connection refused.我在 Laravel 的工作项目也没有连接数据库,说连接被拒绝。 I tried by changing DB_Port and DB_Host in.env file.我尝试通过更改DB_PortDB_Host in.env 文件。 I tried by clearing cache.我尝试通过清除缓存。

Any Help will be appreciated.任何帮助将不胜感激。

My .env file我的.env文件

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

Open localhost/phpmyadmin and find a tab called User accounts .打开localhost/phpmyadmin并找到一个名为User accounts的选项卡。

Find the root user and set its password in your .env and also don't forget to create the database named laravel if it doesn't exist找到root用户并在.env中设置其密码,如果数据库不存在,请不要忘记创建名为laravel的数据库

Then you can clear config cache然后你可以清除配置缓存

php artisan config:clear

And migrate并迁移

php artisan migrate

I've come across this Error too by building up a new project with Laravel running in docker-compose for development.通过在 docker-compose 中运行 Laravel 构建一个新项目,我也遇到了这个错误。

My solution was to compare the prebuild.env-File with the actual credentials I used for building the database container.我的解决方案是将 prebuild.env-File 与我用于构建数据库容器的实际凭据进行比较。 Especially I was using DB_HOST=127.0.0.1 instead of the correct service name of my docker-compose setup: DB_HOST=mysql特别是我使用DB_HOST=127.0.0.1而不是我的 docker-compose 设置的正确服务名称: DB_HOST=mysql

Just simple step i follow and solved我遵循并解决的只是简单的步骤

open.env file打开.env 文件

change DB_HOST = 127.0.0.1 to localhost将 DB_HOST = 127.0.0.1 更改为 localhost

done完毕

I had the same issue.我遇到过同样的问题。 It was resolved by just restarting the local host server ie Xampp只需重新启动本地主机服务器即可解决,即 Xampp

I had the same problem and when I applied the following operations, the problem was solved.我遇到了同样的问题,当我应用以下操作时,问题就解决了。

    php artisan key:generate
    php artisan cache:clear
    php artisan route:clear
    php artisan config:clear 
    php artisan view:clear 

In most cases it's an issue with the port configuration.在大多数情况下,这是端口配置的问题。 You need to check which port your server is running on.您需要检查您的服务器在哪个端口上运行。 Then you have to edit the .env file of your project.然后您必须编辑项目的.env文件。 For instance if you are using mamp the port should be changed to 8889 instead of the default 3606 for laravel.例如,如果您使用 mamp,则应将端口更改为 8889,而不是 laravel 的默认 3606。 The same applies to laravel 8.这同样适用于 laravel 8。

Try out doing尝试做

php artisan config:cache php 工匠配置:缓存

then on your.env file specify your database and your port然后在 your.env 文件中指定您的数据库和端口

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE= YOUR_DATABASE_NAME
DB_USERNAME=root
DB_PASSWORD=

then run your mysql server and run然后运行您的 mysql 服务器并运行

php artisan migrate php 工匠迁移

Note: Make sure your Xammp server is perfectly fine & have already started mysql (using apache server or any server you are using) and also make sure you already created your database before running php artisan migrate Hope it works for you注意:确保您的 Xammp 服务器完全正常并且已经启动 mysql(使用 apache 服务器或您正在使用的任何服务器),并确保您在运行 ZE1BFD762321E409CEE4AC0B6E8419 之前已经创建了数据库希望它适用于您

I had this issue, I use MAMP for MYSQL server.我遇到了这个问题,我将 MAMP 用于 MYSQL 服务器。 It was a configuration issue that I solved by changing the port to the correct one I found in MAMP instructions, which is 8889 .这是一个配置问题,我通过将端口更改为我在 MAMP 指令中找到的正确端口来解决,即8889 So the correct configuration in my .env file is:所以我的.env文件中的正确配置是:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=dbname
DB_USERNAME=root
DB_PASSWORD=rootpassword

Try this conf in.env (i'm using Laravel v7.0), it's working for me:试试这个 conf in.env(我使用的是 Laravel v7.0),它对我有用:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=root

Damn, I feel so stupid lol.该死的,我觉得自己很愚蠢哈哈。 This is what helped me after scratching my head for hours.这就是在挠头几个小时后帮助我的原因。 Check what port your MySQL is running on and that's the one you use in your.env file.检查您的 MySQL 在哪个端口上运行,这就是您在.env 文件中使用的端口。

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=laravel
DB_USERNAME=yourusername
DB_PASSWORD=yourpassword

Make sure when you create your user that you grant all privileges for the user.确保在创建用户时授予用户所有权限。 If you're still confused then ask me and ill find time to help you out.如果您仍然感到困惑,请询问我,我会抽空帮助您。 Happy coding everyone!祝大家编码愉快!

I was using docker compose file.我正在使用 docker 撰写文件。 All you need to do is to expose the port of the mysql您需要做的就是暴露mysql的端口

 db: image: mysql command: --default-authentication-plugin=mysql_native_password restart: always environment: MYSQL_ROOT_PASSWORD: example MYSQL_DATABASE: example MYSQL_USER: example MYSQL_PASSWORD: example ports: - 3306:3306 adminer: image: adminer restart: always ports: - 8080:8080

It was actually this in my case.在我的情况下实际上是这样。

DB_HOST=127.0.0.1
DB_PORT=33060

Before it was之前

DB_HOST=127.0.0.1
DB_PORT=3306

Many solutions were suggesting to change DB_HOST to "localhost".许多解决方案建议将DB_HOST更改为“localhost”。 In my case it was just a tiny little 0 in DB_PORT .在我的情况下,它只是DB_PORT中的一个很小的0

I had the same issue while running laravel and mysql within a docker container (MacOs).我在 docker 容器(MacOs)中运行 laravel 和 mysql 时遇到了同样的问题。
I figured out that the problem was within the.env file.我发现问题出在 .env 文件中。

the default configuration in.env was: in.env 的默认配置是:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_db_name
DB_USERNAME=sail
DB_PASSWORD=password

Some answers suggested to change DB_HOST to:一些答案建议将 DB_HOST 更改为:

DB_HOST = localhost

But it didn't work for me...但这对我不起作用...

After some research I found out that, while running laravel in docker, the DB_HOST expects the database service that is running in docker too, in our case the service is mysql. After some research I found out that, while running laravel in docker, the DB_HOST expects the database service that is running in docker too, in our case the service is mysql.


So I had to change the.env to this:所以我不得不把.env改成这样:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=my_db_name
DB_USERNAME=sail
DB_PASSWORD=password

I also changed the DB_HOST in config/database.php我还更改了 config/database.php 中的 DB_HOST

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'mysql'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'my_db_name'),
            'username' => env('DB_USERNAME', 'sail'),
            'password' => env('DB_PASSWORD', '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'),
            ]) : [],
        ],

It's only then that everything started working fine.只有到那时,一切才开始正常工作。

I'm using laravel 8 and mysql with docker, i had the same problem i tried several solutions and none worked, i deleted my containers and volumes and nothing too, so i decided to restart my os and go into incognito mode and boomm it went smoothly so i went back for my tab that was giving error and I deleted the session cookies and boom the problem was solved, another tip is to use the database container IP using docker inspect [container--database-id] instead of DB_HOST=localhost. I'm using laravel 8 and mysql with docker, i had the same problem i tried several solutions and none worked, i deleted my containers and volumes and nothing too, so i decided to restart my os and go into incognito mode and boomm it went smoothly so i went back for my tab that was giving error and I deleted the session cookies and boom the problem was solved, another tip is to use the database container IP using docker inspect [container--database-id] instead of DB_HOST=localhost . Sorry for the writing, I don't speak English, I hope I helped.对不起,我不会说英语,希望对我有所帮助。

Verify port:验证端口:

'${FORWARD_DB_PORT:-3306}:3306'

In my case ( Windowsx64 + WSL + php/laravel8.1 ) I was able to add data source via PhPStorm/PyCharm using valid credentials but php artisan migrate ended up with SQLSTATE[HY000] [2002] Connection refused error every time.在我的例子中( Windowsx64 + WSL + php/laravel8.1 )我能够使用有效的凭据通过 PhPStorm/PyCharm 添加数据源,但是php artisan migrate都以SQLSTATE[HY000] [2002] Connection refused错误结束。

I had to stop MySql service from XAMPP control panel and setup DB and USER via mysql-cli我不得不从 XAMPP 控制面板停止MySql服务,并通过mysql-cli设置数据库和用户

after stopping services from XAMPP, do sudo service mysql start and then:从 XAMPP 停止服务后,执行sudo service mysql start然后:

sudo mysql -u root

SHOW DATABASES;
CREATE USER 'elite'@'localhost' IDENTIFIED BY 'elite';
CREATE DATABASE laravel
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, CREATE 
TEMPORARY TABLES, LOCK TABLES ON laravel.* TO 'elite'@'localhost';
exit

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

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