[英]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.