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
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
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
# 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
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
あいうえお is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.