【问题标题】:I can't connect to my mysql database with Node.js (connection.connect issue)我无法使用 Node.js 连接到我的 mysql 数据库(connection.connect 问题)
【发布时间】:2017-06-12 03:53:26
【问题描述】:

我无法使用 mysql 节点包从 Node.js 连接到我个人计算机上的本地 mysql 数据库。我的 Node.js 程序与 mysql 数据库位于同一台计算机上,因此不需要联网。我尝试了多种堆栈溢出解决方案,但似乎都没有。

我的目录结构:

project_root/ 
    mysql/         
        connection.js
    node_modules/
        ....
    server.js
    package.json

connection.js 的内容如下所示,错误出现在“connection.connect”那一行。我知道这是因为运行“function(err)”的回调函数在控制台中打印“Mysql 连接错误”,然后打印“null”。

var fs = require('fs');
var mysql = require('mysql');

var connection_data = JSON.parse(fs.readFileSync('../config/mysql.json', 'utf8'));

// JSON.stringify(connection_data)
// ‌{"host":"127.0.0.1","port":3306,"user":"root","passwd":"xxxxxxxxxxx","db":"mydatabase"}

var connection = mysql.createConnection({
    host : connection_data['host'],
    user : connection_data['user'],
    password : connection_data['passwd'],
    database: connection_data['db'],
    charset: "utf8_general_ci",
    socketPath : '/tmp/mysql.sock'
});

export_connection = connection.connect(function(err) {
    console.log("Mysql connection error");
    console.log(err);
});

module.exports = export_connection

其他链接建议在下面给出的命令行中查看 mysql 状态。我什至将 Node.js mysql 连接设置为连接到状态中显示的相同套接字。

mysql> status 
--------------
mysql  Ver 14.14 Distrib 5.7.18, for osx10.12 (x86_64) using  EditLine wrapper

Connection id:      237509
Current database:   
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.7.18 MySQL Community Server (GPL)
Protocol version:   10
Connection:     Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:        /tmp/mysql.sock
Uptime:         1 day 8 hours 9 min 18 sec

注意connection_data['host']是127.0.0.1,connection_data['user']是root,connection_data['db']对应一个已有的数据库。

【问题讨论】:

    标签: javascript mysql node.js


    【解决方案1】:

    您确定它没有连接吗?你写了这个:

    connection.js 的内容如下所示,错误出现在“connection.connect”那一行。我知道这是因为运行“function(err)”的回调函数在控制台中打印“Mysql 连接错误”,然后打印“null”。

    Node.js 回调函数有一个标准的形式:通常

    function (err, result) { ... }
    

    回调被调用无论调用成功还是失败。如果调用失败,则将err 设置为某个值(即not null)。在您的情况下,errnull,表示连接成功。

    回调函数通常看起来像这样:

    function (err, result) {
        if (err) {
            // ...do something with the error, like reporting it to the
            // user or throwing an exception
            return;
        }
    
        // otherwise, everything is cool. Query the database (or
        // do something else)
    }
    

    在您的情况下,看起来连接实际上成功了。下一步是尝试查询并查看它是否有效。如果失败,请使用错误详细信息更新您的问题。

    【讨论】:

    • 我的错误。该代码实际上有效。我不知道为什么它会显示该错误消息。
    • 您的代码显示错误消息的原因是因为这就是您的回调所做的(这就是您的回调所做的全部)。如果没有设置err(例如undefinednull),则表示没有错误。
    猜你喜欢
    • 1970-01-01
    • 2016-03-22
    • 2018-02-06
    • 2014-11-25
    • 2021-03-13
    • 1970-01-01
    • 2018-10-08
    • 2013-03-24
    相关资源
    最近更新 更多