Ubuntu 20.04.2 Not Working With MySQL 8.0.27 . .

nc flag

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"

    if (errconn) 
        console.error('Error connecting: ' + errconn.stack);
        return "Invalid";
        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
    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) {
[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.

in flag
You cannot connect to a MySQL installation on most modern Linux distributions as `root` without using the super user. Simplest — and industry standard — solution is to create a MySQL user for the website and use those credentials
Trunk avatar
nc flag
Already tried a non-root user account I had created within MySQL. But I didn't try this with socket connection. I take it that it must be socket connection for local machine access? Or can it be configged to allow TCP/IP access?
in flag
If it’s a local connection then you’ll be good with either a socket or ``. Who configured the server? `sudo mysql` generally does not require a password because if you can `sudo` on a Linux machine, then you must be the administrator for the server. If the server has been misconfigured, then there’s about a half-million things to check and verify in order to work out why connections are failing.
Trunk avatar
nc flag
It's on localhost already which is normally the same. Any configs are either default ones or additional users, privileges, etc I added. I wish MySQL docs gave more on this. Lo tentaré mañana.
Trunk avatar
nc flag
I removed and reinstalled MySQL. Then I created a new user for mysql with privileges restricted to the Node app's database. The a connection to the MySQL server was made using MySQL Workbench and a TCP/IP connection was selected. A simple Users table was made and populated just to test access from the Node app to the MySQL nodeapp.users table. No access whatever. No connection to MySQL error shown. The crash error is just as before - trying to write an UNDEFINED value to the XHR response. I wonder how Oracle wants Node apps to access MySQL . . .
nc flag

Answer is that mysql is a package that no longer reliably handles connections to MySQL due to tougher authentication protocols after MySQL 8.

Though mysql has made claims in the past 2-3 years that these issues were fixed, they persist.

So the advice from other users is to use the npm package mysql2 instead.

I found that this is sound advice and I'm getting connected.

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.