Chilion
Chilion

Reputation: 4500

Connection to DB with socket refused in Laravel

I'm aware of a ton of hits on SO and Google about this issue, still I have a unique problem it seems.

Error message: SQLSTATE[HY000] [2002] Connection refused

Configuration

    'mysql' => array(
        'driver'    => 'mysql',
        "host"      => "localhost:/var/run/mysqld4.sock",
        'port'      => '3304',
        "username"  => "admin",
        "password"  => "admin",
        "database"  => "frontend_generic",
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'pre_',
    ),

I confirmed that my database is running on port 3304, my prefix is correct, as are user, database and password.

For host I tried "localhost", "127.0.0.1", "http://127.0.0.1" and even the actual ip-address of the server.

Still, there is no luck or change. I tried using the exact same configuration in the local database.php file, but as expected, nothing changes.

Out of options, what am I missing here?

Update:

This is code from another app that works with this configuration. This is Kohana, not Laravel, but it works.

 "general" => array
        (
            "type"       => "mysql",
            "connection" => array
            (
                "hostname"   => "localhost:/var/run/mysqld4.sock",
                "username"   => "admin",
                "password"   => "admin",
                "persistent" => FALSE,
                "database"   => "frontend_generic",
            ),
            "table_prefix" => "pre_",
            "charset"      => "utf8",
            "caching"      => FALSE,
            "profiling"    => TRUE,
        ),

Upvotes: 7

Views: 20572

Answers (3)

baao
baao

Reputation: 73251

This is what the array should look like in Laravel 5:

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

In your .env file, located in the project root, please change the settings you need to adjust and try it again like this. It's really hard to say what could cause this issue, so just give this a try.

Example .env file

APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file

After creating the file with your credentials, as I commented, please call the file .env and put it in the root of your project. Also make sure to check todays Laravel log, located at storage/logs/laravel-todays_date.txt and report any errors here.

Upvotes: -1

ThievingSix
ThievingSix

Reputation: 1064

When using sockets with laravel 5, use the unix_socket config instead of host and port.

While changing to IP addresses does work, there can be advantages to using sockets when the database server is on the same machine.

Correcting the configuration originally posted:

    'mysql' => array(
        'driver'           => 'mysql',
        "unix_socket"      => "/var/run/mysqld4.sock",
        "username"         => "admin",
        "password"         => "admin",
        "database"         => "frontend_generic",
        'charset'          => 'utf8',
        'collation'        => 'utf8_unicode_ci',
        'prefix'           => 'pre_',
    ),

Upvotes: 19

Bopp
Bopp

Reputation: 664

You should change "host" to the actual server IP adres, and nothing else. So, get rid of the socket stuff.

Upvotes: -12

Related Questions