Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Anoud's avatar
Level 1

SQLSTATE[HY000] [2002] Connection refused

Hi,

I'm trying to insert the form to the database, but I keep have this error:

SQLSTATE[HY000] [2002] Connection refused (SQL: insert into `appointments` (`title`, `content`, `slug`, `updated_at`, `created_at`) values (First Appointment, This is a test, 5c229be35faa2, 2018-12-25 21:06:43, 2018-12-25 21:06:43))

I just added these steps:

First I added this line above the class name inside this file " AppointmentController.php":

use App\Appointment;

Also added this part inside function store:

public function store(AppointmentFormRequest $request)
    {
      $slug = uniqid();
      $appointment = new Appointment(array(
        'title' => $request->get('title'),
        'content' => $request->get('content'),
        'slug' => $slug
      ));

      $appointment->save();

      return redirect('/contact')->with('status', 'Your appointment has been created! Its unique id is: '.$slug);
    }

This is the entire code for this page:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\AppointmentFormRequest;
use App\Appointment;
class AppointmentsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('appointments.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AppointmentFormRequest $request)
    {
      $slug = uniqid();
      $appointment = new Appointment(array(
        'title' => $request->get('title'),
        'content' => $request->get('content'),
        'slug' => $slug
      ));

      $appointment->save();

      return redirect('/contact')->with('status', 'Your appointment has been created! Its unique id is: '.$slug);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

Also, I changed the Appointment Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Appointment extends Model
{
    protected $fillable = ['title', 'content', 'slug', 'status', 'user_id'];
}

This is mysql inside datavase.php

  'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            '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' => '',
            'prefix_indexes' => true,
            'strict' => false,
            'engine' => null,
        ],

this is the .env file:

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

And I'm using Sequel Pro, and I logged in I can see all my tables but I can't insert anything.

I was following a tutorial from Chapter 3 of this book: Learning Laravel 5: https://learninglaravel.net/books/laravel/building-a-support-ticket-system On how to create ticket systems. everything before this section "Insert data into the database" worked for me but I struggle with the connection.

This is the full error:

Illuminate \ Database \ QueryException (2002)
SQLSTATE[HY000] [2002] Connection refused (SQL: insert into `appointments` (`title`, `content`, `slug`, `updated_at`, `created_at`) values (First Appointment, This is a test to ckeck, 5c22b4e03f687, 2018-12-25 22:53:20, 2018-12-25 22:53:20))

Myname/code/appointmentSystems/vendor/laravel/framework/src/Illuminate/Database/Connection.php
     * @param  array     $bindings
     * @param  \Closure  $callback
     * @return mixed
     *
     * @throws \Illuminate\Database\QueryException
     */
    protected function runQueryCallback($query, $bindings, Closure $callback)
    {
        // To execute the statement, we'll simply call the callback, which will actually
        // run the SQL against the PDO connection. Then we can calculate the time it
        // took to execute and log the query SQL, bindings and time in our memory.
        try {
            $result = $callback($query, $bindings);
        }
 
        // If an exception occurs when attempting to run a query, we'll format the error
        // message to include the bindings with SQL, which will make this exception a
        // lot more helpful to the developer instead of just the database's errors.
        catch (Exception $e) {
            throw new QueryException(
                $query, $this->prepareBindings($bindings), $e
            );
        }
 
        return $result;
    }
 
    /**
     * Log a query in the connection's query log.
     *
     * @param  string  $query
     * @param  array   $bindings
0 likes
11 replies
realrandyallen's avatar

When you login with Sequel Pro do you use the same credentials that you’re seeing in your .env file? Username and password homestead/secret?

Anoud's avatar
Level 1

@REALRANDYALLEN - Yes, I logged with Sequel Pro with this details as the tutorial suggested.

Name: homestead
Host: 127.0.0.1
Username: homestead
Password: secret
Port: 33060

I can see all the tables that I created before. I used this command to create a table and I can see my table on my database in Sequel Pro

php artisan make:migration create_ appointments_table
Anoud's avatar
Level 1

@JLRDW - On my code, I used 3306 but with Sequel Pro, I can't use 3306 I need to use 33060.

jlrdw's avatar

Try changing to

DB_HOST=localhost

then do a php artisan config:clear

munazzil's avatar

Port should be 3306 and you should let the hostname to localhost and check.

      'host' => env('DB_HOST', 'localhost'),
Anoud's avatar
Level 1

@JLRDW - I tried that; all my tables will disappear from my homestead database on Sequel Pro. And on my website, I will have the same error.

Anoud's avatar
Anoud
OP
Best Answer
Level 1

@MUNAZZIL - Yes, I change the PC, and I instal Valet instead of the homestead. I used the same code and it's working now. Thank you

Please or to participate in this conversation.