I think I have exhausted all of my options for trying to get MySQL to connect to my express server. Scoured the Google machine, etc.. I am fairly new to MySQL, although I had to wipe out my computer recently and have since tried to get MySQL working to no avail.
I am able to connect to MySQL and create a db locally. However, when trying to connect to the server it looks like it will work, and then after about 10 seconds I am greeted with an ETIMEDOUT
error. I'm guessing that this is a network error of some sort, I just can't seem to get to the bottom of it.
For what it's worth I am using the latest MacOS, Node v17.6.0, Server version: 8.0.28 Homebrew
I verified the correct port:
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.01 sec)
mysql>
The error message:
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> quit
Bye
chriss-air:demo-db pesarcomputer$ npm start
> demo-db@1.0.0 start
> node server.js
Connected to the election database
Server running on port 3306
node:events:505
throw er; // Unhandled 'error' event
^
Error: connect ETIMEDOUT
at Connection._handleTimeoutError (/Users/pesarcomputer/Dropbox/Mac/Documents/my-sql/demo-db/node_modules/mysql2/lib/connection.js:189:17)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7)
Emitted 'error' event on Connection instance at:
at Connection._notifyError (/Users/pesarcomputer/Dropbox/Mac/Documents/my-sql/demo-db/node_modules/mysql2/lib/connection.js:236:12)
at Connection._handleFatalError (/Users/pesarcomputer/Dropbox/Mac/Documents/my-sql/demo-db/node_modules/mysql2/lib/connection.js:167:10)
at Connection._handleNetworkError (/Users/pesarcomputer/Dropbox/Mac/Documents/my-sql/demo-db/node_modules/mysql2/lib/connection.js:180:10)
at Connection._handleTimeoutError (/Users/pesarcomputer/Dropbox/Mac/Documents/my-sql/demo-db/node_modules/mysql2/lib/connection.js:193:10)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7) {
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true
}
Node.js v17.6.0
chriss-air:demo-db pesarcomputer$ lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 3104 pesarcomputer 21u IPv4 0xd408ceb9108e063 0t0 TCP localhost:mysql (LISTEN)
mysqld 3104 pesarcomputer 36u IPv4 0xd408ceb96cb4b43 0t0 TCP localhost:mysql->localhost:49631 (ESTABLISHED)
TablePlus 3143 pesarcomputer 20u IPv4 0xd408ceb9109c013 0t0 TCP localhost:49631->localhost:mysql (ESTABLISHED)
chriss-air:demo-db pesarcomputer$
Here is my code:
const mysql = require("mysql2");
const express = require("express");
const PORT = process.env.PORT || 3306;
const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
const db = mysql.createConnection(
{
host: "localhost",
port: "3306",
user: "root",
password: "password",
database: "election",
// debug: true,
},
console.log("Connected to the election database")
);
app.get("/", (req, res) => {
res.json({
message: "Hello World",
});
});
// app.use((req, res) => {
// res.status(404).end();
// });
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
3306 is your DB Port. Change the other port (const PORT = process.env.PORT || 3306;) to something else, typically 3000 or 3001.