Hello,
For a few days now I have been trying to set up a droplet with a mysql Database on a managed DB cluster (also on DigitalOcean), but have been running into trouble getting the app connected to the database: I keep getting the connection timed out error, which the docs refer to as a firewall (db-side) issue.
/home/nodejs/.pm2/logs/app-error.log last 15 lines:
0|app | at process.processTimers (node:internal/timers:507:7) {
0|app | errorno: 'ETIMEDOUT',
0|app | code: 'ETIMEDOUT',
0|app | syscall: 'connect',
0|app | fatal: true
0|app | }
0|app | Error: connect ETIMEDOUT
0|app | at PoolConnection._handleTimeoutError (/var/www/html/project-root/node_modules/mysql2/lib/connection.js:205:17)
0|app | at listOnTimeout (node:internal/timers:564:17)
0|app | at process.processTimers (node:internal/timers:507:7) {
0|app | errorno: 'ETIMEDOUT',
0|app | code: 'ETIMEDOUT',
0|app | syscall: 'connect',
0|app | fatal: true
0|app | }
The set-up is as follows:
hostname -i
inside the droplets ssh consoleThis is the setup for the dbPool connect:
const mysql = require('mysql2');
const fs = require('fs');
const path = require('path');
const caCertPath = path.join(__dirname, '../../config/ca-certificate.crt');
const caCert = fs.readFileSync(caCertPath);
const dbPool = mysql.createPool({
connectionLimit: 100,
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
multipleStatements: true,
ssl: {
ca: caCert
}
});
module.exports = dbPool;
The ca certificate is in /project-root/config/ca-certificate.crt while the dbConnect.js is inside /project-root/backend/utils/dbConnect.js
And inside the app.js:
**// Import module**
const dbPool = require('./backend/utils/dbConnect');
**// Connect db**
dbPool.getConnection((err, connection) => {
console.log('err connent', err);
if (err) throw (err); **// If not connected**
console.log('Connected to mySQL as ID ' + connection.threadId);
});
And the .env file:
DB_HOST='db-mysql-****-*****-do-user-********-0.c.db.ondigitalocean.com'
DB_USER='username'
DB_PASSWORD='password'
DB_DATABASE='dbname'
DB_PORT='25060'
I also tried various ports for the DB Port, like 25061 and 3306. When I use localhost or 127.0.0.1 or variations of that, I get a handshake error issue:
0|app | Listening on port 3000
0|app | err connent Error: self-signed certificate in certificate chain
0|app | at TLSSocket.onConnectSecure (node:_tls_wrap:1538:34)
0|app | at TLSSocket.emit (node:events:513:28)
0|app | at TLSSocket._finishInit (node:_tls_wrap:952:8)
0|app | at ssl.onhandshakedone (node:_tls_wrap:733:12) {
0|app | code: 'HANDSHAKE_SSL_ERROR',
0|app | fatal: true
0|app | }
I also tried setting up a managed app with node.js and a deployment process with the github repository with a DB added to the App during the app creation process in the digital ocean backend and tried the existing managed DB cluster database and run into 1:1 the same issues.
Could someone point me into the right directions to solve this issue?
Thank you very much, Julian
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!
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.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Heya @julianlaudan,
From the provided info, I didn’t see you setting your Droplet’s IP address as a trusted source for your managed DB app:
https://docs.digitalocean.com/products/app-platform/how-to/manage-databases/
Check the ‘Add Trusted Sources’ part where it shows how you can add an IP to be part of the trusted list so that you can connect to that DB.