docker mysql带有错误:连接econnrefuse



试图从微服务中连接数据库,无法连接数据库

错误:连接Econnrefuse 172.18.0.2:3306

服务/index.js

var http = require('http');
//create a server object:
http.createServer(function (req, res) {
    res.write('Hello World!'); //write a response to the client
    res.end(); //end the response
}).listen(8080); //the server object listens on port 8080
console.log("Listening at 8080");
var mysql = require('mysql');
var con = mysql.createConnection({
    host: "database",
    user: "root",
    password: "password"
});
con.connect(function(err) {
    if (err) throw err;
    console.log("Database Connected!");
});

docker-compose.yml

version: '3'
services:
  database:
    build:
      ./database
    ports:
      - "6603:3306"
    image: "test-mysql"
    container_name: "test-mysql"
  service:
    build:
      ./service
    ports:
      - "8080:8080"
    depends_on:
      - database
    image: "test-nodejs"
    container_name: "test-nodejs"
    restart: on-failure

我尝试使用不同的设置连接到数据库。

1(没有端口

var con = mysql.createConnection({
    host: "database",
    user: "root",
    password: "password"
});

2(指定的端口3306

var con = mysql.createConnection({
    host: "database",
    user: "root",
    password: "password"
    port: 3306
});

3(指定的端口6603

var con = mysql.createConnection({
    host: "database",
    user: "root",
    password: "password",
    port: 6603
});

数据库/Dockerfile

FROM mysql
ENV MYSQL_DATABASE=test
ENV MYSQL_ROOT_PASSWORD=password
EXPOSE 6603:3306
COPY ./schema.sql /docker-entrypoint-initdb.d/

基本上我的node.js微服务如何发现数据库服务?


编辑

我怀疑在Nodejs启动时还没有准备好数据库,因此我在连接到数据库之前添加了一些延迟,并且错误更改了

更新的代码

setTimeout(function(){
    var mysql = require('mysql');
    var con = mysql.createConnection({
        host: "database",
        user: "root",
        password: "password"
    });
    con.connect(function(err) {
        if (err) throw err;
        console.log("Database Connected!");
    });
}, 20 * 1000);

输出

Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

可能您正在使用不支持您尝试登录的MySQL版本。尝试MySQL v5.7:

docker run -d -p 6603:3306 --name mysql-container -e MYSQL_ROOT_PASSWORD=password mysql:5.7

您错过了订单:

首先连接到数据库,然后收听端口。

var http = require('http');
var mysql = require('mysql');
var con = mysql.createConnection({
  host: "database",
  user: "root",
  password: "password"
 } );
con.connect(function(err) {
  if (err) throw err;
  console.log("Database Connected!");
});
 //create a server object:
 http.createServer(function (req, res) {
 res.write('Hello World!'); //write a response to the client
 res.end(); //end the response
  }).listen(8080); //the server object listens on port 8080
console.log("Listening at 8080");

数据库端口在容器中不会被翻译。只需在您的应用中使用3306

如果使用 Docker Apple Silicon 使用Mac M1预览。您可能会在尝试降级到5.7版本

时发现此错误
ERROR: no matching manifest for linux/arm64/v8 in the manifest list entries

事件如果将平台更改为 linux/x86_64 AMD64 它将安装,但是连接的错误。

尝试与Doc

中的Adminer Image一起添加新服务
db:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

终于使用http://localhost打开管理服务:8080用

登录
server : db //which is mysql service name that you just created
username: // your username
password: // your password

最新更新