简体   繁体   English

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

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

so I want to deploy my existing laravel porject into my digitalocean vps I user this<\/a> tut and I upload my site successfully所以我想将我现有的 laravel 项目部署到我的 digitalocean vps 我使用这个<\/a>tut 并成功上传了我的网站

also my env file is我的环境文件也是

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

.env文件中,将DB_HOST127.0.0.1更改为localhost

Put string:放字符串:

DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

after DB_PASSWORD=在 DB_PASSWORD= 之后

It works for MAMP.它适用于 MAMP。 Set your path to mysql.sock将您的路径设置为 mysql.sock

There are a few possibilities why your getting this error.您收到此错误的原因有几种可能。

  • database server isn't started, check if you can still use your phpmyadmin.数据库服务器没有启动,检查你是否仍然可以使用你的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检查 .env 文件和 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.没有对服务器和/或具有给定凭据的数据库的特权查看您已经采取的步骤,我认为这是最后一步。

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 数据库的特定配置。

In your .env file, change DB_HOST from 127.0.0.1 to localhost在 .env 文件中,将 DB_HOST 从 127.0.0.1 更改为 localhost

and then进而

  1. php artisan cache:clear php工匠缓存:清除
  2. php artisan route:clear php工匠路线:清除
  3. php artisan config:clear php工匠配置:清除
  4. php artisan view:clear php工匠视图:清除

    This worked for me.这对我有用。

SELinux might be the cause. SELinux 可能是原因。

Set this rule and try again:设置此规则并重试:

sudo setsebool -P httpd_can_network_connect_db=1

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

My composer was installed globally, i run php artisan serve without starting xampp & getting this error.我的作曲家是全局安装的,我在没有启动 xampp 的情况下运行 php artisan serve 并收到此错误。 After running my xampp server, it's working fine from my side.运行我的 xampp 服务器后,它在我这边工作正常。

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.在 Mac 上,密码也默认为“root”。 Changing my .env file to look as follows fixed my issue:将我的 .env 文件更改为如下所示修复了我的问题:

DB_USERNAME=root
DB_PASSWORD=root

in your .env file在你的 .env 文件中

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ur_db_name
DB_USERNAME=ur_db_username
DB_PASSWORD=ur_dbuser_password

Before you do changes as per instructed above,在按照上述说明进行更改之前,

  1. check if your database server running or not.检查您的数据库服务器是否正在运行。 If you are using command prompt , running mysql.server status or mysqld status shall show the database status.如果您使用command prompt ,运行mysql.server status 或mysqld status 将显示数据库状态。
  2. If the database is running, check if the dbuser is allow to connect from '%' or 'localhost' or '127.0.0.1' .如果数据库正在运行,请检查dbuser是否允许从'%''localhost''127.0.0.1' If not, just grant the user access from '%'如果没有,只需从'%'授予用户访问权限
  3. If you have done 1 and 2 and still can't connect, see if the db user is allow to access the schema.如果您已经完成了 1 和 2 仍然无法连接,请查看是否允许 db 用户访问架构。 If not, grant access to the schema如果没有,请授予对架构的访问权限
  4. If you reach here and still got the same error, try to restart you web server.如果您到达此处仍然遇到相同的错误,请尝试重新启动您的 Web 服务器。

If you are using XAMPP on Mac, Just use the embedded Ip Address from your XAMPP Dashboard as DB_HOST instead of 127.0.0.1 or localhost...如果您在 Mac 上使用 XAMPP,只需使用 XAMPP 仪表板中的嵌入式 IP 地址作为 DB_HOST 而不是 127.0.0.1 或 localhost...

If It still refuses to connect deal with a user but stick on that idea.如果它仍然拒绝与用户联系,但坚持这个想法。

Thank you.谢谢你。

"

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

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