Question

Can't connect to mysql with sequelize

My NodeJS app, can’t connect to mysql managed database using Sequelize ORM library. Surprisingly I’m able to make direct connection from bash shell like: mysql -u -p -h [randolympics-mysql-database-do-user-17547011-0.l.db.ondigitalocean.com](https://randolympics-mysql-database-do-user-17547011-0.l.db.ondigitalocean.com/) -P 25060 -D randolympics

But when trying to do same, with Sequelize it gives me timeout error (from same server, so it already belongs to trusted sources if it’s able to make connection via bash)

I’ve downloaded CA certificate and placed it in same folder where config.js lies. ( /data/config.js , and /data/ca-certificate.crt ) And code for making Sequelize MySQL connection is like:

const path = require('path');
const fs = require('fs');


const config = {
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
dialect: 'mysql',
port: process.env.PORT_MYSQL,
ssl: true,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false,
ca: fs.readFileSync(path.join(__dirname, 'ca-certificate.crt')),
},
},

pool: {
max: 5,
min: 0,
acquire: 60000,
idle: 10000
}
};

module.exports = config;

I’ve triple checked env variables are proper and correct in app platform for backend, also when trying from that same server (that can connect with bash shell), it also timeouts.

I’ve also made sure to use Legacy– MySQL 5.x Password authentication for user I’m trying to connect to, as Sequelize doesn’t support newer password authentication 8

So here’s logs of it:

