使用 NestJs 和 Mysql 检查连接是否成功
Check if connection was successful using NestJs and Mysql
标题不言自明,我开始学习 NestJS,我想知道是否有可能,例如 console.log 或其他方式,知道与数据库的连接是否成功。
当我 运行 npm run start:dev
时,一切都编译正常,但我仍然不确定我是否能够连接到数据库。
注意:我正在使用import { TypeOrmModule } from '@nestjs/typeorm/dist/typeorm.module';
编辑:如果在运行安装您的应用程序(在您的终端上)后出现此行
TypeOrmModule dependencies initialized
这意味着您已成功建立与数据库的连接
import { Connection, createConnection, getConnectionManager } from 'typeorm';
const connectionManager = getConnectionManager();
try {
getConnection(connection.name);
} catch (error) {
await createConnection(connection);
}
如果您的连接失败,您将在您的控制台中收到一个非常明确的错误:
[Nest] 1274 - 04/09/2021, 11:24:52 AM [TypeOrmModule] Unable to connect to the database. Retrying (1)... +2042ms
Error: getaddrinfo ENOTFOUND localhast
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)
或
Unable to connect to the database. Retrying (2)... +3009ms
error: password authentication failed for user "mysql"
at Parser.parseErrorMessage (nest/node_modules/pg-protocol/src/parser.ts:357:11)
at Parser.handlePacket (nest/node_modules/pg-protocol/src/parser.ts:186:21)
at Parser.parse (nest/node_modules/pg-protocol/src/parser.ts:101:30)
at Socket.<anonymous> (nest/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (events.js:315:20)
at Socket.EventEmitter.emit (domain.js:483:12)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at Socket.Readable.push (_stream_readable.js:212:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
或
[Nest] 1412 - 04/09/2021, 11:32:36 AM [TypeOrmModule] Unable to connect to the database. Retrying (2)... +3008ms
error: database "example" does not exist
at Parser.parseErrorMessage (nest/node_modules/pg-protocol/src/parser.ts:357:11)
at Parser.handlePacket (nest/node_modules/pg-protocol/src/parser.ts:186:21)
at Parser.parse (nest/node_modules/pg-protocol/src/parser.ts:101:30)
at Socket.<anonymous> (nest/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (events.js:315:20)
at Socket.EventEmitter.emit (domain.js:483:12)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at Socket.Readable.push (_stream_readable.js:212:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
没有消息表示成功
标题不言自明,我开始学习 NestJS,我想知道是否有可能,例如 console.log 或其他方式,知道与数据库的连接是否成功。
当我 运行 npm run start:dev
时,一切都编译正常,但我仍然不确定我是否能够连接到数据库。
注意:我正在使用import { TypeOrmModule } from '@nestjs/typeorm/dist/typeorm.module';
编辑:如果在运行安装您的应用程序(在您的终端上)后出现此行
TypeOrmModule dependencies initialized
这意味着您已成功建立与数据库的连接
import { Connection, createConnection, getConnectionManager } from 'typeorm';
const connectionManager = getConnectionManager();
try {
getConnection(connection.name);
} catch (error) {
await createConnection(connection);
}
如果您的连接失败,您将在您的控制台中收到一个非常明确的错误:
[Nest] 1274 - 04/09/2021, 11:24:52 AM [TypeOrmModule] Unable to connect to the database. Retrying (1)... +2042ms
Error: getaddrinfo ENOTFOUND localhast
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)
或
Unable to connect to the database. Retrying (2)... +3009ms
error: password authentication failed for user "mysql"
at Parser.parseErrorMessage (nest/node_modules/pg-protocol/src/parser.ts:357:11)
at Parser.handlePacket (nest/node_modules/pg-protocol/src/parser.ts:186:21)
at Parser.parse (nest/node_modules/pg-protocol/src/parser.ts:101:30)
at Socket.<anonymous> (nest/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (events.js:315:20)
at Socket.EventEmitter.emit (domain.js:483:12)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at Socket.Readable.push (_stream_readable.js:212:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
或
[Nest] 1412 - 04/09/2021, 11:32:36 AM [TypeOrmModule] Unable to connect to the database. Retrying (2)... +3008ms
error: database "example" does not exist
at Parser.parseErrorMessage (nest/node_modules/pg-protocol/src/parser.ts:357:11)
at Parser.handlePacket (nest/node_modules/pg-protocol/src/parser.ts:186:21)
at Parser.parse (nest/node_modules/pg-protocol/src/parser.ts:101:30)
at Socket.<anonymous> (nest/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (events.js:315:20)
at Socket.EventEmitter.emit (domain.js:483:12)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at Socket.Readable.push (_stream_readable.js:212:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
没有消息表示成功