php Laravel SQLSTATE[HY000] [2002] 连接被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41225720/
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
Laravel SQLSTATE[HY000] [2002] Connection refused
提问by Mohammad Reza
so I want to deploy my existing laravel porject into my digitalocean vps I user thistut and I upload my site successfully
所以我想将我现有的 Laravel 项目部署到我的 digitalocean vps 我使用这个tut 并且我成功上传了我的网站
also my env file is
我的 env 文件也是
APP_ENV=local
APP_KEY=my app key
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=my ip
DB_PORT=3306
DB_DATABASE=form
DB_USERNAME=root
DB_PASSWORD=my pass
but here is the problem
但问题就在这里
I used mysql and created and ran php artisan migrate
and got these errors
what is the problem
我使用 mysql 并创建并运行php artisan migrate
并得到这些错误是什么问题
[Illuminate\Database\QueryException] SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_s chema.tables where table_schema = form and table_name = migrations)
[Illuminate\Database\QueryException] SQLSTATE[HY000] [2002] 连接被拒绝(SQL: select * from information_s chema.tables where table_schema = form and table_name = migrations)
and
和
[PDOException] SQLSTATE[HY000] [2002] Connection refused
[PDOException] SQLSTATE[HY000] [2002] 连接被拒绝
回答by Arslan Khan
In your .env
file, change DB_HOST
from 127.0.0.1
to localhost
在你的.env
文件,更改DB_HOST
从127.0.0.1
到localhost
回答by Dmitriy Korytnyi
Put string:
放字符串:
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
after DB_PASSWORD=
在 DB_PASSWORD= 之后
It works for MAMP. Set your path to mysql.sock
它适用于 MAMP。将您的路径设置为 mysql.sock
回答by davejal
There are a few possibilities why your getting this error.
您收到此错误的原因有几种可能。
- database server isn't started, check if you can still use your phpmyadmin. If you can still use it then nothing is wrong with your database server.
- wrong config; check username, password and server settings in both your .env file and your config/database.php
- no privileges to the server and or database with given credentials Looking at the steps you already took I think it's the last.
- 数据库服务器没有启动,检查你是否仍然可以使用你的phpmyadmin。如果您仍然可以使用它,那么您的数据库服务器就没有问题。
- 错误的配置;检查 .env 文件和 config/database.php 中的用户名、密码和服务器设置
- 没有对服务器和/或具有给定凭据的数据库的特权查看您已经采取的步骤,我认为这是最后一步。
Check to see if this user has access to the database from the remote location. By default the root user has access to the server locally (localhost,127.0.0.1 and ::1).
检查此用户是否可以从远程位置访问数据库。默认情况下,root 用户可以在本地访问服务器(localhost、127.0.0.1 和 ::1)。
So check to see if the user has access from either your remote ip or from anywhere.
因此,请检查用户是否可以从您的远程 IP 或任何地方访问。
As you are using digitalocean I would suggest to check if they have a specific configuration to connect to mysql database.
当您使用 digitalocean 时,我建议检查他们是否具有连接到 mysql 数据库的特定配置。
回答by Ehsan
SELinux might be the cause.
SELinux 可能是原因。
Set this rule and try again:
设置此规则并重试:
sudo setsebool -P httpd_can_network_connect_db=1
回答by Francy Thacker
In your .env file, change DB_HOST from 127.0.0.1 to localhost
在 .env 文件中,将 DB_HOST 从 127.0.0.1 更改为 localhost
and then
进而
- php artisan cache:clear
- php artisan route:clear
- php artisan config:clear
php artisan view:clear
This worked for me.
- php工匠缓存:清除
- php工匠路线:清除
- php工匠配置:清除
php工匠视图:清除
这对我有用。
回答by Luthermilla Mucula dos Reis
After long hours this worked for me:
经过长时间的工作,这对我有用:
Changing the port in php My Admin previously 8889
更改 php My Admin 中以前的端口为 8889
APP_NAME=laravel
APP_ENV=local
APP_KEY= YOUR KEY
APP_DEBUG=true
APP_URL=127.0.0.1
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
and then
进而
php artisan cache:clear
php artisan route:clear
php artisan config:clear
php artisan view:clear
回答by Al Mamun
My composer was installed globally, i run php artisan serve without starting xampp & getting this error. After running my xampp server, it's working fine from my side.
我的作曲家是全局安装的,我在没有启动 xampp 的情况下运行 php artisan serve 并收到此错误。运行我的 xampp 服务器后,它在我这边工作正常。
回答by SeamusX7
In attempt to provide a solution for those of you that have not found success in any of the already mentioned answers, I will share my solution.
为了为那些在任何已经提到的答案中都没有成功的人提供解决方案,我将分享我的解决方案。
Coming from Windows 10 I was of the understanding that the password for the root user was always blank thus my .env file looked as follows:
从 Windows 10 开始,我了解到 root 用户的密码始终为空,因此我的 .env 文件如下所示:
DB_USERNAME=root
DB_PASSWORD=
On Mac, the password is defaulted to "root" also. Changing my .env file to look as follows fixed my issue:
在 Mac 上,密码也默认为“root”。将我的 .env 文件更改为如下所示修复了我的问题:
DB_USERNAME=root
DB_PASSWORD=root