SQLSTATE[HY000] [2002] 尝试将数据写入数据库时​​拒绝与 Laravel 的连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42295955/
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-14 15:20:58  来源:igfitidea点击:

SQLSTATE[HY000] [2002] Connection refused with Laravel when attempting to write data to the database

phpmysqlwordpresslaravel

提问by Dave Chambers

I have been using the Laravel package for the Wordpress JSON REST APIand it works well. I want to store my blogposts in my database. I used phpMyAdminto add a table named idybrand_laravel

我一直在为 Wordpress JSON REST API使用Laravel 包,它运行良好。我想将我的博客文章存储在我的数据库中。我曾经phpMyAdmin添加一个名为的表idybrand_laravel

enter image description here

在此处输入图片说明

I created a file app/blogpost

我创建了一个文件 app/blogpost

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class blogpost extends Model
{
    //
}

and for now in my BlogControllerI am trying a simple test, wishing to store data in just 3 fields of the table

现在BlogController我正在尝试一个简单的测试,希望将数据存储在表的 3 个字段中

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use WpApi;

use App\blogpost;

use Carbon\Carbon;

class BlogController extends Controller
{
  public function getPosts()
  {

    foreach (WpApi::posts() as $post) {
      $this->createPost($post);
    }
    //return view('pages.blog', ['active'=>'navBlog'])->with('posts',     WpApi::posts());
  }

  protected function createPost($data)
  {
    $post = new blogpost();
    $post->id = $post['id'];          // integer
    $post->wp_id = $post['id'];      // integer
    $post->link = $post['link'];          //string
    $post->save();

    return $post;
  }

}

I have not changed config/database.phpand my .envfile looks like this:

我没有改变config/database.php,我的.env文件看起来像这样:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=idybrand_laravel
DB_USERNAME=idybrand_dave
DB_PASSWORD=myPassword

The error I receive is:

我收到的错误是:

QueryException in Connection.php line 770: SQLSTATE[HY000] [2002] Connection refused (SQL: insert intoblogposts(id,wp_id,link,updated_at,created_at) values (, , , 2017-02-17 10:21:57, 2017-02-17 10:21:57))

QueryException in Connection.php line 770: SQLSTATE[HY000] [2002] Connection refused (SQL: insert into博客文章(id ,wp_id,链接,updated_at ,created_at) values (, , , 2017-02-17 10:21:57, 2017-02-17 10:21:57))

Do I need to write some code like initor saveinside app/blogpost? Can anybody tell me why the connection is refused? I'm using localhostas my project is in development not production.

我需要写一些类似initsave里面的代码app/blogpost吗?谁能告诉我为什么连接被拒绝?我正在使用localhost我的项目是在开发而不是生产中。

EDIT:

编辑:

After some help from the guys in chat including davejal, I am now able to connect to the server BOTH via the command line in OS X:

在包括 davejal 在内的聊天人员的帮助下,我现在可以通过 OS X 中的命令行连接到服务器:

$ mysql -u idybrand_dave -pMY_PASSWORD -h 181.224.130.159 idybrand_laravel

AND using Sequel Prowith no problem. But I still get the same error in Laravel when trying to connect using those exact same settings in my .envfile:

并且使用Sequel Pro没有问题。但是当我尝试在我的.env文件中使用完全相同的设置进行连接时,我仍然在 Laravel 中遇到相同的错误:

DB_CONNECTION=mysql
DB_HOST=181.224.130.159 //Siteground IP Address
DB_PORT=3306
DB_DATABASE=idybrand_laravel
DB_USERNAME=idybrand_dave
DB_PASSWORD=MY_PASSWORD

I also tried to edit my model as suggested by Sona but it doesn't help:

我还尝试按照 Sona 的建议编辑我的模型,但没有帮助:

class blogpost extends Model
{
    protected $table = 'blogposts';
    protected $fillable = array('id', 'wp_id', 'link', 'title', 'author_code', 'content', 'created_at', 'updated_at');
}

Can anybody please shed any light on this error?

任何人都可以对这个错误有所了解吗?

FINAL EDIT:

最终编辑:

I had a fundamental misunderstanding in that I thought I could use a localhost 'local' development environment to write data to a table that exists on the server (Siteground) in the 'production' environment.

我有一个根本的误解,因为我认为我可以使用本地主机“本地”开发环境将数据写入“生产”环境中服务器(Siteground)上存在的表。

Once I read the Laravel Database Getting Startedand watched these threevideos by Mindspace all became clear. Basically, I needed a server environment running locally and this is provided by Vagrantand VMWare Fusion.

一旦我阅读了Laravel 数据库入门并观看了 Mindspace 的这三个视频,一切就变得清晰了。基本上,我需要一个在本地运行的服务器环境,这是由VagrantVMWare Fusion 提供的

After setting it up I could access http://idy.app/in the browser whilst the homestead-7 VMwas running and write to the database.

设置好后,我可以http://idy.app/在浏览器中访问,同时homestead-7 VM运行并写入数据库。

My .envfile:

我的.env文件:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=33060
DB_DATABASE=idy
DB_USERNAME=homestead
DB_PASSWORD=secret

I have accepted davejal's answer as it lead me to the solution.

我接受了 davejal 的回答,因为它引导我找到解决方案。

回答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
  • 数据库服务器没有启动,检查你是否仍然可以使用你的phpmyadmin。如果您仍然可以使用它,那么您的数据库服务器就没有问题。
  • 错误的配置;检查 .env 文件和 config/database.php 中的用户名、密码和服务器设置
  • 没有给定凭据的服务器和/或数据库权限

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 或任何地方访问。

回答by zEELz

I also encountered the "SQLSTATE[HY000] [2002] Connection refused" error message, all I needed to do was change MySQL Port Number in .envand database.phpfiles to match the one on MAMP

我还遇到了“SQLSTATE[HY000] [2002] 连接被拒绝”错误消息,我需要做的就是更改 MySQL 端口号.envdatabase.php文件以匹配 MAMP 上的端口号

DB_PORT=3306
'port' => env('DB_PORT', '3306')

I changed both to the port number configured for MySQL in MAMP.

我将两者都更改为在 MAMP 中为 MySQL 配置的端口号。