I never had this problem when on Windows.
For some reason, I can't get any effect whatever when trying to connect to a local MySQL database from a local Node.js app.
Everything traceably works in the code up to the attempted connection to MySQL.
And everything traceably works well (assuming a stub assignment is inserted to substitute for the missing result from the MySQL call) after the MySQL connection.
I looked in vain at System logs and MySQL logs for any clue. Googling around it is sometimes suggested that MySQL may be denying a connection via the network but rather using a socket connection instead. I adjusted the connection parameters accordingly (no port: ...., added a socketPath: ....., entry) but have fared no better.
Below is the basic test code for
const mysql = require('mysql');
const queryNodeAppData = (query) =>
{
console.log("In MySQL Query script now ...")
console.log("Query: " + query);
const con = mysql.createConnection(
{
host: "localhost",
socketPath: '/var/run/mysqld/mysqld.sock',
database: "nodeapp",
user: "root",
password: "My.$qu3@1$y"
});
con.connect(function(errconn)
{
if (errconn)
{
console.error('Error connecting: ' + errconn.stack);
return "Invalid";
}
else
{
console.log('Connected as id ' + connection.threadId);
return "Valid;"
}
});
};
module.exports = { queryNodeAppData };
The corresponding terminal output after trying to run the app with nodemon is below.
Console logs are made as the user task is delegated by app.js to other modules, e.g. router, handler, format-checker, mysql, etc. Outputs are sometimes logged.
The eventual error that crashes the app is purely down to trying to write an undefined value to a text-defined XHR response variable. Stubbing the response with any string has the app return a 200 status code and no error.
[nodemon] starting `node app.js`
HTTP Server for NodeApp started at port 3000
We're in to the back end of NodeApp !
Someone has POSTed data to the Node server ...
URL trimmed endpoint: reg-user
Input Value: tamjk
In router.js now ...
Endpoint: reg-user Data: tamjk
Handler: authen.js
In handler authen.js now ...
Mode: reg Field: user Value: tamjk
In format validation now ...
Format Validation Message: Valid format
Connecting to user-data database checking user ...
In MySQL Query script now ...
Query: SELECT user FROM users WHERE user = 'tamjk'
Router returned message undefined
Callback Message: undefined
Message: undefined
Sent back response: undefined
_http_outgoing.js:696
throw new ERR_INVALID_ARG_TYPE('first argument',
^
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer or Uint8Array. Received undefined
at write_ (_http_outgoing.js:696:11)
at ServerResponse.write (_http_outgoing.js:661:15)
at reqCallback (/home/sandbar/Desktop/nodeapp-local/app.js:116:6)
at IncomingMessage.<anonymous> (/home/sandbar/Desktop/nodeapp-local/app.js:87:10)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'ERR_INVALID_ARG_TYPE'
}
[nodemon] app crashed - waiting for file changes before starting...
As well as the above problem, I have also started to get non-cooperation when I try to access the MySQL database from the terminal.
$ sudo mysql
[sudo] password for sandbar:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
All in all, I wonder if Ubuntu 20.04.2 and MySQL 8.0.27 are really made to work together.