将MySQL数据库与节点JavaScript应用程序连接 [英] Connect MySQL database with node JavaScript application

查看:100
本文介绍了将MySQL数据库与节点JavaScript应用程序连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个节点JavaScript应用程序,并使用SSH将其部署在cPanel上. 应用程序在没有数据库的情况下运行良好,但是当我将应用程序与cPanel(GoDaddy)上的数据库连接时,它会花费一些时间并显示消息建立数据库连接时出错". 我的连接代码

I make a node JavaScript app and deploy it on cPanel using SSH. App is working fine without database but when I connect the app with database on cPanel (GoDaddy) it takes times and shows the message "Error establishing a database connection". My connection code

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

var pool = mysql.createConnection({
    host: 'localhost',
    user: '<MY_USER_NAME>',
    password: '<MY_PASSWORD>',
    database: '<DB_NAME>'
});

pool.connect(function(err) {
  if (err) throw err;
  else{
  console.log("Connected!");
  }
});



module.exports = pool;

数据库交互但失去连接的路由.

route where DB interact,but lost the connection.

app.post('/loginn', (req, res) => {
var id = req.body.id
  console.log("user_id= "+id);
  var sql = "select * from users where id  NOT IN ('" + id + "') ";
    pool.query(sql, function (err, rows) {
        if (err) throw err;
        else {
          res.render('allusers', {
            users: rows,
            user_id:id
          })
        }
    });
  });

推荐答案

此答案将采取调试过程的形式,因为这是我能深入研究问题的唯一途径.

This answer is going to take the form of a debugging journey, because that's the only way I can see to get to the bottom of your issue.

让我们为您的应用做一个简单的表示,以确保您可以将查询发送到MySQL并从Express中的路由处理程序接收响应.像这样设置您的应用:

Let's do a dead-simple representation of your app to make sure that you can send a query to MySQL and receive a response from a route-handler in Express. Setup your app like this:

const mysql = require('mysql');
const express = require('express');
const app = express();
const PORT = // define the PORT for your host

var connection = mysql.createConnection({
    host: 'localhost',
    user: '<MY_USER_NAME>',
    password: '<MY_PASSWORD>',
    database: '<DB_NAME>'
});

connection.connect(function(err) {
  if (err) console.error(err);
  console.log("Connected!");
});

app.get('/db-test', (req, res, next) => {

  var id = // fill in a user_id that you know exists
  var sql = `SELECT * FROM users WHERE id NOT IN ('${id}') `;

  console.log(sql); // confirm you are sending the sql request you believe you should be sending

  connection.query(sql, function (err, results, fields) {
      if (err) console.error(err);
      console.log(`results: ${results}\nfields: ${fields}`);
  });

});

app.listen(PORT);

,然后从您的应用程序中找到路由/db-test,看看会发生什么.如果这行得通,那么我们至少将证明您 CAN 在Express和MySQL之间发出请求.现在,我不确定是否可以,所以不确定要调试什么.

And then hit the route /db-test from your app, and see what happens. If this works, then we will have at least proved that you CAN make requests between Express and MySQL. Right now, I'm not sure you can, so I'm not sure what to debug.

这篇关于将MySQL数据库与节点JavaScript应用程序连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