从nodejs连接到mysql phpmyadmin时,协议\u连接\u丢失错误

uttx8gqw  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(272)

phpmyadmin正在以下位置的vps上运行http://ip_address:port_number. 当尝试从nodejs应用程序连接时,我收到错误:“连接丢失:服务器关闭了连接”。
下面是代码片段:-

const express = require('express');
// const bodyParser = require('body-parser');
const mysql = require('mysql');

const app = express();
const port = process.env.PORT || 5000;

// MySQL
const connection = mysql.createPool({
  connectionLimit : 1000,
  connectTimeout  : 60 * 60 * 1000,
  acquireTimeout  : 60 * 60 * 1000,
  timeout         : 60 * 60 * 1000,
  host            : 'ip_address', 
  user            : 'root',
  password        : 'password', 
  database        : 'customers',
  port            : port_number,
  multipleStatements: true
});

// Listen on environment port of 5000
app.listen(port, () => {
  console.log(`Listening on ${port}`)
});

// Get all data
app.get('/', (req, res) => {

  let sql = `SELECT * FROM customers_data LIMIT 50`;
  connection.query(sql, (err, results) => {
    if(err) {
      console.log('err', err);
      return;
    }
    console.log('connection established: ', results);
  });

});

系统:Windows10,mysql版本:2.18.1(最新),nodejs版本:14.17.1

nfg76nw0

nfg76nw01#

尝试这种连接方式,希望它能起作用

const mysql = require('mysql');
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
  database: 'database name'
});
connection.connect((err) => {
  if (err) throw err;
  console.log('Connected!');
});

相关问题