docker mysql错误:connect ECONREFUSED

bfhwhh0e  于 2023-08-02  发布在  Mysql
关注(0)|答案(4)|浏览(128)

尝试从微服务内连接数据库,连接数据库失败
错误:连接ECONREFUSED 172.18.0.2:3306

service/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
});

database/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

rggaifut

rggaifut1#

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

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

字符串

vddsk6oq

vddsk6oq2#

您错过了订单:
首先连接到数据库,然后监听端口。

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

字符串

lnlaulya

lnlaulya3#

数据库端口不被转换,因为它在容器内。只需在您的应用程序中使用3306

afdcj2ne

afdcj2ne4#

如果您正在使用Docker Apple SiliconPreview with Mac m1。你可能会发现这个错误,而试图降级到5.7版本

ERROR: no matching manifest for linux/arm64/v8 in the manifest list entries

字符串
事件如果你将平台更改为linux/x86_64amd 64,它将被安装,但与连接相同的错误。
尝试添加新的服务与管理员形象在doc

db:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password

adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080


最后打开adminer service用http://localhost:8080登录用

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

相关问题