douruye5092 2017-02-17 10:48 采纳率: 100%
浏览 81
已采纳

SQLSTATE [HY000] [2002]在尝试将数据写入数据库时​​,Connection拒绝了Laravel

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

enter image description here

I created a file app/blogpost

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class blogpost extends Model
{
    //
}

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

<?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.php and my .env file looks like this:

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))

Do I need to write some code like init or save inside app/blogpost? Can anybody tell me why the connection is refused? I'm using localhost as my project is in development not production.

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:

$ mysql -u idybrand_dave -pMY_PASSWORD -h 181.224.130.159 idybrand_laravel

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

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:

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.

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

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

My .env file:

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.

  • 写回答

2条回答 默认 最新

  • dthhlf1777 2017-02-17 12:05
    关注

    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.

    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).

    So check to see if the user has access from either your remote ip or from anywhere.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)