繁体   English   中英

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

[英]error connecting/querying mysql node.js and postreSQL

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');
});

继承人的逻辑

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)

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

您需要从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';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM