我在Laravel 5.2应用中使用读写MySQL连接设置:
I'm using read and write MySQL connection setup in my Laravel 5.2 app:
'mysql' => [ 'write' => ['host' => env('DB_HOST_WRITE', 'localhost'),], 'read' => ['host' => env('DB_HOST_READ', 'localhost'),], 'driver' => 'mysql', 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null ]该网站使用负载均衡器在多台服务器上运行.
The website runs on multiple servers, using a load balancer.
现在,在应用程序中,有一些读写操作接连发生的情况,例如
Now, in the app, there are cases of read and write operations done one after another, e.g.
使用当前的连接设置,即使正确插入了记录,选择也不会返回任何内容.
With the current connection setup, it can happen that the select will return nothing, even if the records were inserted correctly.
这可能是什么原因?
推荐答案在Laravel 5.5中,我为此做了一个PR,引入了粘性"选项.
In Laravel 5.5 I've done a PR for this that introduces a "sticky" option.
这意味着您的应用程序将尽可能使用读取"连接,但是如果您执行写入"操作,则同一请求周期上的任何后续读取"也将来自写入连接.
It means your application will use the "read" connection whenever possible, but if you do a "write", then any subsequent "reads" on the same request cycle will also come from the write connection.
这可确保数据完整性.
github/laravel/framework/pull/20445
更多推荐
Laravel读写连接不同步
发布评论