valentin_vranic's avatar

config/database.php Laravel11 -> Laravel12

I have an application, where I connect to multiple db-s. I have In 8 connections 3 where read & write credentials are separately defined, and the rest where only the read were defined and write was left empty.

This worked under laravel 11. After I've upgraded to version 12 I'm getting

SQLSTATE[HY000] [2002] No such file or directory for those connections where the write is left empty like above.

My question is what happened in the upgrade? Does anyone had some similar issues?

And yeah, why don't I write it into single dimension without read, write in this second scenario, even if it works like that? Because I wanted to keep the structure.

Probably I will if there's no other solution.

NOTE: maybe it's not in the upgrade, but on system I use it. On live test server (debian 12) everything seems okay yet. But in my local env, macos under Herd, it got broken.

Thanks in advance,

Val

0 likes
4 replies
Glukinho's avatar

Honestly I don't know what is broken from v11 to v12. But in your case I would try something like:

write => null,
// or
write => [],
// or
write => [ 
    host => [],
],

Setting host name to empty string seems odd to me.

valentin_vranic's avatar

Hm. Firstly, it has to be an array.

If empty array then: Undefined array key "host".

Or in third case, getting this error: Database hosts array is empty.

This is kind of strange a bit to me. And have a feeling that I'm missing something.

Glukinho's avatar

Why you can't just set write credentials same as read? In worst case you get error of wrong privileges, but you don't plan to execute write queries on that database anyway, so no harm there.

1 like
LaryAI's avatar
Level 58

When using Laravel's read/write database configuration (read/write arrays), Laravel expects both sections to contain valid host/username/password settings when present. In Laravel 12, the database connection handling code has become stricter about this: providing an empty 'host' or missing credentials in the write array often leads to connection errors like:

SQLSTATE[HY000] [2002] No such file or directory

This happens because when a write config exists—even if incomplete—Laravel will try to connect using those values.

What changed from Laravel 11 → 12?

  • Laravel 12’s database "read/write splitting" logic is stricter/interprets config more literally.
  • If you define a write key, Laravel fully expects there to be a working/real connection. An empty string as host ('') is not valid, and your OS/PHP MySQL driver will fail to connect.

Solution:

There are two safe ways to solve this:

1. Remove the write array entirely for those connections

If you don't need to split read and write for this connection, just leave out the write key:

'mysql_second' => [
    'driver' => 'mysql',
    'port' => env('DB_PORT', 3306),
    'database' => env('DB_MYSQL_SECOND_DATABASE'),
    'host' => env('DB_MYSQL_SECOND_HOST'),
    'username' => env('DB_MYSQL_SECOND_READ_USERNAME'),
    'password' => env('DB_MYSQL_SECOND_READ_PASSWORD'),
    // Do not include 'read' or 'write' arrays
],

This is the simplest and recommended way.

2. Only set the write array if it’s valid

If you want to keep the structure with read/write keys for all connections, make sure that any write array is fully and correctly configured—no empty hosts allowed.

'mysql_second' => [
    'driver' => 'mysql',
    'port' => env('DB_PORT', 3306),
    'database' => env('DB_MYSQL_SECOND_DATABASE'),
    'read' => [
        'host' => [
            env('DB_MYSQL_SECOND_HOST'),
        ],
        'username' => env('DB_MYSQL_SECOND_READ_USERNAME'),
        'password' => env('DB_MYSQL_SECOND_READ_PASSWORD'),
    ],
    // Only include 'write' if you have valid credentials:
    // 'write' => [
    //     'host' => [
    //         env('DB_MYSQL_SECOND_WRITE_HOST'),
    //     ],
    //     'username' => env('DB_MYSQL_SECOND_WRITE_USERNAME'),
    //     'password' => env('DB_MYSQL_SECOND_WRITE_PASSWORD'),
    // ],
],

3. Conditional Configuration

If you must keep the structure for some reason, conditionally set the write config only when needed:

'mysql_second' => array_filter([
    'driver' => 'mysql',
    'port' => env('DB_PORT', 3306),
    'database' => env('DB_MYSQL_SECOND_DATABASE'),
    'read' => [
        'host' => [env('DB_MYSQL_SECOND_HOST')],
        'username' => env('DB_MYSQL_SECOND_READ_USERNAME'),
        'password' => env('DB_MYSQL_SECOND_READ_PASSWORD'),
    ],
    'write' => env('DB_MYSQL_SECOND_WRITE_HOST') ? [
        'host' => [
            env('DB_MYSQL_SECOND_WRITE_HOST'),
        ],
        'username' => env('DB_MYSQL_SECOND_WRITE_USERNAME'),
        'password' => env('DB_MYSQL_SECOND_WRITE_PASSWORD'),
    ] : null,
], fn ($v) => $v !== null),

(You could use inline logic or config overrides for this.)


Summary:
In Laravel 12, leaving the write array empty with an invalid host (like '') causes a connection failure. You should either remove the write key if not needed, or ensure it has valid, working credentials.

If you want structure and only have one set of credentials, just use the top-level host/username/password keys and remove read/write arrays for that connection.

References:

Please or to participate in this conversation.