Eclipse Che 中的 MySQL 运行时拒绝连接
Connection refused from MySQL runtime in Eclipse Che
我正在尝试从 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
能否分享您的 recipe
多机工作区?这对调试它有很大帮助。
只是一个猜测:我认为您设置的问题是使用 localhost
作为您的数据库连接。如果您是 运行 多机设置,则数据库 运行 在不同的 docker 容器中,需要通过其名称进行寻址。
摘自多机教程:
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.
您需要在 recipe
中配置开放端口。
免责声明:我不直接隶属于 Eclipse Che、Codenvy 或 Red Hat,但我们正在 Eclipse Che 之上构建我们的own cloud IDE for C/C++ multicore optimization。
我正在尝试从 multi-machine workspace 中的另一个 NodeJS 运行时连接到 MySQL 运行时中的数据库。
在测试中,我用目标用户列表调用 API http://localhost:3000/target
。此 API 中的代码在 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 {
...
我从终端得到的结果:
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
能否分享您的 recipe
多机工作区?这对调试它有很大帮助。
只是一个猜测:我认为您设置的问题是使用 localhost
作为您的数据库连接。如果您是 运行 多机设置,则数据库 运行 在不同的 docker 容器中,需要通过其名称进行寻址。
摘自多机教程:
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.
您需要在 recipe
中配置开放端口。
免责声明:我不直接隶属于 Eclipse Che、Codenvy 或 Red Hat,但我们正在 Eclipse Che 之上构建我们的own cloud IDE for C/C++ multicore optimization。