Reputation: 1
I'm running nodejs docker and mysql on my computer. But even I give the correct username and password I'm not able to access the database via localhost. And when I try to access the docker nodejs server I get error message.
Here are the $docker-compose logs -f logs messages I get, when I try to access docker server via localhost:
db_1 | App listening on port 9000
backend_1 | Error occurred: { Error: connect ECONNREFUSED
172.18.0.3:3306
backend_1 | at Object.createConnection
(/opt/app/node_modules/mysql2/promise.js:31:31)
backend_1 | at _callee$ (/opt/app/src/index.js:29:30)
backend_1 | at tryCatch (/opt/app/node_modules/regenerator-
runtime/runtime.js:65:40)
backend_1 | at Generator.invoke [as _invoke]
(/opt/app/node_modules/regenerator-runtime/runtime.js:303:22)
backend_1 | at Generator.prototype.(anonymous function) [as
next] (/opt/app/node_modules/regenerator-runtime/runtime.js:117:21)
backend_1 | at step (/opt/app/src/index.js:21:191)
backend_1 | at /opt/app/src/index.js:21:437
backend_1 | at new Promise (<anonymous>)
backend_1 | at /opt/app/src/index.js:21:99
backend_1 | at /opt/app/src/index.js:26:1
backend_1 | message: 'connect ECONNREFUSED 172.18.0.3:3306',
backend_1 | code: 'ECONNREFUSED',
backend_1 | errno: 'ECONNREFUSED',
backend_1 | sqlState: undefined }
backend_1 |
backend_1 | Error: connect ECONNREFUSED 172.18.0.3:3306
backend_1 | at Object.createConnection
(/opt/app/node_modules/mysql2/promise.js:31:31)
backend_1 | at _callee$ (/opt/app/src/index.js:29:30)
backend_1 | at tryCatch
(/opt/app/node_modules/regenerator-runtime/runtime.js:65:40)
backend_1 | at Generator.invoke [as _invoke]
(/opt/app/node_modules/regenerator-runtime/runtime.js:303:22)
backend_1 | at Generator.prototype.(anonymous function)
[as next] (/opt/app/node_modules/regenerator-
runtime/runtime.js:117:21)
backend_1 | at step (/opt/app/src/index.js:21:191)
backend_1 | at /opt/app/src/index.js:21:437
backend_1 | at new Promise (<anonymous>)
backend_1 | at /opt/app/src/index.js:21:99
backend_1 | at /opt/app/src/index.js:26:1
Here is my docker-compose.yml file:
version: '3'
services:
backend:
build:
context: ./
dockerfile: .dockerfiles/BackendDev
ports:
- "9000:9000"
volumes:
- ./backend:/opt/app
restart: always
backend_test:
build:
context: ./
dockerfile: .dockerfiles/BackendDev
command: yarn run test:watch
ports:
- "9005:9005"
volumes:
- ./backend:/opt/app
restart: always
db:
image: mysql:8.0.3
volumes:
- /var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: db_rootpass
MYSQL_DATABASE: db_1
restart: always
mywebsql:
image: quantumobject/docker-mywebsql
ports:
- "9001:80"
restart: always
Here is my js file:
import 'babel-polyfill';
import Koa from 'koa';
import Router from 'koa-router';
import mysql from 'mysql2/promise';
// The port that this server will run on, defaults to 9000
const port = process.env.PORT || 9000;
// Instantiate a Koa server
const app = new Koa();
// Instantiate routers
const test = new Router();
// Define API path
const apiPath = '/api/v1';
const connectionSettings = {
host: 'db',
user: 'root',
database: 'db_1',
password: 'db_rootpass',
namedPlaceholders: true,
};
test.get(`${apiPath}/test`, async (ctx) => {
// Tell the HTTP response that it contains JSON data encoded in UTF-8
try {
const conn = await mysql.createConnection(connectionSettings);
const [data] = await conn.execute(`
SELECT *
FROM test_table
`);
console.log('Data fetched:', data);
// Tell the HTTP response that it contains JSON data encoded in UTF-8
ctx.type = 'application/json; charset=utf-8';
// Add stuff to response body
ctx.body = { greeting: 'Hello world!', data };
} catch (error) {
console.error('Error occurred:', error);
ctx.throw(500, error);
}
});
app.use(test.routes());
app.use(test.allowedMethods());
// Start the server and keep listening on port until stopped
app.listen(port);
console.log(`App listening on port ${port}`);
Upvotes: 0
Views: 534
Reputation: 17
For debugging purposes, you might also connect directly to your docker container, for example like this: docker exec -it CONTAINER_ID /bin/bash
and try to connect there locally to your mysql DB. I would first verify, that the DB is able to accept connections. Furthermore, are you sure, that the DB is already running when your nodeJS app is trying to connect to it? I'm not doing anything with docker compose, so I'm not so sure about the networking features, but when you are using the network_mode: host
, I think you should also be able to connect to the mysql container directly from your local computer with mysql-client. You should make these tests first.
Give us some feedback :)
Upvotes: 0