1

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)
3
  • 1
    🚫📸 Please post code, errors, sample data or textual output here as plain-text, not as images that can be hard to read, can’t be copy-pasted to help test code or use in answers, and are barrier to those who depend on screen readers or translation tools. You can edit your question to add the code in the body of your question. For easy formatting use the {} 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. Commented Feb 24, 2021 at 10:46
  • 1
    I didn't consider that, thanks. Edited. Commented Feb 24, 2021 at 11:05
  • 1
    Are you sure that the mysql is running on your local? Commented Feb 24, 2021 at 11:20

2 Answers 2

2

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.