错误:握手不活动超时MySQL + Node.js

问题描述

将 docker-compose 与 ngnix、节点后端和 MysqL.js 一起使用。发生了这个错误

api      | there was an error connecting to pool:  Error: Handshake inactivity timeout
api      |     at Handshake.<anonymous> (/app/node_modules/MysqL/lib/protocol/Protocol.js:160:17)
api      |     at Handshake.emit (node:events:376:20)
api      |     at Handshake._onTimeout (/app/node_modules/MysqL/lib/protocol/sequences/Sequence.js:124:8)
api      |     at Timer._onTimeout (/app/node_modules/MysqL/lib/protocol/Timer.js:32:23)
api      |     at listOnTimeout (node:internal/timers:556:17)
api      |     at processtimers (node:internal/timers:499:7)
api      |     --------------------
api      |     at Protocol._enqueue (/app/node_modules/MysqL/lib/protocol/Protocol.js:144:48)
api      |     at Protocol.handshake (/app/node_modules/MysqL/lib/protocol/Protocol.js:51:23)
api      |     at PoolConnection.connect (/app/node_modules/MysqL/lib/Connection.js:116:18)
api      |     at Pool.getConnection (/app/node_modules/MysqL/lib/Pool.js:48:16)
api      |     at Pool.query (/app/node_modules/MysqL/lib/Pool.js:202:8)
api      |     at /app/db/connect.js:18:10
api      |     at new Promise (<anonymous>)
api      |     at Object.connectionFunc (/app/db/connect.js:17:21)
api      |     at Object.<anonymous> (/app/routes/index.js:17:12)
api      |     at Module._compile (node:internal/modules/cjs/loader:1108:14) {
api      |   code: 'PROTOCOL_SEQUENCE_TIMEOUT',api      |   fatal: true,api      |   timeout: 10000
api      | }

正如另一篇文章所提到的,我尝试更改超时时间,但没有奏效。

我必须使用网络来连接我的 docker-compose 文件。我间歇性地让它在不使用内部网络的情况下工作,但有时我会收到以下消息:

ERROR: Could not find an available,non-overlapping IPv4 address pool among the defaults to assign to the network

这本身就很奇怪,因为只有 3 个认网络(我创建了一个)。其他帖子说,如果您访问的网络超过 30 个或更多,这应该会成为一个问题 -

root@ubuntu-s-1vcpu-1gb-nyc1-01:~/blog/blog# docker network ls
NETWORK ID     NAME       DRIVER    ScopE
430e230e6641   bridge     bridge    local
915d73adee82   host       host      local
8cd9e79fd644   none       null      local
9f936dc688b7   underdev   bridge    local

也就是说,我在我的 docker-compose.yml 中尝试过这个

  db:
    container_name: db
    build: ./db
    command: --default-authentication-plugin=MysqL_native_password
    restart: always
    ports:
      - "3306:3306"
    environment:
      - MysqL_ALLOW_EMPTY_PASSWORD=True
      - MysqL_DATABASE=secret
      - MysqL_USER=secret
      - MysqL_ROOT_PASSWORD=secret
      - MysqL_PASSWORD=secret
    volumes:
      - db_data:/var/lib/MysqL
    networks:
      - underdev
volumes:
  db_data: {}
networks:
 underdev:
  external: true

我也试过这个:

networks:
  default:
    driver: bridge
    ipam:
      config:
        - subnet: 172.16.57.0/24

两者都修复了 ipv4 错误,但我仍然收到握手不活动超时错误。有人建议使用network_mode: bridge,但网络不应该自动使用认网桥吗?如果有人有任何想法,请告诉我。

附注:

这是与数据库的连接 - 我尝试向连接处理程序添加超时 - 超时有效,但连接仍然没有握手。

var MysqL = require('MysqL');
  var testconnection = MysqL.createConnection({
    host: 'db',port: '3306',user: 'root',password: process.env.MysqL_ROOT_PASSWORD,database: 'blog',debug: true
  })
  var pool  = MysqL.createPool({
    connectionLimit : 100,host            : 'db',port            : '3306',//connectTimeout  : 20000,//acquireTimeout  : 20000,//timeout         : 20000,user            : 'root',password        : process.env.MysqL_ROOT_PASSWORD,database        : 'blog'
  });
  console.log("-----------------------------------------------------");
  console.log("value of testconnection: ",testconnection);
  console.log("-----------------------------------------------------");
  testconnection.query("SELECT * from blogs",function(err,rows,fields){
    console.log("value of err: ",err);
    console.log("value of rows: ",rows);
    console.log("value of fields: ",fields);
  })
  console.log("-----------------------------------------------------");
  console.log("value of pool: ",pool);
  console.log("-----------------------------------------------------");
  let poolPromise = new Promise(resolve=>{
    pool.query("SELECT * FROM blogs",fields){
      /*
      console.log("value or err: ",err);
      console.log('value of rows: ',rows);
      console.log('value of fields: ',fields);
      */
      if (err){
        console.log('there was an error connecting to pool:',err);
        process.exit();
        resolve(false);
      }else{
        resolve(pool);
      }
    })
  })

  return poolPromise;
}

module.exports = {
  connectionFunc
}
         

编辑:

我已经编写了完全卸载并完全重新安装 docker(包括所有文件夹和子目录)的脚本,但仍然出现以下错误消息:

ERROR: Could not find an available,non-overlapping IPv4 address pool among the defaults to assign to the network

这是删除所有 docker 的脚本:

#!/bin/bash
  
sudo apt-get purge -y docker-engine docker docker.io docker-ce docker-ce-cli
sudo apt-get autoremove -y --purge docker-engine docker docker.io docker-ce

sudo apt-get remove docker docker-engine docker.io containerd runc

sudo rm -rf /var/lib/docker /etc/docker
sudo rm /etc/apparmor.d/docker
sudo groupdel docker
sudo rm -rf /var/run/docker.sock

这是我用来安装 docker 的脚本(我确认它适用于 hello world 测试)并且来自文档 (https://docs.docker.com/engine/install/ubuntu/) 的这个页面中的命令:

#!/bin/bash
  

echo 'apt-get update'
echo '-------------------------------------------------------------------------------'
sudo apt-get update
echo '-------------------------------------------------------------------------------'
echo 'sudo apt-get install'
echo '-------------------------------------------------------------------------------'
yes | sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
echo 'curl software'
echo '-------------------------------------------------------------------------------'
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
echo '-------------------------------------------------------------------------------'
echo 'add-apt-repository'
echo '-------------------------------------------------------------------------------'
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
echo 'sudo apt-get update'
echo '-------------------------------------------------------------------------------'
sudo apt-get update
echo '-------------------------------------------------------------------------------'
echo 'install docker-ce-cli'
echo '-------------------------------------------------------------------------------'
yes | sudo apt-get install docker-ce docker-ce-cli containerd.io
echo '-------------------------------------------------------------------------------'

编辑:

在 docker-compose 文件中向 tha api 容器添加链接变量没有解决问题:

    networks:
      - underdev
    links:
      - db:db

编辑:

尝试重新启动服务器。这不起作用,同样的错误。我也试过 通过

升级服务器
sudo apt-get upgrade && sudo apt-get dist-upgrade

再次运行应用程序 - 同样的错误

我目前运行的是 18.04.5 ubuntu 仿生:

root@ubuntu-s-1vcpu-1gb-nyc1-01:~# lsb_release -a
No LSB modules are available.
distributor ID: Ubuntu
Description:    Ubuntu 18.04.5 LTS
Release:        18.04
Codename:       bionic

尝试升级到 20.04.1,但也不起作用。


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...