I am trying to do a simple query to my azure SQL server from NodeJs, im getting errors that i cannot connect to the server. all the details are correct because i used them on another application i built in C#.
my code:
var mysql = require('mysql');
var config =
{
host: Server,
user: UserName,
password: PassWord,
database: Database
};
const conn = new mysql.createConnection(config);
conn.connect(
function (err) {
if (err) {
console.log("!!! Cannot connect !!! Error:");
throw err;
}
else
{
console.log("Connection established.");
queryDatabase();
}
});
function queryDatabase(){
conn.query('SELECT * FROM Drinks', function (err, results, fields)
{
if (err) throw err;
console.log(results);
})
conn.end(function (err) {
if (err) throw err;
else console.log('Done.')
});
};
I have tried making port rules, allowing nodejs through firewall, turning off firewall, I have added my IP to the firewall on the azure group and still cant connect. Any help would be awesome. Thanks!
Error:
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:211:20)
--------------------
at Protocol._enqueue (C:\node\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\node\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\node\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (C:\node\test.js:14:6)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47 {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}
Ok so i worked out how to do it. and for anyone with similar issues do this.
Instead of mysql i used mssql and it worked straight away.
Example:
var sql = require('mssql');
var config = {
server: "XXX.database.windows.net",
database: "XXX",
user: "XXX",
password: "XXX",
port: 1433,
options: {
encrypt: true
}
};
sql.connect(config).then(function() {
console.log("connected")
sql.query('SELECT * FROM Drinks', function (err, results, fields)
{
if (err) throw err;
console.log(results);
})
})
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.