سؤال

I'm using PHP/Laravel and MySQL databases.

Briefly, I want to use slave databases for reads and master database for writes.

What Laravel config, Laravel package, or database tool do you suggest to handle my issue?

هل كانت مفيدة؟

المحلول 2

To config Laravel to split reads and writes you can edit config/database.php this way:

'connections' => [
    'mysql' => [
        'driver' => 'mysql',
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'read' => [
            'host' => env('DB_READ_HOST', 'localhost'),
            'database' => env('DB_READ_DATABASE', ''),
            'username' => env('DB_READ_USERNAME', ''),
            'password' => env('DB_READ_PASSWORD', ''),
        ],
        'write' => [
            'host' => env('DB_WRITE_HOST', 'localhost'),
            'database' => env('DB_WRITE_DATABASE', ''),
            'username' => env('DB_WRITE_USERNAME', ''),
            'password' => env('DB_WRITE_PASSWORD', ''),
        ],
    ],

    //...
]

As for the point which @rick-james mentioned you can use MySQL Router to balance requests between some databases so you can use both master and slave for reads.

نصائح أخرى

Dangerous. Read about "Critical Read". If you blindly send all SELECTs to a Slave, you could screw up transactions on the master. Or you could miss data when replication is "behind".

Instead, modify your application to connect to either the Master or to some Slave, based on whether it is safe to get the data just from a Slave.

Yes, you can use ProxySQL for this use case. https://proxysql.blogspot.com/2015/09/proxysql-tutorial-setup-in-mysql.html scroll down to the part where it says Read-Write split.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى dba.stackexchange
scroll top