【问题标题】:Connection refused from MySQL runtime in Eclipse CheEclipse Che 中 MySQL 运行时的连接被拒绝
【发布时间】:2017-12-30 06:20:22
【问题描述】:

我正在尝试从multi-machine workspace 中的另一个 NodeJS 运行时连接到 MySQL 运行时中的数据库。

在测试中,我使用目标用户列表调用 API http://localhost:3000/target。此 API 中的代码在 db 上运行 SELECT:

...

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 {

                ...

我从终端得到的结果:

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

这里我定义了到数据库的连接:

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);

....

配置:

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

这是我在 Eclipse Che 中的 db 机器的配置:

snapshot of servers configuration

这是我的食谱:

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

【问题讨论】:

    标签: mysql node.js docker eclipse-che


    【解决方案1】:

    您可以分享您的recipe 用于多机工作区吗?这对调试有很大帮助。

    只是一个猜测:我认为您的设置问题是使用localhost 进行数据库连接。如果您正在运行多机设置,则 db 在不同的 docker 容器中运行,需要通过其名称来寻址。

    多机教程节选:

    在配方中,“dev-machine”的depends_on参数允许它 连接到“db”机器 MySQL 进程的 3306 端口。 “dev-machine”在项目中配置其 MySQL 客户端连接 源代码位于 src/main/resources/spring/data-access.properties。这 url 由 jdbc.url=jdbc:mysql://db:3306/petclinic 定义,它使用 数据库机器的名称“db”和 MySQL 服务器默认端口 3306.

    您需要在recipe 中配置开放端口。

    免责声明:我不直接隶属于 Eclipse Che、Codenvy 或 Red Hat,但我们正在 Eclipse Che 之上构建我们的 own cloud IDE for C/C++ multicore optimization

    【讨论】:

    • 从我在问题中添加的配方中可以看出,有一个“depends_on”参数可以让开发机器连接到数据库机器。我需要做更多的事情吗?
    • 是的,您需要更改配置文件以使用db 机器而不是本地主机:{ "mysql": { "host": "db" } }
    • 有效!我认为将 localhost 和真实端口映射到 eclipse che 主机名和端口是工作区配置的问题,而不涉及实际的应用程序代码。相反,在应用程序的配置文件中,我不得不按照您的建议将“localhost”更改为“db”。
    • 很高兴听到这个消息!
    猜你喜欢
    • 2021-06-24
    • 2022-01-22
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 2017-07-22
    • 2021-09-06
    • 2016-11-07
    • 2021-03-16
    相关资源
    最近更新 更多