billy.pc's avatar

Newbie to Laravel, [PDOException] SQLSTATE[HY000]

Hi there, so I'm new to laravel and frameworks in general, I've been following the tutorials on the laracast and everything's been amazing so far, but then when I got to the data migrations video and hit a bump. In the tutorial video, I'm doing a data migration to sqlite, but when i tried to do the migration i kept getting the error [PDOException] could not find driver. Anyways, through some google searching I did sudo apt-get install php5-mysql and now i'm getting a different error when I try migrating, [PDOException] SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111). I'm unsure of how to resolve this. I've tried googling the issue but nothing's resolved it. Here is the default configurations for the database.php file as well. Any help would be appreciated! Thanks!

'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix' => '',
    ],

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],

    'pgsql' => [
        'driver' => 'pgsql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '5432'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'schema' => 'public',
    ],

],
0 likes
4 replies
Jaytee's avatar

This is a Standard PHP/MYSQL error. But, are you using Homestead, Valet or just a local server like WAMP or MAMP?

If you are using homestead then you need to connect via 192.168.10.10. Check your .env file and see what the details are there.

1 like
tykus's avatar

I'm doing a data migration to sqlite

You are getting a MySQL error although you should be expecting to be using a SQLite database - check if your .env file has a key-value pair:

DB_CONNECTION=sqlite

If you are intending to use a SQLite database, make sure that is exists, e.g. the fallback DB_DATABASE in config/database.php is database/database.sqlite

1 like
billy.pc's avatar
billy.pc
OP
Best Answer
Level 1

This is a Standard PHP/MYSQL error. But, are you using Homestead, Valet or just a local server like WAMP or MAMP? If you are using homestead then you need to connect via 192.168.10.10. Check your .env file and see what the details are there.

@Jaytee to be honest i'm unsure how i got the local server up. I just followed the video tutorial and installed it via composer and i was able to serve it up locally.

You are getting a MySQL error although you should be expecting to be using a SQLite database - check if your .env file has a key-value pair: DB_CONNECTION=sqlite If you are intending to use a SQLite database, make sure that is exists, e.g. the fallback DB_DATABASE in config/database.php is database/database.sqlite

@tykus_ikus Turns out I didn't change my .env enough, by default, the .env file had this in it

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

Just changing the DB_CONNECTION to sqlite kicked up another error ([InvalidArgumentException] Database (homestead) does not exist.). So to fix this I completely deleted it and changed it to

DB_CONNECTION=sqlite
DB_FILE=database.sqlite

Thank you guys both for the help!

billy.pc's avatar

**Update: I found the fix! I had to change my database.php in my config. Here's the piece of code I changed

'default' => 'sqlite',

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => database_path('database.sqlite'),
            'prefix' => '',
        ],

So.......I've encountered a new error that has that same error message again. I'm unsure if I should piggy back off this or start a new one (my thought process was it was the same error still in the tutorial video i'm going through). Anyways this time, instead of it saying it in the terminal, after continuing on the laracast video, I've encountered it again but on my local server now.

My Error

I'm trying to get my data to be pulled from the db to $cards, in php tinker, it works fine and I can pull the data. Not sure what codes can help with this but here is my CardsController

<?php

namespace App\Http\Controllers;

use DB;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;


class CardsController extends Controller
{
    public function index() {

        $cards = DB::table('cards')->get();

        return view('cards.index', compact('cards'));
    }
}

and my index.blade.php

@extends('layout')

@section('content')
    <h1>All Cards</h1>

    @foreach ($cards as $card)

        <div> 
            {{ $card->title }} 
        </div>
    @endforeach

@stop 

my .env file

APP_ENV=local
APP_KEY=base64:BUR4ECy7KfWWcgFHiPZcQzg7aTUYTvquHiog5+XCP0o=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=sqlite
DB_FILE=database.sqlite


CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

and my database.php file

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | PDO Fetch Style
    |--------------------------------------------------------------------------
    |
    | By default, database results will be returned as instances of the PHP
    | stdClass object; however, you may desire to retrieve records in an
    | array format for simplicity. Here you can tweak the fetch style.
    |
    */

    'fetch' => PDO::FETCH_CLASS,

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => env('DB_CONNECTION', 'sqlite'),

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer set of commands than a typical key-value systems
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => [

        'cluster' => false,

        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],

];

Please or to participate in this conversation.