【问题标题】:Cannot connect to a MySQL database using NestJS and TypeORM无法使用 NestJS 和 TypeORM 连接到 MySQL 数据库
【发布时间】:2021-05-26 16:11:06
【问题描述】:

我正在尝试使用 NestJS 创建一个新项目,我想在 TypeORM 的帮助下使用 MySQL 数据库,但我无法让事情顺利进行。我正在学习这个非常简单的教程https://docs.nestjs.com/techniques/database#database,但我仍然无法连接到 MySQL 数据库。

我已经安装了所有依赖项:

"dependencies": {
    "@nestjs/common": "^7.5.1",
    "@nestjs/core": "^7.5.1",
    "@nestjs/platform-express": "^7.5.1",
    "@nestjs/typeorm": "^7.1.5",
    "mysql": "^2.18.1",
    "mysql2": "^2.2.5",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.6.3",
    "typeorm": "^0.2.31"
  }

在这个例子中我没有使用任何实体,但即使我尝试使用实体并且我在 app-module.ts 的“entities: []”中使用全局路径“dist/**/*”定义了它们.entity{ .ts,.js}",还是不行。

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [TypeOrmModule.forRoot({
    type: 'mysql',
    host: 'localhost',
    port: 3306,
    username: 'root',
    password: 'root',
    database: 'test',
    entities: ["dist/**/*.entity{.ts,.js}"],
    synchronize: true,
  })],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {
  constructor() {}
}

需要注意的一件非常重要的事情是,由于某种原因,我无法运行“typeorm”命令,就好像我从未安装过它,而它显然位于 node_modules 中。只有当我在它之前使用“npx”时,我才能运行“typeorm”命令。我查看了其他 stackoverflow 线程,但没有找到任何可以帮助我的东西。

我的项目是否因为某种原因我的系统找不到 typeorm 而无法运行?提前致谢。

编辑:

这也是我得到的错误。

[Nest] 14092   - 02/24/2021, 1:55:56 PM   [TypeOrmModule] Unable to connect to the database. Retrying (6)... +5082ms
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)

【问题讨论】:

  • ????????请在此处以纯文本形式发布代码、错误、示例数据或文本输出,而不是难以阅读的图像,不能复制粘贴以帮助测试代码或在答案中使用,并且是那些依赖的人的障碍在屏幕阅读器或翻译工具上。您可以编辑问题以在问题正文中添加代码。为了便于格式化,请使用{} 按钮标记代码块,或使用四个空格缩进以获得相同的效果。 屏幕截图的内容无法搜索、作为代码运行或复制和编辑以创建解决方案。
  • 我没有考虑到这一点,谢谢。已编辑。
  • 你确定mysql在你本地运行吗?

标签: mysql nestjs typeorm


【解决方案1】:

它可以帮你解决可能!您必须先安装 typeorm 模块。如果您安装了 typeorm 模块,请检查您的 typeorm 配置文件。这是 postgres 的一个示例。您必须使用 mysql

而不是 postgres
import { Global, Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { TodoEntity } from "src/entities/todo.entity";

@Global()
@Module({
    imports: [
        TypeOrmModule.forRootAsync({
            imports:[ConfigModule],
            useFactory: (configService: ConfigService) => ({
                type:'postgres',
                host:'localhost',
                port:5432,
                username:'postgres',
                password:'postgres',
                database:'postgres',
                schema:'public',
                synchronize: true,
                logging: true,
                entities:[TodoEntity],
            }),
            inject: [ConfigService],
        }),
    ],
})

export class TypeOrmConfigModule{}

您也可以从“https://typeorm.io/#/connection”获取帮助

【讨论】:

    【解决方案2】:

    我解决了我的问题。这是我的一个非常愚蠢的错误,因为我忘记在本地启动我的 MySQL 服务器,而为什么 NestJS 应用程序无法连接到它是完全有道理的。我认为问题出在我的代码中。

    抱歉浪费了大家的时间,感谢您的回答!

    【讨论】:

      猜你喜欢
      • 2019-08-13
      • 2021-10-19
      • 2020-04-25
      • 2020-11-25
      • 2022-08-03
      • 2019-08-02
      • 2020-07-21
      • 2020-01-25
      • 2021-12-03
      相关资源
      最近更新 更多