Reputation: 71
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)
Upvotes: 1
Views: 3764
Reputation: 71
I fixed my problem. It was a very stupid mistake of mine because because I forgot to start my MySQL server locally and it makes perfect sense why the NestJS app couldn't connect to it. I thought the problem was somewhere in my code.
Sorry for wasting everyone's time, I appreciate your answers!
Upvotes: 2
Reputation: 71
It could help you to solve may be! You have to install typeorm module first. If you installed typeorm module then check your typeorm config file. Here is one example with postgres. Instead of postgres you have to use mysql
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{}
You can also take help from "https://typeorm.io/#/connection"
Upvotes: 1