1

I'm trying to connect to a db in a MySQL runtime from another NodeJS runtime in a multi-machine workspace.

In a test I'm calling the API http://localhost:3000/target with the list of target users. Code in this API runs a SELECT on the db:

...

exports.list = function(req, res) {

    req.getConnection(function(err, connection) {

        if (err) {
                console.log("MySQL " + err);
        } else {

              connection.query('SELECT id FROM target', function(err, rows) {

            if (err) {
                console.log("Error Selecting : %s ", err);
            } else {

                ...

The result I get from terminal:

get target list from http://localhost:3000/target
MySQL Error: connect ECONNREFUSED 127.0.0.1:3306

Here I define the connection to the db:

var express = require('express');
var connection = require('express-myconnection');
var mysql = require('mysql');
var config = require('config');
var connectionConfig = config.get('mysql');
var connectionInstance = connection(mysql, connectionConfig, 'request');

...

app.use(connectionInstance);

app.get('/', function(req, res) {
    res.send('Welcome');
});

app.get('/target', target.list);

....

config:

{
    "mysql": {
        "host": "localhost",
        "user": "[user]",
        "password": "[password]",
        "database": "[database]"
    },
    "app": {
        "port": 3000,
        "server": "http://localhost"
    }
}

This is what I have in the configuration of the db machine in Eclipse Che:

snapshot of servers configuration

Here's my recipe:

services: db: image: eclipse/mysql environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: petclinic MYSQL_USER: petclinic MYSQL_PASSWORD: password MYSQL_ROOT_USER: root mem_limit: 1073741824 dev-machine: image: eclipse/node mem_limit: 2147483648 depends_on: - db elasticsearch: image: florentbenoit/cdvy-ela-23 mem_limit: 2147483648

Michele Minno
  • 273
  • 1
  • 3
  • 14

1 Answers1

0

Can you share your recipe for the multi-machine workspace? That would help a lot in debugging it.

Just a guess: I think the problem with your setup is the use of localhost for your db connection. If you are running a multi-machine setup, the db is running in a different docker container and needs to be addressed by its name.

Excerpt from the Multi-Machine Tutorial:

In the recipe the depends_on parameter of the “dev-machine” allows it to connect to the “db” machine MySQL process’ port 3306. The “dev-machine” configures its MySQL client connection in the projects source code at src/main/resources/spring/data-access.properties. The url is defined by jdbc.url=jdbc:mysql://db:3306/petclinic which uses the database machine’s name “db” and the MySQL server default port 3306.

You need to configure the open ports in your recipe.

Disclaimer: I am not directly affiliated with Eclipse Che, Codenvy or Red Hat, but we are building our own cloud IDE for C/C++ multicore optimization on top of Eclipse Che.

  • as you can see from my recipe I added in my question, there's the 'depends_on' parameter to let the dev machine connect to the db machine. Do I have to do something more? – Michele Minno Jul 26 '17 at 09:01
  • yes, you need to change your configuration file to use the `db` machine instead of localhost: `{ "mysql": { "host": "db" } }` – Hanno Kolvenbach Jul 26 '17 at 09:04
  • 1
    It works! I thought mapping localhost and real ports to eclipse che hostnames and ports was a matter of workspace configuration, without touching the actual application code. Instead in the configuration file of the app I had to change 'localhost' to 'db', as you suggested. – Michele Minno Jul 26 '17 at 09:22
  • Glad to hear that! – Hanno Kolvenbach Jul 26 '17 at 09:24