Не получается подключить nestjs приложение к базе данных mysql на хостинге с cpanel

Пытаюсь подключить mysql к nestjs приложению, но получаю ошибку на хостинге:

Error: connect ECONNREFUSED 172.31.1.100:3306

вот полная ошибка:

returncode: 1

stdout:

 server@0.0.1 start:prod /home/enebbvmz/server

 node main

[32m[Nest] 33942   - [39m06/13/2021, 4:31:13 PM   [38;5;3m[NestFactory] [39m[32mStarting Nest application...[39m

[32m[Nest] 33942   - [39m06/13/2021, 4:31:13 PM   [38;5;3m[InstanceLoader] [39m[32mTypeOrmModule dependencies initialized[39m[38;5;3m +254ms[39m

[32m[Nest] 33942   - [39m06/13/2021, 4:31:13 PM   [38;5;3m[InstanceLoader] [39m[32mMailerModule dependencies initialized[39m[38;5;3m +1ms[39m

[32m[Nest] 33942   - [39m06/13/2021, 4:31:13 PM   [38;5;3m[InstanceLoader] [39m[32mJwtModule dependencies initialized[39m[38;5;3m +9ms[39m

[32m[Nest] 33942   - [39m06/13/2021, 4:31:13 PM   [38;5;3m[InstanceLoader] [39m[32mMailerCoreModule dependencies initialized[39m[38;5;3m +1ms[39m

[32m[Nest] 33942   - [39m06/13/2021, 4:31:13 PM   [38;5;3m[InstanceLoader] [39m[32mAppModule dependencies initialized[39m[38;5;3m +1ms[39m

stderr:
npm WARN lifecycle The node binary used for scripts is /home/enebbvmz/nodevenv/server/12/bin/node but npm is using /opt/alt/alt-nodejs12/root/usr/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.

[31m[Nest] 33942   - [39m06/13/2021, 4:31:13 PM   [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (1)...[39m[38;5;3m +18ms[39m

Error: connect ECONNREFUSED 172.31.1.100:3306

    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)

    --------------------

    at Protocol._enqueue (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at PoolConnection.connect (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Connection.js:116:18)
    at Pool.getConnection (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Pool.js:48:16)
    at /home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:800:18
    at new Promise (<anonymous>)
    at MysqlDriver.createPool (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:797:16)
    at MysqlDriver.<anonymous> (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:279:51)
    at step (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:143:27)
    at Object.next (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:124:57)
[31m[Nest] 33942   - [39m06/13/2021, 4:31:16 PM   [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (2)...[39m[38;5;3m +3012ms[39m
Error: connect ECONNREFUSED 172.31.1.100:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
    --------------------
    at Protocol._enqueue (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at PoolConnection.connect (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Connection.js:116:18)
    at Pool.getConnection (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Pool.js:48:16)
    at /home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:800:18
    at new Promise (<anonymous>)
    at MysqlDriver.createPool (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:797:16)
    at MysqlDriver.<anonymous> (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:279:51)
    at step (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:143:27)
    at Object.next (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:124:57)
[31m[Nest] 33942   - [39m06/13/2021, 4:31:19 PM   [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (3)...[39m[38;5;3m +3008ms[39m
Error: connect ECONNREFUSED 172.31.1.100:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
    --------------------
    at Protocol._enqueue (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at PoolConnection.connect (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Connection.js:116:18)
    at Pool.getConnection (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Pool.js:48:16)
    at /home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:800:18
    at new Promise (<anonymous>)
    at MysqlDriver.createPool (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:797:16)
    at MysqlDriver.<anonymous> (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:279:51)
    at step (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:143:27)
    at Object.next (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:124:57)
[31m[Nest] 33942   - [39m06/13/2021, 4:31:22 PM   [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (4)...[39m[38;5;3m +3006ms[39m
Error: connect ECONNREFUSED 172.31.1.100:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
    --------------------
    at Protocol._enqueue (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at PoolConnection.connect (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Connection.js:116:18)
    at Pool.getConnection (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Pool.js:48:16)
    at /home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:800:18
    at new Promise (<anonymous>)
    at MysqlDriver.createPool (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:797:16)
    at MysqlDriver.<anonymous> (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:279:51)
    at step (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:143:27)
    at Object.next (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:124:57)
[31m[Nest] 33942   - [39m06/13/2021, 4:31:25 PM   [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (5)...[39m[38;5;3m +3009ms[39m
Error: connect ECONNREFUSED 172.31.1.100:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
    --------------------
    at Protocol._enqueue (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at PoolConnection.connect (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Connection.js:116:18)
    at Pool.getConnection (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Pool.js:48:16)
    at /home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:800:18
    at new Promise (<anonymous>)
    at MysqlDriver.createPool (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:797:16)
    at MysqlDriver.<anonymous> (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:279:51)
    at step (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:143:27)
    at Object.next (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:124:57)
[31m[Nest] 33942   - [39m06/13/2021, 4:31:28 PM   [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (6)...[39m[38;5;3m +3010ms[39m
Error: connect ECONNREFUSED 172.31.1.100:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
    --------------------
    at Protocol._enqueue (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at PoolConnection.connect (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Connection.js:116:18)
    at Pool.getConnection (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Pool.js:48:16)
    at /home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:800:18
    at new Promise (<anonymous>)
    at MysqlDriver.createPool (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:797:16)
    at MysqlDriver.<anonymous> (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:279:51)
    at step (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:143:27)
    at Object.next (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:124:57)
[31m[Nest] 33942   - [39m06/13/2021, 4:31:31 PM   [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (7)...[39m[38;5;3m +3008ms[39m
Error: connect ECONNREFUSED 172.31.1.100:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
    --------------------
    at Protocol._enqueue (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at PoolConnection.connect (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Connection.js:116:18)
    at Pool.getConnection (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Pool.js:48:16)
    at /home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:800:18
    at new Promise (<anonymous>)
    at MysqlDriver.createPool (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:797:16)
    at MysqlDriver.<anonymous> (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:279:51)
    at step (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:143:27)
    at Object.next (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:124:57)
[31m[Nest] 33942   - [39m06/13/2021, 4:31:34 PM   [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (8)...[39m[38;5;3m +3006ms[39m
Error: connect ECONNREFUSED 172.31.1.100:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
    --------------------
    at Protocol._enqueue (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at PoolConnection.connect (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Connection.js:116:18)
    at Pool.getConnection (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Pool.js:48:16)
    at /home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:800:18
    at new Promise (<anonymous>)
    at MysqlDriver.createPool (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:797:16)
    at MysqlDriver.<anonymous> (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:279:51)
    at step (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:143:27)
    at Object.next (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:124:57)
[31m[Nest] 33942   - [39m06/13/2021, 4:31:37 PM   [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (9)...[39m[38;5;3m +3008ms[39m
Error: connect ECONNREFUSED 172.31.1.100:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
    --------------------
    at Protocol._enqueue (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at PoolConnection.connect (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Connection.js:116:18)
    at Pool.getConnection (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Pool.js:48:16)
    at /home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:800:18
    at new Promise (<anonymous>)
    at MysqlDriver.createPool (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:797:16)
    at MysqlDriver.<anonymous> (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:279:51)
    at step (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:143:27)
    at Object.next (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:124:57)
[31m[Nest] 33942   - [39m06/13/2021, 4:31:37 PM   [38;5;3m[ExceptionHandler] [39m[31mconnect ECONNREFUSED 172.31.1.100:3306[39m[38;5;3m +2ms[39m
Error: connect ECONNREFUSED 172.31.1.100:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
    --------------------
    at Protocol._enqueue (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at PoolConnection.connect (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Connection.js:116:18)
    at Pool.getConnection (/home/enebbvmz/nodevenv/server/12/lib/node_modules/mysql/lib/Pool.js:48:16)
    at /home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:800:18
    at new Promise (<anonymous>)
    at MysqlDriver.createPool (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:797:16)
    at MysqlDriver.<anonymous> (/home/enebbvmz/nodevenv/server/12/lib/node_modules/typeorm/driver/mysql/MysqlDriver.js:279:51)
    at step (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:143:27)
    at Object.next (/home/enebbvmz/nodevenv/server/12/lib/node_modules/tslib/tslib.js:124:57)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! server@0.0.1 start:prod: `node main`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the server@0.0.1 start:prod script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/enebbvmz/.npm/_logs/2021-06-13T12_31_37_897Z-debug.log

параметры подключения проверял, всё правильно

import { TypeOrmModuleOptions } from "@nestjs/typeorm";


export const config: TypeOrmModuleOptions = {
    type: 'mysql',
    host: 'localhost',
    port:  3306,
    username:  'USERNAME',
    password: 'PASSWORD',
    database: 'DATABASE',
    entities: ['dist/**/*.entity{.ts,.js}'],
    synchronize: true,
}

пробовал подключиться к mysql в https://www.freemysqlhosting.net, всё нормально подключалось typeorm автоматически создал таблицы․

Ты точно уверен, что DB запущена локально и таблица существует?

Оба приложения (nodejs, myslq) запускаются на одном и том же хосте? Есть ли настройки firewall на хосте где запущена mysql? Пробовал ли подключиться к mysql напрямую: через публичный айпишник хоста на котором она запущена?

насчёт запущена или нет не уверен, говорил с владельцем хостинга, он сказал, что на shared хостинге бд всегда запущена,
а таблицы typeorm создаст автоматически

да оба приложения на одном хосте, но почему-то бд не подключается, пробовал отдельный сервис для бд. Из хостинга нормально подключается на этот отдельный сервис, настройки файрвол я так и не нашёл, скорее всего оно отсутствует,
не совсем понял про публичный ip, мошеь по конкретнее сказать где найти это?

Знаешь домен по которому можно открыть php сайт что запущен на этом же самом хосте что и mysql? Попробуй айпи который резолвится для этого адреса.

Например есть домен forum.jscourse.com, и он резолвится в айпишник 95.217.3.104. Я обычно проверяю ping-ом в терминале.


Но могут быть другие пути.

И еще мысль. Попробуй сделать простой php скрипт (это же ж php хостинг) и попробовать подключиться к базе через него. Если не подключится, то дело может быть в самой базе или логин-пароле. Если подключится, значит это настройки хостинга (на которые ты возможно не сможешь повлиять).

связался с владельцем хостинга, после длительных разговоров оказалось что он тоже не в курсах как проблему решить, так что я просто забил на это и поменял базу с typeorm на mongoose, тоже своего рода решение))

1 симпатия