docker connection from nodejs container to mysql container ECONNREFUSED 127.0.0.1:3308

I have 2 containers, nodejs and mysql, with both configured to use the same network. Inside the nodejs container there is a file that use mysql2 package to create a connection pool from the mysql container, here’s the code that create the connection pool

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const pool: Pool = mysql.createPool({
host: 'localhost',
user: 'admin',
password: 'mypasswd',
database: 'mydb',
port: '3308',
connectionLimit: 10,
waitForConnections: true
});
</code>
<code>const pool: Pool = mysql.createPool({ host: 'localhost', user: 'admin', password: 'mypasswd', database: 'mydb', port: '3308', connectionLimit: 10, waitForConnections: true }); </code>
const pool: Pool = mysql.createPool({
        host: 'localhost',
        user: 'admin',
        password: 'mypasswd',
        database: 'mydb',
        port: '3308',
        connectionLimit: 10,
        waitForConnections: true
    });

With this code, after running the docker-compose up -d command, mysql container work smoothly while nodejs container failed to start with the following error

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Error: connect ECONNREFUSED 127.0.0.1:3308
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1606:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3308,
fatal: true
}
</code>
<code>Error: connect ECONNREFUSED 127.0.0.1:3308 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1606:16) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 3308, fatal: true } </code>
Error: connect ECONNREFUSED 127.0.0.1:3308
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1606:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3308,
  fatal: true
}

Here’s what I’ve tried so far but still got no luck:

  • Grant all privileges to ‘admin’@’%’ and ‘admin’@’localhost’ users
  • Try changing port from 3308 to ‘/var/run/mysqld/mysqld.sock’ 1
  • Check the /etc/my.cnf file in the mysql container hoping to find the bind-address or something similar, here’s my /etc/my.cnf file content
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.3/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.3/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
host-cache-size=0
# skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
# if this message show up, this means the operation was a success
</code>
<code># For advice on how to change settings please see # http://dev.mysql.com/doc/refman/8.3/en/server-configuration-defaults.html [mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M # Remove leading # to revert to previous value for default_authentication_plugin, # this will increase compatibility with older clients. For background, see: # https://dev.mysql.com/doc/refman/8.3/en/server-system-variables.html#sysvar_default_authentication_plugin # default-authentication-plugin=mysql_native_password host-cache-size=0 # skip-name-resolve datadir=/var/lib/mysql socket=/var/run/mysqld/mysqld.sock secure-file-priv=/var/lib/mysql-files user=mysql pid-file=/var/run/mysqld/mysqld.pid [client] socket=/var/run/mysqld/mysqld.sock !includedir /etc/mysql/conf.d/ # if this message show up, this means the operation was a success </code>
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.3/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.3/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
host-cache-size=0
# skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/

# if this message show up, this means the operation was a success
  • Change plugin from ‘mysql_native_password’ to ‘caching_sha2_password’ (I was just so desperate at that point)
  • I’ve tried invoke the function that create a connection pool from the mysql container locally (not using nodejs container) and the function worked like a charm. So I figure that the problem is the connectivity between both nodejs and mysql containers
  • Actually, it used to work before but somehow starting to break after I messed with mysql program installed locally, I don’t know if it’s just a coincidence since I believe that mysql working in a container should have nothing to do with mysql program installed on local pc

Here’s my docker code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>node:
container_name: node
build: ./server
ports:
- 8000:8000
volumes:
- ./server:/usr/src/app
env_file:
- server/.env
networks:
- app-network
- db-network
- server-network
db:
container_name: mysql
image: mysql:latest
restart: unless-stopped
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --default-authentication-plugin=caching_sha2_password
environment:
- MYSQL_ROOT_PASSWORD=mypasswd
- MYSQL_USER=admin
- MYSQL_PASSWORD=mypasswd
- MYSQL_DATABASE=mydb
- TZ='Asia/Bangkok'
ports:
- 3308:3306
volumes:
- ./server/db/mysql/data:/var/lib/mysql
networks:
- db-network
networks:
db-network:
name: db-network
</code>
<code>node: container_name: node build: ./server ports: - 8000:8000 volumes: - ./server:/usr/src/app env_file: - server/.env networks: - app-network - db-network - server-network db: container_name: mysql image: mysql:latest restart: unless-stopped command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --default-authentication-plugin=caching_sha2_password environment: - MYSQL_ROOT_PASSWORD=mypasswd - MYSQL_USER=admin - MYSQL_PASSWORD=mypasswd - MYSQL_DATABASE=mydb - TZ='Asia/Bangkok' ports: - 3308:3306 volumes: - ./server/db/mysql/data:/var/lib/mysql networks: - db-network networks: db-network: name: db-network </code>
node:
    container_name: node
    build: ./server
    ports:
      - 8000:8000
    volumes:
      - ./server:/usr/src/app
    env_file:
      - server/.env
    networks:
      - app-network
      - db-network
      - server-network
db:
    container_name: mysql
    image: mysql:latest
    restart: unless-stopped
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --default-authentication-plugin=caching_sha2_password
    environment:
      - MYSQL_ROOT_PASSWORD=mypasswd
      - MYSQL_USER=admin
      - MYSQL_PASSWORD=mypasswd
      - MYSQL_DATABASE=mydb
      - TZ='Asia/Bangkok'
    ports:
      - 3308:3306
    volumes:
      - ./server/db/mysql/data:/var/lib/mysql
    networks:
      - db-network
networks:
  db-network:
    name: db-network

New contributor

あいうえお is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật