【问题标题】:How to connect node.js to MySQL如何将 node.js 连接到 MySQL
【发布时间】:2020-04-09 12:40:16
【问题描述】:

我正在尝试将 node.js 连接到 MySQL,但失败了。我已经安装了 MySQL 和相关库。如何解决此错误?另外,如果我想将数据获取到 react-native,我应该怎么做呢?

const express = require('express');
const mysql = require('mysql');

const connection = mysql.createPool({
  host     : '*****',//aws db endpoint/MySQL hostname
  user     : 'administrator', // username of MySQL connection
  password : '*****', //pw of MySQL db
  database : 'database_3' // name of database to be used
});

const app = express();
app.get('/User', function (req, res) {
    connection.getConnection(function (err, connection) {
    connection.query('SELECT * FROM User', function (error, results, fields) {
      if (error) throw error;
      res.send(results)
    });
  });
});

// Starting our server.
app.listen(3306, () => {
    console.log('Go to http://localhost:3306/User');
   });
   

收到的错误信息:

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3306
    at Server.setupListenHandle [as _listen2] (net.js:1279:14)
    at listenInCluster (net.js:1327:12)
    at Server.listen (net.js:1414:7)
    at Function.listen (C:\Users\Irvin\my-new-project\node_modules\express\lib\application.js:618:24)
    at Object.<anonymous> (C:\Users\Irvin\my-new-project\route.js:39:5)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
Emitted 'error' event at:
    at emitErrorNT (net.js:1306:8)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

【问题讨论】:

    标签: javascript mysql node.js reactjs react-native


    【解决方案1】:

    错误:监听 EADDRINUSE:地址已在使用 :::3306

    这表示端口3306已经在使用中,请尝试将其更改为另一个端口或停止在该端口上运行的进程。

    // Lets change 3306 to 3307
    app.listen(3307, () => {
        console.log('Go to http://localhost:3307/User');
    });
    

    如果你想停止这个端口上的进程here is how to do it on linux

    【讨论】:

      【解决方案2】:

      3306 是默认的 mysql 端口。如果您已经运行了 mysql,请不要将该端口用于服务器。

      不要停止该端口上的进程,否则 mysql 将停止运行,除非你在另一个端口上运行它,这不是你的情况。

      app.listen(3000, () => {
          console.log('Go to http://localhost:3306/User');
      });
      

      【讨论】:

        【解决方案3】:

        您好,因为您正在运行 mysql,它默认在 3306 端口上运行。由于您尝试在同一端口上启动您的 express 应用程序,因此您会收到此错误。您需要在任何其他端口上启动您的 express 应用程序。

        【讨论】:

          【解决方案4】:

          您的nodeJs 应用程序使用与MySQL 相同的端口。

          // Starting our server.
             app.listen(3306, () => {
              console.log('Go to http://localhost:3306/User');
             });
          
          

          app.listen(3306) 是您的nodeJs 应用程序侦听端口, 你当前的MySQL监听端口也是3306, 所以你的nodeJs 应用程序会抛出一个错误

          Error: listen EADDRINUSE: address already in use :::3306
          

          表示端口3306 已在使用中。 您应该更改您的nodeJs 应用程序侦听端口,例如80808090, 例如:

          const express = require('express');
          const mysql = require('mysql');
          
          const connection = mysql.createPool({
            host     : '*****',//aws db endpoint/MySQL hostname
            user     : 'administrator', // username of MySQL connection
            password : '*****', //pw of MySQL db
            database : 'database_3' // name of database to be used
          });
          
          const app = express();
          app.get('/User', function (req, res) {
              connection.getConnection(function (err, connection) {
              connection.query('SELECT * FROM User', function (error, results, fields) {
                if (error) throw error;
                res.send(results)
              });
            });
          });
          
          // change this port to 8090
          app.listen(8090, () => {
              console.log('Go to http://localhost:3306/User');
             });
          
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-11-10
            • 2018-05-14
            • 1970-01-01
            • 2020-12-05
            • 2017-08-11
            • 2017-12-07
            • 2019-08-16
            相关资源
            最近更新 更多