无法从节点但从终端访问mysql容器 [英] Can't access mysql container from node but from terminal

查看:63
本文介绍了无法从节点但从终端访问mysql容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个由 docker-compose 运行的节点应用程序。该应用程序需要同时启动的mysql服务器,但是当我到达节点应用程序中的要访问服务器的位置时,会出现以下错误:

I created a node application that is run by a docker-compose. The application needs a mysql server which is also started but when I get to the point in my node application to access the server I get the following error:

events.js:136
throw er; // Unhandled 'error' event
^ 
Error: connect ECONNREFUSED 127.0.0.1:3306
 at Object._errnoException (util.js:1031:13)
 at _exceptionWithHostPort (util.js:1052:20)
 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1195:14)
 --------------------
 at Protocol._enqueue (/usr/src/node_modules/mysql/lib/protocol/Protocol.js:145:48)
 at Protocol.handshake (/usr/src/node_modules/mysql/lib/protocol/Protocol.js:52:23)
 at Connection.connect (/usr/src/node_modules/mysql/lib/Connection.js:130:18)
 at Connection._implyConnect (/usr/src/node_modules/mysql/lib/Connection.js:461:10)
 at Connection.query (/usr/src/node_modules/mysql/lib/Connection.js:206:8)
 at Object.<anonymous> (/usr/src/configs/passport.js:15:12)
 at Module._compile (module.js:641:30)
 at Object.Module._extensions..js (module.js:652:10)
 at Module.load (module.js:560:32)
 at tryModuleLoad (module.js:503:12)
 at Function.Module._load (module.js:495:3)
 at Module.require (module.js:585:17)
 at require (internal/module.js:11:18)
 at Object.<anonymous> (/usr/src/app.js:47:1)
 at Module._compile (module.js:641:30)
 at Object.Module._extensions..js (module.js:652:10)

相应的节点代码本质上是这样的:

The corresponding node code is essentially this:

var mysql = require('mysql');
var dbconfig = require('./database');
var connection = mysql.createConnection(dbconfig.connection);

database.js看起来像这样:

And database.js looks like this:

module.exports = {
  'connection': {
    'host': '127.0.0.1',
    'user': 'user',
    'password': 'password'
  },
  'database': 'website_user',
  'users_table': 'users'
};

所有这些都很好地在我的MacBook上运行。当我打开第二个终端窗口并执行时:

All of this is run lokally on my MacBook. When I open a second terminal window and execute:

mysql -h 127.0.0.1 -u user -p

我可以成功连接到该服务器。指定的用户与在节点应用程序中传递给我的连接建立的用户相同。我大概已经花了四个小时弄弄了这个,却不知道我在做什么错。为了使其正常工作,我必须更改什么?

I can successfully connect to this server. The user specified there is identical to the user that is passed to my connection establishment in the node app. I've messed around with this for probably 4 hours now and have no idea what I'm doing wrong. What do I have to change in order for this to work?

让我知道是否需要提供任何东西来帮助我解决问题。谢谢!

Let me know if there is anything I need to provide in order to help me with my problem. Thanks!

推荐答案

您正尝试连接到 127.0.0.1:3306 从节点容器开始-但这就是节点容器本身。如果您的数据库服务名为 mysql ,则应该能够使用这样的配置(假设docker-compose.yml版本2或更高版本):

You are trying to connect to 127.0.0.1:3306 from the node container - but that's the node container itself. If your database service is named mysql you should be able to use a config like this (assuming docker-compose.yml version 2 or above):

module.exports = {
  'connection': {
    'host': 'mysql',
    'user': 'user',
    'password': 'password'
  },
  'database': 'website_user',
  'users_table': 'users'
};

这篇关于无法从节点但从终端访问mysql容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