I have setup the Node.js v19.3.0
with MySQL 5.7.41
using docker 20.10.14, build a224086
I have written the connection code in the script.js
file but when I hit npm start
then after 8-10 seconds it causes connection close error as below
Error: Connection lost: The server closed the connection. at Protocol.end (/home/kishan/node-api-demo/node_modules/mysql/lib/protocol/Protocol.js:112:13) at Socket.<anonymous> (/home/kishan/node-api-demo/node_modules/mysql/lib/Connection.js:94:28) at Socket.<anonymous> (/home/kishan/node-api-demo/node_modules/mysql/lib/Connection.js:526:10) at Socket.emit (node:events:525:35) at endReadableNT (node:internal/streams/readable:1359:12) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) -------------------- at Protocol._enqueue (/home/kishan/node-api-demo/node_modules/mysql/lib/protocol/Protocol.js:144:48) at Protocol.handshake (/home/kishan/node-api-demo/node_modules/mysql/lib/protocol/Protocol.js:51:23) at Connection.connect (/home/kishan/node-api-demo/node_modules/mysql/lib/Connection.js:116:18) at Object.<anonymous> (/home/kishan/node-api-demo/config/server.js:79:5) at Module._compile (node:internal/modules/cjs/loader:1218:14) at Module._extensions..js (node:internal/modules/cjs/loader:1272:10) at Module.load (node:internal/modules/cjs/loader:1081:32) at Module._load (node:internal/modules/cjs/loader:922:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12) at node:internal/main/run_main_module:23:47 { fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }
I have created the docker-compose.yml
and script.js
files below.
docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
container_name: db
environment:
MYSQL_ROOT_PASSWORD: my_secret_password
MYSQL_DATABASE: app_db
MYSQL_USER: db_user
MYSQL_PASSWORD: db_user_pass
ports:
- "6033:3306"
volumes:
- dbdata:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: pma
links:
- db
environment:
PMA_HOST: db
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 2025:80
volumes:
dbdata:
script.js
var mysql = require("mysql");
var con = mysql.createConnection({
connectionLimit: 100,
//host: "localhost",
user: "db_user",
password: "db_user_pass",
database: "app_db",
port: 2025,
// connectionLimit: 15,
// queueLimit: 30,
// queryTimeout: 600000,
// connectTimeout: 1000000,
// acquireTimeout: 1000000
});
con.connect(function (err) {
if (err) throw err;
console.log("Connected!");
});
I have tried all the comment removing on the createConnection function but it didn’t work
Kindly please help me to fix this out in the standard way.
Thank you!