[randolympics-backend] [2024-12-22 00:20:18] ╭──────────── git repo clone ───────────╼
[randolympics-backend] [2024-12-22 00:20:18] │  › fetching app source code
[randolympics-backend] [2024-12-22 00:20:18] │ => Selecting branch "main"
[randolympics-backend] [2024-12-22 00:20:19] │ => Checking out commit "78bd39b57ad64af578e036bfa502d3127ac6a3ce"
[randolympics-backend] [2024-12-22 00:20:19] │
[randolympics-backend] [2024-12-22 00:20:19] │  ✔ cloned repo to /workspace
[randolympics-backend] [2024-12-22 00:20:19] ╰────────────────────────────────────────╼
[randolympics-backend] [2024-12-22 00:20:19]
[randolympics-backend] [2024-12-22 00:20:19]  › configuring build-time app environment variables:
[randolympics-backend] [2024-12-22 00:20:19]      PORT_MYSQL PASS_email STRIPE_TEST_API PAYPAL_CLIENT_SECRET SERVICE STRIPE_PUBLIC_KEY USER PASSWORD JWT_SECRET STRIPE_SECRET_KEY PORT EMAIL_PORT SITE_SECRET BASE_URL S3_BUCKET_SECRET_KEY JWT_REFRESH_SECRET PRODUCTION STRIPE_WEBHOOK_SECRET S3_BUCKET_NAME USER_email S3_BUCKET_ACCESS_KEY HOST DATABASE BASE_URL_BACKEND SECURE PAYPAL_CLIENT_ID
[randolympics-backend] [2024-12-22 00:20:19]
[randolympics-backend] [2024-12-22 00:20:19] ╭──────────── buildpack detection ───────────╼
[randolympics-backend] [2024-12-22 00:20:19] │  › using Ubuntu 22.04 stack
[randolympics-backend] [2024-12-22 00:20:20] │ Detected the following buildpacks suitable to build your app:
[randolympics-backend] [2024-12-22 00:20:20] │
[randolympics-backend] [2024-12-22 00:20:20] │    digitalocean/nodejs-appdetect  v0.0.3
[randolympics-backend] [2024-12-22 00:20:20] │    heroku/nodejs                  v0.260.4  (Node.js)
[randolympics-backend] [2024-12-22 00:20:20] │    digitalocean/procfile          v0.0.5    (Procfile)
[randolympics-backend] [2024-12-22 00:20:20] │    digitalocean/custom            v0.1.3    (Custom Build Command)
[randolympics-backend] [2024-12-22 00:20:20] ╰─────────────────────────────────────────────╼
[randolympics-backend] [2024-12-22 00:20:20]
[randolympics-backend] [2024-12-22 00:20:20] ╭──────────── app build ───────────╼
[randolympics-backend] [2024-12-22 00:20:21] │
[randolympics-backend] [2024-12-22 00:20:21] │ -----> Creating runtime environment
[randolympics-backend] [2024-12-22 00:20:21] │
[randolympics-backend] [2024-12-22 00:20:21] │        NPM_CONFIG_LOGLEVEL=error
[randolympics-backend] [2024-12-22 00:20:21] │        NODE_VERBOSE=false
[randolympics-backend] [2024-12-22 00:20:21] │        NODE_ENV=
[randolympics-backend] [2024-12-22 00:20:21] │        NODE_MODULES_CACHE=true
[randolympics-backend] [2024-12-22 00:20:21] │
[randolympics-backend] [2024-12-22 00:20:21] │ -----> Installing binaries
[randolympics-backend] [2024-12-22 00:20:21] │        engines.node (package.json):   unspecified
[randolympics-backend] [2024-12-22 00:20:21] │        engines.npm (package.json):    unspecified (use default)
[randolympics-backend] [2024-12-22 00:20:21] │
[randolympics-backend] [2024-12-22 00:20:21] │        Resolving node version 20.x...
[randolympics-backend] [2024-12-22 00:20:21] │        Downloading and installing node 20.15.1...
[randolympics-backend] [2024-12-22 00:20:25] │        Using default npm version: 10.7.0
[randolympics-backend] [2024-12-22 00:20:26] │
[randolympics-backend] [2024-12-22 00:20:26] │ -----> Installing dependencies
[randolympics-backend] [2024-12-22 00:20:26] │        Installing node modules
[randolympics-backend] [2024-12-22 00:20:38] │
[randolympics-backend] [2024-12-22 00:20:38] │        added 705 packages, and audited 706 packages in 11s
[randolympics-backend] [2024-12-22 00:20:38] │
[randolympics-backend] [2024-12-22 00:20:38] │        78 packages are looking for funding
[randolympics-backend] [2024-12-22 00:20:38] │          run `npm fund` for details
[randolympics-backend] [2024-12-22 00:20:38] │
[randolympics-backend] [2024-12-22 00:20:38] │        4 vulnerabilities (3 moderate, 1 high)
[randolympics-backend] [2024-12-22 00:20:38] │
[randolympics-backend] [2024-12-22 00:20:38] │        To address all issues, run:
[randolympics-backend] [2024-12-22 00:20:38] │          npm audit fix
[randolympics-backend] [2024-12-22 00:20:38] │
[randolympics-backend] [2024-12-22 00:20:38] │        Run `npm audit` for details.
[randolympics-backend] [2024-12-22 00:20:38] │        npm notice
[randolympics-backend] [2024-12-22 00:20:38] │        npm notice New major version of npm available! 10.7.0 -> 11.0.0
[randolympics-backend] [2024-12-22 00:20:38] │        npm notice Changelog: https://github.com/npm/cli/releases/tag/v11.0.0
[randolympics-backend] [2024-12-22 00:20:38] │        npm notice To update run: npm install -g npm@11.0.0
[randolympics-backend] [2024-12-22 00:20:38] │        npm notice
[randolympics-backend] [2024-12-22 00:20:38] │
[randolympics-backend] [2024-12-22 00:20:38] │ -----> Build
[randolympics-backend] [2024-12-22 00:20:38] │
[randolympics-backend] [2024-12-22 00:20:38] │ -----> Caching build
[randolympics-backend] [2024-12-22 00:20:38] │        - npm cache
[randolympics-backend] [2024-12-22 00:20:38] │
[randolympics-backend] [2024-12-22 00:20:38] │ -----> Pruning devDependencies
[randolympics-backend] [2024-12-22 00:20:38] │        Skipping because NODE_ENV is not 'production'
[randolympics-backend] [2024-12-22 00:20:38] │
[randolympics-backend] [2024-12-22 00:20:38] │ -----> Build succeeded!
[randolympics-backend] [2024-12-22 00:20:39] ╰───────────────────────────────────╼
[randolympics-backend] [2024-12-22 00:20:39]
[randolympics-backend] [2024-12-22 00:20:39] ╭──────────── app upload ───────────╼
[randolympics-backend] [2024-12-22 00:20:39] │  › uploading app container image to DOCR
[randolympics-backend] [2024-12-22 00:20:40] │ Adding layer 'heroku/nodejs:profile'
[randolympics-backend] [2024-12-22 00:20:47] │ Adding 1/1 app layer(s)
[randolympics-backend] [2024-12-22 00:20:47] │ Adding layer 'launcher'
[randolympics-backend] [2024-12-22 00:20:47] │ Adding layer 'config'
[randolympics-backend] [2024-12-22 00:20:47] │ Adding label 'io.buildpacks.lifecycle.metadata'
[randolympics-backend] [2024-12-22 00:20:47] │ Adding label 'io.buildpacks.build.metadata'
[randolympics-backend] [2024-12-22 00:20:47] │ Adding label 'io.buildpacks.project.metadata'
[randolympics-backend] [2024-12-22 00:20:47] │ Saving <image-1>...
[randolympics-backend] [2024-12-22 00:20:53] │ *** Images (sha256:0b1b6aef1ee2ef18d1306fd5db3774a201dd557e09619df53538d375a736940c):
[randolympics-backend] [2024-12-22 00:20:53] │       <image-2>
[randolympics-backend] [2024-12-22 00:20:53] │ Adding cache layer 'heroku/nodejs:shim'
[randolympics-backend] [2024-12-22 00:20:56] │  ✔ uploaded app image to DOCR
[randolympics-backend] [2024-12-22 00:20:56] ╰────────────────────────────────────╼
[randolympics-backend] [2024-12-22 00:20:56]
[randolympics-backend] [2024-12-22 00:20:56]  ✔  build complete
[randolympics-backend] [2024-12-22 00:20:56]
[randolympics-backend] [2024-12-22 00:21:51] [22/12/2024, 01:21:51] ErrorSequelizeConnectionError: connect ETIMEDOUT
[randolympics-backend] [2024-12-22 00:21:51] /workspace/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:102
[randolympics-backend] [2024-12-22 00:21:51]           throw new SequelizeErrors.ConnectionError(err);
[randolympics-backend] [2024-12-22 00:21:51]                 ^
[randolympics-backend] [2024-12-22 00:21:51]
[randolympics-backend] [2024-12-22 00:21:51] ConnectionError [SequelizeConnectionError]: connect ETIMEDOUT
[randolympics-backend] [2024-12-22 00:21:51]     at ConnectionManager.connect (/workspace/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:102:17)
[randolympics-backend] [2024-12-22 00:21:51]     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[randolympics-backend] [2024-12-22 00:21:51]     at async ConnectionManager._connect (/workspace/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:222:24)
[randolympics-backend] [2024-12-22 00:21:51]     at async /workspace/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:174:32 {
[randolympics-backend] [2024-12-22 00:21:51]   parent: Error: connect ETIMEDOUT
[randolympics-backend] [2024-12-22 00:21:51]       at Connection._handleTimeoutError (/workspace/node_modules/mysql2/lib/connection.js:205:17)
[randolympics-backend] [2024-12-22 00:21:51]       at listOnTimeout (node:internal/timers:573:17)
[randolympics-backend] [2024-12-22 00:21:51]       at process.processTimers (node:internal/timers:514:7) {
[randolympics-backend] [2024-12-22 00:21:51]     errorno: 'ETIMEDOUT',
[randolympics-backend] [2024-12-22 00:21:51]     code: 'ETIMEDOUT',
[randolympics-backend] [2024-12-22 00:21:51]     syscall: 'connect',
[randolympics-backend] [2024-12-22 00:21:51]     fatal: true
[randolympics-backend] [2024-12-22 00:21:51]   },
[randolympics-backend] [2024-12-22 00:21:51]   original: Error: connect ETIMEDOUT
[randolympics-backend] [2024-12-22 00:21:51]       at Connection._handleTimeoutError (/workspace/node_modules/mysql2/lib/connection.js:205:17)
[randolympics-backend] [2024-12-22 00:21:51]       at listOnTimeout (node:internal/timers:573:17)
[randolympics-backend] [2024-12-22 00:21:51]       at process.processTimers (node:internal/timers:514:7) {
[randolympics-backend] [2024-12-22 00:21:51]     errorno: 'ETIMEDOUT',
[randolympics-backend] [2024-12-22 00:21:51]     code: 'ETIMEDOUT',
[randolympics-backend] [2024-12-22 00:21:51]     syscall: 'connect',
[randolympics-backend] [2024-12-22 00:21:51]     fatal: true
[randolympics-backend] [2024-12-22 00:21:51]   }
[randolympics-backend] [2024-12-22 00:21:51] }
[randolympics-backend] [2024-12-22 00:21:51]
[randolympics-backend] [2024-12-22 00:21:51] Node.js v20.15.1
[randolympics-backend] [] exited with exit code 1
[randolympics-backend] [2024-12-22 00:22:09] [22/12/2024, 01:22:09] ErrorSequelizeConnectionError: connect ETIMEDOUT
[randolympics-backend] [2024-12-22 00:22:09] /workspace/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:102
[randolympics-backend] [2024-12-22 00:22:09]           throw new SequelizeErrors.ConnectionError(err);
[randolympics-backend] [2024-12-22 00:22:09]                 ^
[randolympics-backend] [2024-12-22 00:22:09]
[randolympics-backend] [2024-12-22 00:22:09] ConnectionError [SequelizeConnectionError]: connect ETIMEDOUT
[randolympics-backend] [2024-12-22 00:22:09]     at ConnectionManager.connect (/workspace/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:102:17)
[randolympics-backend] [2024-12-22 00:22:09]     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[randolympics-backend] [2024-12-22 00:22:09]     at async ConnectionManager._connect (/workspace/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:222:24)
[randolympics-backend] [2024-12-22 00:22:09]     at async /workspace/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:174:32 {
[randolympics-backend] [2024-12-22 00:22:09]   parent: Error: connect ETIMEDOUT
[randolympics-backend] [2024-12-22 00:22:09]       at Connection._handleTimeoutError (/workspace/node_modules/mysql2/lib/connection.js:205:17)
[randolympics-backend] [2024-12-22 00:22:09]       at listOnTimeout (node:internal/timers:573:17)
[randolympics-backend] [2024-12-22 00:22:09]       at process.processTimers (node:internal/timers:514:7) {
[randolympics-backend] [2024-12-22 00:22:09]     errorno: 'ETIMEDOUT',
[randolympics-backend] [2024-12-22 00:22:09]     code: 'ETIMEDOUT',
[randolympics-backend] [2024-12-22 00:22:09]     syscall: 'connect',
[randolympics-backend] [2024-12-22 00:22:09]     fatal: true
[randolympics-backend] [2024-12-22 00:22:09]   },
[randolympics-backend] [2024-12-22 00:22:09]   original: Error: connect ETIMEDOUT
[randolympics-backend] [2024-12-22 00:22:09]       at Connection._handleTimeoutError (/workspace/node_modules/mysql2/lib/connection.js:205:17)
[randolympics-backend] [2024-12-22 00:22:09]       at listOnTimeout (node:internal/timers:573:17)
[randolympics-backend] [2024-12-22 00:22:09]       at process.processTimers (node:internal/timers:514:7) {
[randolympics-backend] [2024-12-22 00:22:09]     errorno: 'ETIMEDOUT',
[randolympics-backend] [2024-12-22 00:22:09]     code: 'ETIMEDOUT',
[randolympics-backend] [2024-12-22 00:22:09]     syscall: 'connect',
[randolympics-backend] [2024-12-22 00:22:09]     fatal: true
[randolympics-backend] [2024-12-22 00:22:09]   }
[randolympics-backend] [2024-12-22 00:22:09] }
[randolympics-backend] [2024-12-22 00:22:09]
[randolympics-backend] [2024-12-22 00:22:09] Node.js v20.15.1
[randolympics-backend] [] exited with exit code 1
[randolympics-backend] [2024-12-22 00:22:37] [22/12/2024, 01:22:37] ErrorSequelizeConnectionError: connect ETIMEDOUT
[randolympics-backend] [2024-12-22 00:22:37] /workspace/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:102
[randolympics-backend] [2024-12-22 00:22:37]           throw new SequelizeErrors.ConnectionError(err);
[randolympics-backend] [2024-12-22 00:22:37]                 ^
[randolympics-backend] [2024-12-22 00:22:37]
[randolympics-backend] [2024-12-22 00:22:37] ConnectionError [SequelizeConnectionError]: connect ETIMEDOUT
[randolympics-backend] [2024-12-22 00:22:37]     at ConnectionManager.connect (/workspace/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:102:17)
[randolympics-backend] [2024-12-22 00:22:37]     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[randolympics-backend] [2024-12-22 00:22:37]     at async ConnectionManager._connect (/workspace/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:222:24)
[randolympics-backend] [2024-12-22 00:22:37]     at async /workspace/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:174:32 {
[randolympics-backend] [2024-12-22 00:22:37]   parent: Error: connect ETIMEDOUT
[randolympics-backend] [2024-12-22 00:22:37]       at Connection._handleTimeoutError (/workspace/node_modules/mysql2/lib/connection.js:205:17)
[randolympics-backend] [2024-12-22 00:22:37]       at listOnTimeout (node:internal/timers:573:17)
[randolympics-backend] [2024-12-22 00:22:37]       at process.processTimers (node:internal/timers:514:7) {
[randolympics-backend] [2024-12-22 00:22:37]     errorno: 'ETIMEDOUT',
[randolympics-backend] [2024-12-22 00:22:37]     code: 'ETIMEDOUT',
[randolympics-backend] [2024-12-22 00:22:37]     syscall: 'connect',
[randolympics-backend] [2024-12-22 00:22:37]     fatal: true
[randolympics-backend] [2024-12-22 00:22:37]   },
[randolympics-backend] [2024-12-22 00:22:37]   original: Error: connect ETIMEDOUT
[randolympics-backend] [2024-12-22 00:22:37]       at Connection._handleTimeoutError (/workspace/node_modules/mysql2/lib/connection.js:205:17)
[randolympics-backend] [2024-12-22 00:22:37]       at listOnTimeout (node:internal/timers:573:17)
[randolympics-backend] [2024-12-22 00:22:37]       at process.processTimers (node:internal/timers:514:7) {
[randolympics-backend] [2024-12-22 00:22:37]     errorno: 'ETIMEDOUT',
[randolympics-backend] [2024-12-22 00:22:37]     code: 'ETIMEDOUT',
[randolympics-backend] [2024-12-22 00:22:37]     syscall: 'connect',
[randolympics-backend] [2024-12-22 00:22:37]     fatal: true
[randolympics-backend] [2024-12-22 00:22:37]   }
[randolympics-backend] [2024-12-22 00:22:37] }
[randolympics-backend] [2024-12-22 00:22:37]
[randolympics-backend] [2024-12-22 00:22:37] Node.js v20.15.1

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
December 22, 2024

Hi there,

As far as I can see you are getting a timeout error. This is most likely due to the database trusted sources.

You will need to make sure that you only connect to the database during the deploy stage and not during the build stage.

During the build stage, the App platform will not have access to the database cluster even if it is added to the trusted sources.

It is best to run any database migrations and other database interactions during the deploy/run stage instead of the build stage.

That way if for any reason the build stage succeeds and the deploy stage fails, you will not end up with a mismatch between your database and the codebase.

- Bobby

I am having the same problem, did you resolve it? Please reply

Same problem here.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.