Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection lost: The server closed the connection with Bun/Drizzle/MySQL/DockerSwarm #3117

Closed
rnfrafael opened this issue Oct 15, 2024 · 3 comments

Comments

@rnfrafael
Copy link

rnfrafael commented Oct 15, 2024

I keep getting this error often, sometimes occurs many times, sometimes not...
These are some logs
image

Happens in any query, so is not related to the query itself, I tried many config in the connection, this is the connection code.

import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import { env } from "../env";
import { schemas } from "./schemas";
const connection = mysql.createPool({
  uri: env.DATABASE_URL,
});

export const db = drizzle(connection, { schema: schemas, mode: "default", logger: env.DATABASE_LOG ?? false });

I always used pool connection, didn't try the direct connection.

So, is this a bug? I think probably is something about the Docker environment, maybe? This is my docker stack

version: '3.8'

volumes:
  db-api-db:
  mysqlconf:
networks:
  api-db:
  proxy:
    external: true

services:
  api-db:
    image: api-db:1.6.9
    # command: ["sleep", "infinity"]
    logging:
      driver: loki
      options:
        loki-url: "http://localhost:3100/loki/api/v1/push"
    environment:
      RUN_MIGRATIONS: 'true'
      DATABASE_URL: mysql://root:password@mysql:3306/api-db
      MP_CLIENT_ID: id
      MP_CLIENT_SECRET: secret
      MP_REDIRECT_URI: redirecturi
      DATABASE_LOG: "false"
      DATABASE_ROOT_PASSWORD: password
      DATABASE_NAME: api-db
      DATABASE_USER: root
      DATABASE_PASSWORD: password
      DATABASE_PORT: 3306
      DATABASE_HOST: mysql
    depends_on:
      - mysql
    networks:
      - api-db
      - proxy
    deploy:
      mode: replicated
      replicas: 1
      placement:
        constraints:
          - node.role == manager
      labels:
        - traefik.enable=true
        - traefik.http.routers.api-db.rule=Host(`dns`)
        - traefik.http.routers.api-db.entrypoints=websecure
        - traefik.http.routers.api-db.tls=true
        - traefik.http.routers.api-db.tls.certresolver=leresolver
        - traefik.http.services.api-db.loadbalancer.server.port=3000
        - traefik.http.routers.api-db.service=api-db
        # - traefik.http.services.api-db.loadbalancer.healthcheck.path=/health
        # - traefik.http.services.api-db.loadbalancer.healthcheck.interval=30s

  mysql:
    image: mysql
    volumes:
      - db-api-db:/var/lib/mysql
      - mysqlconf:/etc/mysql
    networks:
      - api-db
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: api-db

  phpmyadmin:
    image: phpmyadmin
    networks:
      - api-db
    ports:
      - 9001:80
    depends_on:
      - mysql
    environment:
      PMA_HOST: mysql

Can this be related to Drizzle? Maybe Bun?

Thanks for any help.

Environment

bun: v1.1.27
mysql2: v3.11.3

Edit:
I realized that I only tested using Bun, I decided to test it with Node now

Edit2: Tested with Node, got "same error"

2024-10-15 13:53:54.250 | {"level":50,"time":1729011234249,"pid":89,"hostname":"d8316eea0257","err":{"type":"Error","message":"read ECONNRESET","stack":"Error: read ECONNRESET\n    at PromisePool.query (/app/node_modules/mysql2/promise.js:356:22)\n    at MySql2PreparedQuery.execute (/app/node_modules/src/mysql2/session.ts:121:31)\n    at MySqlSelectBase.execute (/app/node_modules/src/mysql-core/query-builders/select.ts:961:25)\n    at MySqlSelectBase.then (/app/node_modules/src/query-promise.ts:31:15)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","code":"ECONNRESET","errno":-104},"msg":"read ECONNRESET"}
@wellwelwel
Copy link
Collaborator

Hey, @rnfrafael 🙋🏻‍♂️

Could you try to check it without Drizzle?

@rnfrafael
Copy link
Author

Yes, I can try, but it will take a while, I need to update the code and leave it running in production for a "real" test. This problem takes some time to happen, I haven't been able to find a logic yet, but that's it, I'll try without Drizzle.

@rnfrafael
Copy link
Author

@wellwelwel I didn't make the change, I found this #2599 and I did make this change #2599 (comment)
maxIdle: 0 and did work, for two days without any errors, It may not be the best approach but works :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants