docker中的数据库连接超时

环境</ strong> </ p>


  • 环境:Mac和Docker </ li>
  • 语言:GoLang </ li>
  • 数据库:Postgresql </ li>
    </ ul>

    预期 行为</ strong> </ p>

    我们有一个用于连接数据库的连接池。 数据库连接的时间可能在1分钟到20分钟之间,具体取决于所调用的函数。</ p>

    实际行为</ strong> </ p>

    < ul>

  • 在本地运行:一切都很好。</ li>
  • 在Docker中运行:少于5分钟的数据库连接就可以了。 对于超过5分钟的数据库连接,每5-6分钟在docker中断开连接,并自动启动一个新连接。 3次重新连接后,将返回以下错误并终止进程</ p>

    驱动程序:连接错误</ p> </ li>
    </ ul>

    某些尝试</ strong> </ p>


    • 在函数调用期间尝试ping数据库以保持连接
      alive-不起作用(在某些简单情况下有效,但在
      情况复杂且不稳定。</ li>
    • 尝试使用
      db.SetMaxOpenConns(1)和db.SetMaxIdleConns(1)在具有相同连接的函数调用期间对数据库进行ping操作。</ li>

    • 尝试使用db.SetConnMaxLifetime()设置更长的连接寿命-
      无效。</ li>
      </ ul>

      某些信息</ strong> </ p>

      进行一些搜索后,可能的原因是docker会定期检查连接,并在连接存活300秒且没有响应返回时将其超时。找不到任何方法来覆盖此配置。 / p>

      https://github.com/moby/moby/ Issues / 36053 </ p>

      ”> https://github.com/docker/for-mac/issues/2442 </ p>

      如何解决此问题? 感谢您的帮助。</ strong> </ p>
      </ div>

展开原文

原文

Environment

  • Environment: Mac and Docker
  • Language: GoLang
  • Database: Postgresql

Expected Behavior

We have a connection pool used to connect to database. The time of database connection may vary from 1 minute up to 20 minutes depending on the functions being called.

Actual Behavior

  • Running in local: everything is fine.
  • Running in Docker: DB connection that is less than 5 minutes is fine. For DB connection that is longer than 5 minutes, every 5-6 minutes the connection is broken in docker and a new connection is initiated automatically. After 3 reconnections, following error is returned and process is terminated

    driver: bad connection

Some Tries

  • try to ping database during the function call to keep connection alive -- not works (working in some simple cases, but not in complicate case and not stable.
  • try to ping database during function call with same connection using db.SetMaxOpenConns(1) and db.SetMaxIdleConns(1) -- not works.
  • try to set connection life to longer with db.SetConnMaxLifetime() -- not works.

Some Info

After some search, the possible reason is that docker periodically checks connections and time them out if connection is alive for 300 seconds and no response returns. Not find any way to overwrite this configuration.

https://github.com/moby/moby/issues/36053

https://github.com/docker/for-mac/issues/2442

How can I get around this issue? Appreciate if any help.

duanqiu2064
duanqiu2064 我在docker设置上也有超时问题。重新启动MacOS。在Mac上,我还使用dbeaver访问了多个postgres版本。
大约一年之前 回复
douyinzha5820
douyinzha5820 找到解决方案了吗?我遇到了同样的问题:github.com/docker/for-mac/issues/3728
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问