简体   繁体   中英

error connecting/querying mysql node.js and postreSQL

the mysql/node.js appears to connect then i "think" there may be a problem with the query event logic. Here's the code and error. I also have a separate/similar problem with postreSQL. I install npmed both in the same directory.

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)

this is my server/mysql logic

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

heres error

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)

Your password seems to be empty in both connections.

You need to get the connection details for the database from your devOps / SysAdmin / whoever created it. Ask for the hostname, port, user and password, and schema name; then put those in your init object:

// 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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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