Node.js MySQL database connection - time out (ETIMEDOUT) - javascript

I try to make a simple connection to an online mysql database on a node.js server. This is my code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: 'example.org',
username: 'myusername',
password: 'mypassword'
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
Whenever I run this server I get the error:
"connect ETIMEDOUT".
I am a 100% sure my credentials are correct and I also changed the privileges of the user in phpmyadmin (I gave this user all possible privileges).
I run the server locally, I am not sure if this has anything to do with it.
My questions are:
- Why is it timing out?
- And how can I connect to this database?

Seemingly, this is related to your DataBase server.
Though, you can try to extend the timeout default, by passing a longer timeout value. (Default is 10000).
Just do:
var con = mysql.createConnection({
host: 'example.org',
username: 'myusername',
password: 'mypassword',
connectTimeout: 30000
});

Related

Error connecting to posgresql database using pg on windows

I've got a PostgreSQL database, definitely existing because I can connect to it and make SQL query using psql.
However, when I try to connect with node.js, using the pg client, I've got an error saying that the database does not exist.
Here is how the connection work:
var pg = require('pg');
const connection = new pg.Client({
host: 'localhost',
port: 5432,
database: 'camino',
user: 'postgres',
password: 'postgres'
})
connection.connect();
And here is the error I obtained:
In english: The database **camino** does not exist.
Can you enligthen me on this error?
Are you sure you have created the database camino by using the create table syntax found here: https://www.postgresql.org/docs/9.1/sql-createdatabase.html
If you have already done that, Postgresql starts with 3 default databases called: postgres, template0 and template1. Have you tried connecting to the default postgres database and confirmed that it works?

How to connect to remote mysql database using Node.js

I'm trying to connect to a mysql database using node. It is working perfectly fine when connecting with localhost.
var pool = mysql.createPool({
connectionLimit : 100,
host : 'http://<ip-address here>/phpmyadmin',
user : '*********',
password : '*********',
database : '*********'
});
pool.getConnection(function(err, connection) {
// Use the connection
connection.query( 'SELECT * FROM table', function(err, rows) {
// And done with the connection.
connection.release();
// Don't use the connection here, it has been returned to the pool.
});
});
The above code is not working.
I haven't found any answer on Google as well as here.
Host is only contains IP address, try to remove phpmyadmin.

Connect to MySQL database with Node.js

I'm experiencing an issue when trying to connect to a MySQL database with Node.JS in my React Native application.
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
port: 8889,
database: "blabla"
});
con.connect(function(err) {
if (err) console.log(err);
});
I can access to phpmyadmin without any problem (same user and password)
but when I execute the JS file the terminal seems to load but stay stuck like this:
I don't think that there is a link with React but I really have no idea about it.
Thanks in advance for your help
According to the mysql docs: https://www.npmjs.com/package/mysql you need to add con.end(); after you are finished with the connection.
I just understood that I didn't exit the script, but it's working (sorry).
The line to end the script for Node.js beginners like me:
process.exit()

Cannot connect to SQL Server with Node.js and Tedious

