Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,
        ),
like image 355
Chilion Avatar asked Feb 02 '15 14:02

Chilion


People also ask

How check DB connected or not in Laravel?

Echo the Laravel database name in Blade/PHP This will output the name of the database or return 'none' if there is no connection. If you view it in the browser, it gives you the name of the connected database. Checking whether the application is connected to a Laravel database.

How does Laravel connect to database?

Laravel makes connecting with databases and running queries extremely simple. The database configuration file is app/config/database. php . In this file you may define all of your database connections, as well as specify which connection should be used by default.

Which DB to use with Laravel?

Currently, Laravel provides first-party support for five databases: MariaDB 10.3+ (Version Policy) MySQL 5.7+ (Version Policy) PostgreSQL 10.0+ (Version Policy)

Does Laravel close database connection?

It disconnects at the end of The Script. Laravel isn't using persistent connections and they can be bad at times anyway please dig into the PHP manual and there's a in-depth explaination of when and when not to use.


1 Answers

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_',
    ),
like image 174
ThievingSix Avatar answered Sep 17 '22 13:09

ThievingSix