node js mysql connection - mysql

i have a nodejs application and want to connect the application to mysql remote host. here is my query
mysql -u user -p password -h http://xxx.xxx.com.
but getting error ERROR 2005 (HY000): Unknown MySQL server host 'http://xxx.xxx.com' (0).
and here is nodejs code both are not working
var connection = mysql.createConnection({
host : 'http://xxx.xxx.com',
hostname : 'xxx.xxx.com',
user : 'user',
password : 'password'
});
connection.connect(function(err){
});
connection.query('SELECT * from test', function(err, rows, fields) {
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.');
});
connection.end();
thanks in advance

Related

why do i get an error connection in nodejs - mysql (trying remote connection)

I recently started learning Nodejs to connect to MySQL and i'm having connection issues. I tried using putty to connect and i'm able to do so without any issues
this is my example.route.js file
const express = require('express'),
router = express.Router(),
mysql = require('mysql');
var connection = mysql.createConnection({
host: <ipaddress>,
port: <default Port>,
user: 'root',
password: <given password>,
database: <db name>,
connectionTimeout: 30000
});
connection.connect((error) => {
if (error) {
console.log('error connecting: ' + error);
} else {
console.log('Connected to server');
}
});
connection.query('SELECT * FROM test', function(err, results, fields) {
console.log('results in table: ' + results);
console.log('fields in table: ' + fields); // -1
connection.release();
if (err) throw error;
});
connection.end();
module.exports = router;
i get the following error => Error: connect ECONNREFUSED (Will update with complete error in a few hours)
As i mentioned before i used PUTTY in order to make sure there was an issue when connecting but i was able to connect to the given database name, host with the same user and password.
Not sure if this helps is an ubuntu server with MySQL
Anyone has an idea of why i'm getting the connection error? I would appreciate it the help

Error while performing Query. node-mysql.js:15

I tried simple mysql connection in nodejs but got this error, checked with the sql connection....server is up and running....please help!
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '****',
database : 'todo'
});
connection.connect();
connection.query('SELECT * from < table name >', function(err, rows, fields) {
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.');
});
connection.end();
Output:
Debugger listening on ws://127.0.0.1:48226/9809e9bf-990f-4d1e-9d98-420af454907f
Error while performing Query.

node mysql connection.connect does not work

Having an odd issue with node mysql. I am running a mysql container (docker) on my local machine with a standard setup. I am able to get past mysql.createConnection, but my code always fails at connection.connect. MySQL Error logs on the container show no attempted/failed connections, and I can connect via Workbench. Has anyone had this experience trying to connect to MySQL via node.
router.get('/page', function(req,res){
const msg = 'My Query from config, tested and confirmed on Workbench';
var connection = mysql.createConnection({
host : '127.0.0.1',
user : 'user',
password : 'pw',
database : 'db',
port : 3306,
debug : true
});
console.log('Connect to server', connection.state); //Doesn't get past this guy
connection.connect(function(err) {
if (err) {
console.log('error connecting: ' + err.stack);
return err;
}
else {
console.log('connected as id ' + connection.threadId);
}
});
middleware.logger.debug('Make Query');
connection.query(msg, function(err, rows, fields) {
if (!err) {
console.log('The solution is: ', rows);
res.json('We got some rows!');
}
else {
console.log('Error while performing Query.', err);
res.json(err);
}
});
console.log('End Connection');
connection.end();
});
Also, should note: I get the same error whether my docker container is running or not.
EDIT: Okay, want to amend my problem, this is actually an issue with express router. Expanded code above.

Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433

I'm trying to connect to my local MySql server from node.js/express application using mssql with following config:
// Database connection
var sql = require('mssql');
var config = {
user: 'db_user',
password: 'db_pass',
server: 'localhost',
database: 'project_db'
};
sql.connect(config, function (err) {
if (err) {
console.log(err);
}
});
I get this error:
{ [ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433]
name: 'ConnectionError',
message: 'Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433',
code: 'ESOCKET' }
I know I have to enable TCP/IP connections for my local MySql server, but I can not find how to do it on OSX El Capitan. There is nothing like control panel with settings. That's all available at System Preferences > MySql:
Any ideas how to fix this, guys? Thanks for trying to help.
mssql and mysql are two very different things... You should use the node-mysql or some other mysql client library if you are going to be running mysql on your dev machine
https://github.com/felixge/node-mysql
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();

Change <username>#<computername> to <username>#localhost in node js application

I'm new to node js, I was trying a sample of connecting MySql with node. When I run the code I get
Error while performing Query.Error: ER_ACCESS_DENIED_ERROR:
Access denied for user <username>#<computername> (using password: YES)
The problem is when I connect to Mysql through workbench with localhost it succeeds but when I use < computername > it gives me the same error.
I added "mysql": "^2.5.4" dependency in package.json file.
So can anyone help me on how to change the default option from < computername > to localhost
index.js file
var app = express();
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'myusername',
password : 'mypassword',
database : 'mydb'
});
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.get('/', function(request, response) {
connection.connect();
connection.query('SELECT * from projects', function(err, rows, fields) {
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.' + err);
});
connection.end();
response.send('Hello World !');
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
Try using the IP for localhost instead of the string 'localhost' for the host. The default is 127.0.0.1
var connection = mysql.createConnection({
host : '127.0.0.1',
user : 'myusername',
password : 'mypassword',
database : 'mydb'
});
EDIT:
It seems that you actually are able to connect to MYSQL, but that your user is not allowed to use the chosen database. Ensure that the user you connect with can access the database, in your example 'mydb'.