简体   繁体   English

MySQL连接错误node.js events.js 182

[英]MySQL connection error node.js events.js 182

I am new to node.js I had originally planned to use PHP but wanted to gain experience with node. 我是最初计划使用PHP的node.js的新手,但想获得有关node的经验。 I am trying to connect to MySQL database using Node.js when trying to connect I get the following error. 尝试连接时,我尝试使用Node.js连接到MySQL数据库,但出现以下错误。

Thanks for the help!! 谢谢您的帮助!!

Here is the JS code 这是JS代码

    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(); con.end();

This is the code from my MYSQL_Connection.js 这是我的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)
        } 
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM