skip to Main Content

I’m trying to dockerize a Node express server with MYSQL database with sequelize.js ORM.
How can i solve this issue connecting to the database

This is my docker-compose.yml file

version: '3.9'

services:
  mysql_db:
    platform: linux/x86_64
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: budget
      MYSQL_ROOT_PASSWORD: 12345678
    ports:
      - 3309:3306
    networks:
      - budget_app_network
    volumes:
      - mysql_db_volume:/var/lib/mysql

  api: 
    build:
      context: ./server
      dockerfile: Dockerfile
    ports:
      - ${API_PORT}:${API_PORT}
    networks:
      - budget_app_network
    environment:
      PORT: ${API_PORT}
      DATABASE_URL: mysql://root:12345678@localhost:3309/budget
      ACCESS_TOKEN_SECRET: ${ACCESS_TOKEN_SECRET}
      REFRESH_TOKEN_SECRET: ${REFRESH_TOKEN_SECRET}
      MAIL_TOKEN_SECRET: ${MAIL_TOKEN_SECRET}
    depends_on:
      - mysql_db

networks:
  budget_app_network:

volumes:
  mysql_db_volume: {}
 

It is showing this error

✔ Network budget_budget_app_network Created 0.1s 
✔ Container budget-mysql_db-1 Created 0.0s Attaching to budget-mysql_db-1 budget-mysql_db-1 | 2023-06-29 12:16:17+00:00 
[Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.42-1.el7 started. budget-mysql_db-1 | 2023-06-29 12:16:17+00:00 
[Note] [Entrypoint]: Switching to dedicated user 'mysql' budget-mysql_db-1 | 2023-06-29 12:16:17+00:00 
[Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.42-1.el7 started. budget-mysql_db-1 | '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock' budget-mysql_db-1 | 2023-06-29T12:16:17.503579Z 0 
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). budget-mysql_db-1 | 2023-06-29T12:16:17.504445Z 0 
[Note] mysqld (mysqld 5.7.42) starting as process 1 ... budget-mysql_db-1 | 2023-06-29T12:16:17.506351Z 0 
[Note] InnoDB: PUNCH HOLE support available budget-mysql_db-1 | 2023-06-29T12:16:17.506362Z 0 
[Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins budget-mysql_db-1 | 2023-06-29T12:16:17.506364Z 0 
[Note] InnoDB: Uses event mutexes budget-mysql_db-1 | 2023-06-29T12:16:17.506366Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier budget-mysql_db-1 | 2023-06-29T12:16:17.506368Z 0 
[Note] InnoDB: Compressed tables use zlib 1.2.13 budget-mysql_db-1 | 2023-06-29T12:16:17.506369Z 0 [Note] InnoDB: Using Linux native AIO budget-mysql_db-1 | 2023-06-29T12:16:17.506519Z 0 
[Note] InnoDB: Number of pools: 1 budget-mysql_db-1 | 2023-06-29T12:16:17.506596Z 0 
[Note] InnoDB: Using CPU crc32 instructions budget-mysql_db-1 | 2023-06-29T12:16:17.507485Z 0 
[Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M budget-mysql_db-1 | 2023-06-29T12:16:17.511283Z 0 
[Note] InnoDB: Completed initialization of buffer pool budget-mysql_db-1 | 2023-06-29T12:16:17.512403Z 0 
[Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority(). budget-mysql_db-1 | 2023-06-29T12:16:17.524033Z 0 

[ERROR] [FATAL] InnoDB: Table flags are 0 in the data dictionary but the flags in file ./ibdata1 are 0x4800! budget-mysql_db-1 | 2023-06-29 12:16:17 0x7fa4adb1e880 InnoDB: Assertion failure in thread 140345265481856 in file ut0ut.cc line 921 budget-mysql_db-1 | InnoDB: We intentionally generate a memory trap. budget-mysql_db-1 | InnoDB: Submit a detailed bug report to http://bugs.mysql.com. budget-mysql_db-1 | InnoDB: If you get repeated assertion failures or crashes, even budget-mysql_db-1 | InnoDB: immediately after the mysqld startup, there may be budget-mysql_db-1 | InnoDB: corruption in the InnoDB tablespace. Please refer to budget-mysql_db-1 | InnoDB: http://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html budget-mysql_db-1 | InnoDB: about forcing recovery. budget-mysql_db-1 | 12:16:17 UTC - mysqld got signal 6 ; budget-mysql_db-1 | This could be because you hit a bug. It is also possible that this binary budget-mysql_db-1 | or one of the libraries it was linked against is corrupt, improperly built, budget-mysql_db-1 | or misconfigured. This error can also be caused by malfunctioning hardware. budget-mysql_db-1 | Attempting to collect some information that could help diagnose the problem. budget-mysql_db-1 | As this is a crash and something is definitely wrong, the information budget-mysql_db-1 | collection process might fail. budget-mysql_db-1 | budget-mysql_db-1 | key_buffer_size=8388608 budget-mysql_db-1 | read_buffer_size=131072 budget-mysql_db-1 | max_used_connections=0 budget-mysql_db-1 | max_threads=151 budget-mysql_db-1 | thread_count=0 budget-mysql_db-1 | connection_count=0

2

Answers


  1. Chosen as BEST ANSWER

    Updated the docker-compose.yml with a health check.

    version: '3.9'
    
    services:
      mysql_db:
        platform: linux/x86_64
        image: mysql:8
        restart: always
        environment:
          MYSQL_DATABASE: ${DB_NAME}
          MYSQL_ROOT_PASSWORD: ${DB_ROOT_PWD}
          MYSQL_USER: ${DB_USR}
          MYSQL_PASSWORD: ${DB_USR_PWD}
        ports:
          - 3309:3306
        healthcheck:
          test: mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD
          start_period: 5s
          interval: 5s
          timeout: 5s
          retries: 55
        networks:
          - budget_app_network
    
    
      api: 
        build:
          context: ./server
          dockerfile: Dockerfile
        ports:
          - ${API_PORT}:${API_PORT}
        networks:
          - budget_app_network
        environment:
          PORT: ${API_PORT}
          DATABASE_URL: mysql://root:${DB_ROOT_PWD}@mysql_db/${DB_NAME}
        depends_on:
          mysql_db:
            condition: service_healthy
    
    networks:
      budget_app_network:
    
    volumes:
      mysql_db_volume: {}
    

  2. Since you’re dockerizing the Node as well, you need to update the env variables to point to the new address of the database in the other container:

    DATABASE_URL: mysql://root:12345678@mysql_db:3306/budget
    

    I updated it to use the name of the MySLQ service – since compose will make it visible to the node container – and the internal port.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search