简体   繁体   English

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

[英]Error: Handshake inactivity timeout (MySQL + Node.js)

Using docker-compose with ngnix, node backend and mysql.js.将 docker-compose 与 ngnix、节点后端和 mysql.js 一起使用。 This error has occurred:发生此错误:

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      | }

As another post mentioned, I tried changing the timeout and that didn't work.正如另一篇文章提到的,我尝试更改超时,但没有奏效。

I have to use a network to connect my docker-compose files.我必须使用网络连接我的 docker-compose 文件。 I've intermittently had it working without using an internal network, but other times I get the following message:我在不使用内部网络的情况下间歇性地让它工作,但其他时候我收到以下消息:

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

This itself is strange as there are only the 3 default networks (and one I have created).这本身很奇怪,因为只有 3 个默认网络(也是我创建的一个)。 Other posts have said that if you go over 30 or more networks this should become an issue -其他帖子说,如果您的 go 超过 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

So ie I've tried this in my docker-compose.yml所以我在我的 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

I've also tried this:我也试过这个:

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

Both fix the ipv4 error, but then I still get the handshake inactivity timeout error.两者都修复了 ipv4 错误,但我仍然收到握手不活动超时错误。 Someone suggested using network_mode: bridge , but shouldn't the network use the default bridge automatically?有人建议使用network_mode: bridge ,但网络不应该自动使用默认网桥吗? If anyone has any ideas please let me know.如果有人有任何想法,请告诉我。

PS: PS:

Here is the connection to the db - I tried adding a timeout to the connection handler - the timeout worked, but the connection still didn't have a handshake.这是与数据库的连接-我尝试向连接处理程序添加超时-超时有效,但连接仍然没有握手。

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", function(err,rows,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
}
         

EDIT:编辑:

I've written scripts to completely uninstall and completely reinstall docker (including all folders and subdirectories) and I still have the following error message:我编写了脚本来完全卸载并完全重新安装 docker(包括所有文件夹和子目录),但我仍然收到以下错误消息:

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

Here is script to remove all of docker:这是删除所有 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

And here is the script I use to install docker (which I confirm works with the hello world test) and comes from commands in this page of the documentation ( https://docs.docker.com/engine/install/ubuntu/ ):这是我用来安装 docker 的脚本(我确认它适用于 hello world 测试)并且来自文档的此页面中的命令( https://docs.Z05B6053C41A2130AFD6FC3B158BDA4E/6./comtu4E

#!/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 '-------------------------------------------------------------------------------'

EDIT:编辑:

Adding a links variable to tha api container in the docker-compose file didn't fix the issue:将链接变量添加到 docker-compose 文件中的 api 容器中并不能解决问题:

    networks:
      - underdev
    links:
      - db:db

EDIT:编辑:

Tried rebooting the server.尝试重新启动服务器。 That didn't work, same error.那没有用,同样的错误。 I've also tried upgrading the server via我也尝试通过升级服务器

sudo apt-get upgrade && sudo apt-get dist-upgrade

and running the application again - same error.并再次运行应用程序 - 同样的错误。

I'm currently running 18.04.5 ubuntu bionic:我目前正在运行 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

Tried upgrading to 20.04.1 and that also doesn't work.尝试升级到 20.04.1 也不起作用。


I solved the problem in another way.我用另一种方式解决了这个问题。 It wasn't an inbound issue.这不是入站问题。 I'm using 'Standard TCP/IP over SSH' in order to connect Mysql-server in AWS.我正在使用“基于 SSH 的标准 TCP/IP”来连接 AWS 中的 Mysql-server。 There is no problem normally.正常是没有问题的。 But suddenly it happended.但突然就发生了。

"Handshake inactivity timeout"!! “握手不活动超时”!!

I found out that another application is binding port 3306 of the development environment.我发现另一个应用程序正在绑定开发环境的3306端口。 You can use command 'netstat -anb'.您可以使用命令“netstat -anb”。 After deleting it and reconnecting 'Standard TCP/IP over SSH' again, the problem went away.删除它并再次重新连接“标准 TCP/IP over SSH”后,问题就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM