I would like to connect to MySQL database through Node.js in my Angular Universal project.
Connection code is simple:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'dbname',
port: '8889'
});
connection.connect(function(err) {
if (err) {
console.log("Error connecting to DB: " + err);
throw err;
}
});
First I am getting the following error:
TypeError: Cannot read property 'on' of undefined
at Protocol._enqueue (/Applications/MAMP/htdocs/Angular2/myproject/node_modules/mysql/lib/protocol/Protocol.js:152:5)
...
Then after several seconds there is the following error:
Error connecting to DB: Error: Connection lost: The server closed the connection.
Advice me please what should be done in this case to fix the issue.
Thanks a lot.