7

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,
        ),
7
  • 1
    Are you certain the database is actually running? How did you confirm it's running? This error indicates it couldn't find any database at all. Commented Feb 2, 2015 at 14:17
  • I have another application that uses the database and it works fine. It doesn't lock the database. Commented Feb 2, 2015 at 14:29
  • 2
    can you please add dd('test'); directly after your connections array in the database file and say if it is hit Commented Feb 2, 2015 at 14:29
  • With a comma instead of a semicolon I do get a string "Test". Otherwise I get (ofc) a syntax errorrrr. Commented Feb 2, 2015 at 14:33
  • @Chilion Are the server details for the other app exactly the same as the config here? Commented Feb 2, 2015 at 15:00

3 Answers 3

20

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_',
    ),
Sign up to request clarification or add additional context in comments.

2 Comments

This worked for me connecting from Eloquent to Google Cloud SQL. The "dsn" value doesn't do much but using the "unix_socket" key worked. Thank you.
This answer should be absolutely positioned to the top.
0

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', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset'     => 'utf8',
    'collation'   => 'utf8_unicode_ci',
    'prefix'      => '',
    'strict'      => false,
],

You can find the documentation there: https://laravel.com/docs/12.x/database#read-and-write-connections
In your .env file, located in the project root, please change the settings you need to adjust and try it again like this.

Example .env file

APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString

DB_SOCKET=/var/run/mysqld4.sock
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 the logs located at storage/logs/laravel.txt and report any errors here.

5 Comments

These is no .env file in my project, also not a hidden file.
If you haven't deleted it, there is one, if still not, just create one, I'll post how it looks like
I don't see why I should create a .env file. The .env file is not needed for Laravel to communicate with a database. It should work from the database.php file
Anyway, try to set the array the way laravel excpects it to be
Its not different from what I have, only strict, I added that, still no luck :()
-14

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

2 Comments

This does not answer the issue regarding "connection to DB using socket". The answer below does.
Since a socket connection is faster this answer is incomplete and misleading. The answer below from @ThievingSix should be marked as correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.