简体   繁体   English

TypeORM 与 MySQL ECONNREFUSED 10.104.38.229:3306 在 kubernetes pod 上运行时

[英]TypeORM with MySQL ECONNREFUSED 10.104.38.229:3306 when running on kubernetes pod

I am trying to create a simple NodeJs app to connect with a MySQL database.我正在尝试创建一个简单的 NodeJs 应用程序来连接 MySQL 数据库。 The app and MySQL DB both are hosted inside of a Kubernetes cluster.该应用程序和 MySQL DB 都托管在 Kubernetes 集群内。 I am using the K8s service to connect the app to the MySQL pod.我正在使用 K8s 服务将应用程序连接到 MySQL pod。

When I use the MySQL adapter for the connection it works fine.当我使用 MySQL 适配器进行连接时,它工作正常。 However, when I connect it via Typeorm library, it throws an ECONNREFUSED on the first attempt .但是,当我通过 Typeorm 库连接它时,它会在第一次尝试时抛出 ECONNREFUSED 。 However, When I restart the app the connection get establishes.但是,当我重新启动应用程序时,连接就会建立。

can anyone tell me why I'm seeing this strange behavior and how can I solve this problem.谁能告诉我为什么我会看到这种奇怪的行为以及如何解决这个问题。

my error is like this:我的错误是这样的:

[server] Error: connect ECONNREFUSED 10.104.38.229:3306
[server]     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1138:16) {
[server]   errno: -111,
[server]   code: 'ECONNREFUSED',
[server]   syscall: 'connect',
[server]   address: '10.104.38.229',
[server]   port: 3306,
[server]   fatal: true
[server] }

my index.ts is like this:我的 index.ts 是这样的:

import { app } from './app';
import {
  createConnection,
  createConnections,
  getConnectionManager,
} from 'typeorm';
import mysql from 'mysql';
const port = 3000;

const start = async () => {
  try {
    const connectionManager = getConnectionManager();
    const connections = connectionManager.create({
      type: 'mysql',
      host: 'xcute-mysql-srv',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'test',
    });
    await connections.connect();
    console.log('Succesfully Connected');

    // const connection = mysql.createConnection({
    //   host: 'xcute-mysql-srv',
    //   port: 3306,
    //   user: 'root',
    //   password: 'root',
    //   database: 'test',
    // });
    // connection.connect(() => {
    //   console.log('Succesfully Connected');
    // });
  } catch (error) {
    console.log(error);
  }
  app.listen(port, () => console.log(`Listening at port ${port} !!`));
};

start();

Edit1:编辑1:

   await createConnection({
      type: 'mysql',
      host: 'xcute-mysql-srv',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'test',
    });
    console.log('Succesfully Connected');

Also throwing the same error也抛出同样的错误

Have you tried creating the connection using the createConnection function instead of calling the ConnectionManager create method?您是否尝试过使用createConnection function 而不是调用 ConnectionManager 创建方法来创建连接? This is how I do it in my own application and it works fine:这就是我在自己的应用程序中执行此操作的方式,并且效果很好:

const start = async () => {
  try {
    await createConnection({
      type: 'mysql',
      host: 'xcute-mysql-srv',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'test',
    });
    console.log('Succesfully Connected');

  } catch (error) {
    console.log(error);
  }
  app.listen(port, () => console.log(`Listening at port ${port} !!`));
};

start();

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

相关问题 错误:connect ECONNREFUSED::1:3306 是当我尝试在 nestjs 中使用 typeorm 时 - Error: connect ECONNREFUSED ::1:3306 is when i try to use typeorm in nestjs 节点+ MySQL [ECONNREFUSED 127.0.0.1:3306] - Node + MySQL [ECONNREFUSED 127.0.0.1:3306] Docker Compose 使用 MySQL 和 NodeJS 获取错误 ECONNREFUSED 127.0.0.1:3306 - Docker Compose getting error ECONNREFUSED 127.0.0.1:3306 with MySQL and NodeJS 连接到 mysql 时出现错误“connect ECONNREFUSED 127.0.0.1:3306” - Getting error "connect ECONNREFUSED 127.0.0.1:3306" while connecting to mysql 错误:在 MySQL、Heroku 和 Node.js 上连接 ECONNREFUSED 127.0.0.1:3306 - Error: connect ECONNREFUSED 127.0.0.1:3306 on MySQL, Heroku, and Node.js 使用mysql运行将ecto.migrate与phoenix混合时,econnrefused - econnrefused when running mix ecto.migrate with phoenix, using mysql [SequelizeConnectionRefusedError]:连接ECONNREFUSED 127.0.0.1:3306 - [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:3306 SequelizeConnectionRefusedError:连接 ECONNREFUSED 127.0.0.1:3306 - SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306 使用MySQL和PHP创建Kubernetes Pod? - Create Kubernetes Pod with MySQL and PHP? Kubernetes MySQL pod 卡住了 CrashLoopBackOff - Kubernetes MySQL pod stuck with CrashLoopBackOff
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM