Laravel读写连接不同步

编程入门 行业动态 更新时间:2024-10-25 06:25:52
本文介绍了Laravel读写连接不同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在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读写连接不同步

    本文发布于:2023-10-26 19:09:46,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1531089.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:不同步   Laravel

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!