When I try to use Node.js and Tedioius to connect to a local SQL Server instance I get this error:
{ [ConnectionError: Failed to connect to XXXXX:1433 - connect ECONNREFUSED]
name: 'ConnectionError',
message: 'Failed to connect to XXXXX:1433 - connect ECONNREFUSED',
code: 'ESOCKET' }
Here is my connection object:
var config = {
userName: 'username',
password: 'password',
server: 'XXXXX',
options: {
database: 'databasename',
instancename: 'SQLEXPRESS'
}
};
I have checked and TCP/IP is enabled and broadcasting on port 1443 according to Configuration Manager. The SQL Server Browser service is also running, which I read may be causing this type of issue if not. I have disabled my antivirus and firewall and that hasn't helped either.
Any insight?
So what I am guessing happens is that even though Tedious lets you include instance name in 'options' it either doesn't use it or can't use it as it needs to be used. After doing some research, what should be happening is when you give SQL Server the instance name, it redirects you from port 1433 to the dynamic port it is using for that instance. I didn't know it was using a dynamic port, but if your instance is named the port will always be dynamic. I don't know where I saw it broadcasting on 1433, that was my mistake.
To check the dynamic port, look here:
From this information, I changed my code to this:
var config = {
userName: 'username',
password: 'password',
server: 'XXXXX',
options: {
port: 49175,
database: 'databasename',
instancename: 'SQLEXPRESS'
}
};
All is good now, hope this helps someone.
If anyone else is new to SQL Server like I am, and is dealing with this issue, once you enable TCP/IP in SQL Server Config Manager by following these steps:
> SQL Server Network Config
> Protocols for YOURSQLSERVERINSTANCE
> TCP/IP
> Enable
you get a warning message that looks like this:
Any changes made will be saved; however, they will not take effect until the service is stopped and restarted.
I took this to mean, disconnect from the database service in SQL Server Management Studio and reconnect, but this needs to happen in SQL Server Config Manager under the SQL Server Services tab. Find you SQL Server instance, stop and restart it, and hopefully you will be golden! This worked like a charm for me. Oddly, enabling the Named Pipes protocol seemed to work without a restart (I could see the difference in the error message), so I thought for sure it had stopped and restarted as needed.
Also, be sure to enable SQL Server Browser services as well. This and enabling TCP/IP and restarting the service were the keys for me.
If you still have problems after enabling TCP/IP protocol, I would suggest you check that SQL Server Browser Service is running. In my case I spent a lot of time till I realised it wasn't running.
This configuration run fine for me:
var config = {
user: 'user',
password: 'userPwd',
server: 'localhost',
database: 'myDatabase',
options: {
truestedConnection: true,
instanceName: 'SQLEXPRESS'
}
If you still got this error,
"...'Failed to connect to Server:1433 - connect ECONNREFUSED Server IP:1433',
code: 'ESOCKET' }"
and you've checked all the following:
Enable TCP/IP
Open Port 1433
Config setup correctly (database, server, username and password}
No Dynamic ports configured
Check your SQL server version. In my case, I discovered that I could connect to SQL 2012, but not SQL server 2016 with the same code. It appears SQL Server 2016 is not supported by the tedious driver yet.
...
You have to enabled tcp/ip on protocol for MSSQLSERVER
and activate both authentication
here is the complete code
const {
Request
} = require('tedious');
var Connection = require('tedious').Connection;
var config = {
server: 'DESKTOP-RU9C12L', //update me
authentication: {
type: 'default',
options: {
userName: 'begaak', //update me
password: 'begaak#123', //update me
}
},
options: {
encrypt: true,
enableArithAbort: true,
integratedSecurity: true,
trustServerCertificate: true,
rowCollectionOnDone: true,
database: 'selvapoc' //update me
}
};
var connection = new Connection(config);
connection.connect(function(err) {
console.log('testing')
// var request = new Request("Select * from products", function(err, rowCount, rows) {
// console.log(rowCount);
// console.log(JSON.stringify(rows))
// });
// connection.execSql(request);
connection.execSql(new Request('SELECT * FROM Products', function(err, rowCount, rows) {
if (err) {
throw err;
}
})
.on('doneInProc', function(rowCount, more, rows) {
console.log(more, rows[0], rowCount); // not empty
}));
});
connection.on('connect', function(err) {
// If no error, then good to proceed.
if (err) console.log(err)
console.log("Connected");
});
before starting the code configure these with SQL SERVER CONFIGURATION MANAGER

breeze-sequelize with MSSQL possible?

Is it currently possible to connect breeze-sequelize with a MS SQL server?
According to the doc of Sequelize, Sequelize does support MSSQL Server.
Though in the breeze doc there is no MS SQL server listed.
I am a bit confused now. And if it is not possible, is the breeze dev team planning to impl that? Or are there alternatives to use breeze in nodejs with an MSSQL server?
Yes it is actually possible. It took quite some time since the breeze-sequelize documentation is not very "newb friendly".
Here is my configuration for the MS SQL server. The tempHire example from the breeze samples on github was helping out a lot.
var dbConfig = {
user: 'username',
password: 'secret',
dbName: 'myDatabase'
};
var sequelizeOptions = {
host: 'hostname',
dialect: 'mssql',
port: 1433
};
function createSequelizeManager() {
var metadata = readMetadata();
var sm = new SequelizeManager(dbConfig, sequelizeOptions);
sm.importMetadata(metadata);
return sm;
}
The only thing i could not figure out until now is how to communicate with a specific MS SQL instance on a host e.g. localhost\MY_MSSQL_INSTANCE.
UPDATE
I actually did find out how to connect to a specific named instance.
So if you want to connect to an mssql instance like localhost\MY_MSSQL_INSTANCE, the sequelizeOptions should look like the following:
var sequelizeOptions = {
host: 'localhost',
dialect: 'mssql',
dialectOptions: {
instanceName: 'MY_MSSQL_INSTANCE'
}
};

Categories