I am new to node.js I had originally planned to use PHP but wanted to gain experience with node. I am trying to connect to MySQL database using Node.js when trying to connect I get the following error.
Thanks for the help!!
Here is the JS code
var mysql = require('mysql')
var con = mysql.createConnection({
host: 'localhost',
user: 'lcoleman0422',
password:'Micheal4',
database:'Top Level',
port:3306
});
con.connect();
/* function(err){
if (err) throw err
con.query('select * from Customer',function(error,result){
if(error)throw error;
console.log(result[0].First_Name);
}
)
});*/
con.end();
This is the code from my MYSQL_Connection.js
Connected
events.js:182
throw er; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Protocol.end (/home/lcoleman0422/node_modules/mysql/lib/protocol/Protocol.js:113:13)
at Socket.<anonymous> (/home/lcoleman0422/node_modules/mysql/lib/Connection.js:109:28)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
lcoleman0422@p3plcpnl0867 [~/public_html/js]$
Try this code let know if you have any doubts this.
const mysql = require('mysql');
const express = require('express');
const app = express();
const connection = mysql.createConnection({
host: 'localhost',
user: 'lcoleman0422',
password: 'Micheal4',
database: 'Top Level'
});
connection.connect();
app.get('/', (req, res) => {
connection.query(SELECT * FROM Customer, (error, data) => {
if(error) => {
console.log(error)
}else{
res.json(data[0].First_Name)
}
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.