连接/查询mysql node.js和postreSQL时出错

时间:2015-10-02 00:26:35

标签: mysql node.js

mysql / node.js似乎连接然后我"想想"查询事件逻辑可能存在问题。这是代码和错误。我也有一个与postreSQL分开/类似的问题。我在同一目录中安装了npmed。

ryan@\Ryan:~/Desktop/node/datastores$ node MySQL
connection::connected

/home/ryan/Desktop/node/datastores/MySQL.js:20
    if (err) throw err;
                   ^
Error: connect ECONNREFUSED
    at errnoException (net.js:901:11)
    at Object.afterConnect [as oncomplete] (net.js:892:19)
    --------------------
    at Protocol._enqueue (/home/ryan/Desktop/node/node_modules/mysql/lib/protocol/Protocol.js:135:48)
    at Protocol.handshake (/home/ryan/Desktop/node/node_modules/mysql/lib/protocol/Protocol.js:52:41)
    at Connection.connect (/home/ryan/Desktop/node/node_modules/mysql/lib/Connection.js:123:18)
    at Object.<anonymous> (/home/ryan/Desktop/node/datastores/MySQL.js:15:12)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)

这是我的服务器/ mysql逻辑

var mysql = require('mysql');

var connectionConfig = {
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'sakila'
};

var connection = mysql.createConnection( connectionConfig);

connection.connect(function(err) {
    console.log('connection::connected');
});

connection.query('SELECT * FROM actor', function(err, rows, fields) {
    if (err) throw err;

    rows.forEach(function(row) {
        console.log(row.first_name, row.last_name);
    });
});

var actor = { first_name: 'Wil', last_name: 'Wheaton' };
connection.query('INSERT INTO actor SET ?', actor, function(err, results) {
    if (err) throw err;

    console.log(results);
});

connection.end(function(err) {
    console.log('connection::end');
});

heres postre logic

var pg = require('pg');

var connectionString = 'postgres://ryan@localhost/postgres';

var client = new pg.Client(connectionString);

client.connect(function(err) {
    if (err) throw err;

    client.query('SELECT NOW() AS "THE TIME"', function( err, result ) {
        if (err) throw err;

        console.log(result.rows[0]);

        client.end();
    });
});

继承人错误

ryan@\Ryan:~/Desktop/node/datastores$ node postreSQL

/home/ryan/Desktop/node/datastores/postreSQL.js:8
    if (err) throw err;
                   ^
Error: connect ECONNREFUSED
    at errnoException (net.js:901:11)
    at Object.afterConnect [as oncomplete] (net.js:892:19)

1 个答案:

答案 0 :(得分:1)

两个连接中的密码似乎都是空的。

您需要从devOps / SysAdmin /创建数据库的人那里获取数据库的连接详细信息。 询问主机名,端口,用户和密码以及架构名称;然后将它们放在你的init对象中:

// MySQL:
var connectionConfig = {
    host: "",     // Hostname + ":" + port (no need for :port if 3360)
    user: "",     // User
    password: "", // Password
    database: ""  // Schema 
};

// PostgreSQL:
var connectionString = 'postgres://user:password@hostname:port';