I am trying to create a new project using NestJS and I want to use a MySQL database with the help of TypeORM but I can't get things going anywhere. I am following this very simple tutorial https://docs.nestjs.com/techniques/database#database and I am still unable to connect to a MySQL database.
I've installed all the dependencies:
"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"
}
I don't use any entities in this example, but even when I tried to used entities and I defined them in "entities: []" in app-module.ts with a global path "dist/**/*.entity{ .ts,.js}", it still didn't work.
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() {}
}
One very important thing to note is that for some reason, I cannot run "typeorm" commands, as if I've never installed it when it clearly is in node_modules. I can run a "typeorm" command only if I use "npx" before it. I peeked at other stackoverflow threads but I didn't find anything that could help me.
Is my project unable to run because for some reason my system cannot find typeorm? Thanks in advance.
Edit:
Here's also the error that I get.
[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)
{}button to mark blocks of code, or indent with four spaces for the same effect. The contents of a screenshot can’t be searched, run as code, or copied and edited to create a solution.