数据库 Laravel php artisan migrate 连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43804784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:44:38  来源:igfitidea点击:

Database Laravel php artisan migrate connection refused

laravelxamppconnectionartisan

提问by Femke

When I use 'php artisan migrate' I get the following error message:

当我使用“php artisan migrate”时,我收到以下错误消息:

[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations)

[PDOException]
SQLSTATE[HY000] [2002] Connection refused

[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] 连接被拒绝(SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations)

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

I've installed Laravel on a mac with XAMPP and have the following settings:

我已经使用 XAMPP 在 Mac 上安装了 Laravel 并具有以下设置:

database.php

数据库.php

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

.env

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

I've tried several solutions I could find online, but none have worked so far.

我已经尝试了几种可以在网上找到的解决方案,但到目前为止都没有奏效。

回答by Marcial Soto

This is really anoying, but changing DB_HOST=127.0.0.1to DB_HOST=localhostsolves the problem. Give it a try (obviously your file permission must be the right one)

这真的很烦人,但改变DB_HOST=127.0.0.1DB_HOST=localhost解决问题。试一试(显然你的文件权限必须是正确的)

回答by crabbly

First create your database. Read more about it here: http://www.complete-concrete-concise.com/web-tools/creating-a-mysql-database-using-xampp

首先创建您的数据库。在此处阅读更多相关信息:http: //www.complete-concrete-concise.com/web-tools/creating-a-mysql-database-using-xampp

Let's say your new database is named: my_db.

假设您的新数据库名为:my_db。

Use this in your .env:

在您的 .env 中使用它:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=""

回答by philyawj

The solution for me was different than anywhere else I found online.

对我来说,解决方案与我在网上找到的其他任何地方都不同。

I was unknowingly using the VM (virtual machine) version of XAMPP on Mac, which functions differently than the normal version. VM XAMPP interface looks like this.

我在 Mac 上不知不觉地使用了 XAMPP 的 VM(虚拟机)版本,它的功能与普通版本不同。VM XAMPP 界面如下所示。

If you are using the VM XAMPP, uninstall it and install the correct XAMPP version here.

如果您使用的是 VM XAMPP,请在此处卸载它并安装正确的 XAMPP 版本。

Once I installed the new version php artisan migrateworked.

一旦我安装了新版本php artisan migrate就可以了。

回答by Saurabh Mistry

if you are using MAMP then in your .envfile :

如果您使用的是 MAMP,那么在您的.env文件中:

DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=root

回答by Guy

I was using a Vagrant machine to run the whole thing, but I was mistakenly running command on my own machine.Thought this might help someone.

我正在使用 Vagrant 机器来运行整个过程,但我错误地在自己的机器上运行命令。认为这可能对某人有所帮助。

回答by Jan Richter

If someone is experiencing this when using Docker, I had a multi-stage build with first container running the dependency installation and the second one just being the runtime. What I didn't realise is that the installation with Laravel scripts generate a cached config (bootstrap/cache/config.php) which is used instead of the config/database.phpfile.

如果有人在使用Docker时遇到这种情况,我有一个多阶段构建,第一个容器运行依赖项安装,第二个容器只是运行时。我没有意识到的是,使用 Laravel 脚本进行安装会生成一个缓存的配置 ( bootstrap/cache/config.php),而不是config/database.php文件。

Adding the following to the Dockerfile as the last step did the trick:

将以下内容添加到 Dockerfile 作为最后一步就成功了:

RUN php artisan config:clear

回答by YokoH

Hi you don't have DB_SOCKET= /path/to/socketin env.file while you have unix_socket => env('DB_SOCKET', '')in database.php file.

嗨,你没有DB_SOCKET= /path/to/socketenv.file 而你有unix_socket => env('DB_SOCKET', '')database.php 文件。

You could get the /path/to/socket by $ mysql_config --socket

您可以通过以下方式获取 /path/to/socket $ mysql_config --socket

回答by wyz1

This is coming late but it might help someone. I had the same error, it turned out it was a typo in my .env file. Instead of DB_HOST, it was B_HOST. In your case it might be some other env key. Just look closer and you will discover the you have a malformed env file.

这来晚了,但它可能会帮助某人。我有同样的错误,结果是我的 .env 文件中的一个错字。它不是 DB_HOST,而是 B_HOST。在您的情况下,它可能是其他一些 env 键。仔细观察,你会发现你有一个格式错误的 env 文件。