node.js-mysql connection failed on windows7 - mysql

I am just getting started with nodejs.
My question is that i get an Error:
connect ECONNREFUSED
npm install mysql;
server.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '127.0.0.1',
port: '3306'
});
connection.connect(function(err) {
if(err) {
console.log(err);
}
});
connection.end();
Finally
1.I don't know the "user" and "password";
2."In mysql.conf, comment skip-networking.", I can't find mysql.conf;
3.I have tried this and it's not working, my platform is windows 7.
var client = mysql.createClient({
user: uuuu,
password: pppp,
host: '127.0.0.1',
port: '3306',
_socket: '/var/run/mysqld/mysqld.sock',});

It's clear you have not install mysql server in your windows yet. So you should go to http://www.mysql.com and download the server, install and set up, you can set your username and password and grant privilege to it, so you will know the user and password, and mysql.conf will be inside your installation. And if you are in windows, generally you would not use a UNIX socket to build the connection. So "_socket: '/var/run/mysqld/mysqld.sock'" should not be here.

Related

MySQL connection node

I'm running my node app on a linux vps where I have installed apache2 and phpmyadmin. I have my mysql database on the server there which I can connect to user the mysql -uusername -ppassword command, but when running my node app with this code:
const mysql = require('mysql');
const connection = mysql.createConnection({
host : '127.0.0.1',
user : 'root',
password : '*******',
database : 'db_name',
});
connection.connect(function(e) {
if(e) {
console.log('Database didn\'t connect');
} else {
console.log('Database connected successfully');
}
});
It says "database didn't connect". the user, password and db fields are all correct for sure.
When console.log(e) I'm getting:
I'm getting:
error code: 'ER_NOT_SUPPORTED_AUTH_MODE'
errorno: 1251
sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client'.
anybody knows why ?
You can change the connection to use the unix socket or check the mysqld is binding on 0.0.0.0 or on the ip of the ethernet card of the server it's running on.
According to the mysqld version you're using, the option can be "skip-networking" or "bind-address".

Node JS remote mysql database connection error

I am relatively new to the Node JS, and I have been trying to connect to a remote mysql server, but I have been unable to do so. I have been looking for the solutions on here but most of them are for localhost. Here is my code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "###.ipagemysql.com",
user: "user",
password: "mypass",
database: "mydb",
debug: true
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
I am getting the error below:
Error: connect ECONNREFUSED 103.224.212.250:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1117:14)
UPDATE:
I tried using a different MySQL server and that works fine. Could it be because iPage is blocking the connection because it is coming from a foreign machine? Is this something that can be configured in phpmyadmin?
Possibly port 3306 is not accessible from the application. You can try telnet to server 103.224.212.250:3306 to see if it is opened or not.

mariadb for node.js ER_ACCESS_DENIED_ERROR Access denied for user

I am using the following code to establish connection to a MariaDB 10 server:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "local_ip",
user: "username",
password: "my_password",
port: 3307,
database: "name_of_db",
});
con.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
The SQL-Server is running on a machine within a local network.
The SQL-User is granted connections from all hosts (%).
I am able to establish a connection with a SQL GUI tool, with exact the same user.
Seems to me like a bug inside the mysql node package, anybody encountered such a problem?

Can't connect to localhost database from node.js server

Been having a lot of trouble trying to connect to to my localhost database. I've tried using the mysql and mysql-simple node modules but in both cases I just can't get it to connect.
Here's what I used with the 'mysql' module:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
port : '8000',
user : 'uber',
password : 'pass',
});
connection.connect(function(err) {
if (err) throw err;
console.log('Connection Successful');
});
connection.query('USE someDB', function(err) {
if (err) throw err;
console.log('Query Successful');
});
And here' what I used with the 'mysql-simple' module:
var database = require('mysql-simple');
database.init('uber', 'pass', 'mysql', 'localhost', 8000);
database.querySingle('SELECT Host FROM user', function(err, results) {
if (err) {
console.log('error fetching some active users: ' + err);
return;
}
log('Query Successful');
for (var i = 0; i < results.length; i++)
console.log('got active user ' + results[i]);
}
In both cases, when I run my node.js server, it never logs that its connected. I've tried replacing localhost with '127.0.01' and creating a new user to make sure the password is correct, but to no avail. Why isn't it connecting?
Thanks
It's most likely that networking is turned off, that means that mysql server communicates with clients via UNIX sockets and not via TCP/IP. You can check that out running mysql client and run "status" command. If you see port number there, then your mysql server communicates via TCP/IP, or else you'll see something like "socket pathname…", get the pathname and give it to node.js connection parameters, e.g.
...
socketPath: '/opt/lampp/var/...',
...
Check that out in https://github.com/felixge/node-mysql page (search for "socketPathname")
Hope, that's your problem.
You should use mysql_config to show the path to socket.
This a sample on my MAC
QuyLes-MacBook-Pro:freelancer quyle$ mysql_config
Usage: /Applications/MAMP/Library/bin/mysql_config [OPTIONS]
Options:
--cflags [-I/Applications/MAMP/Library/include -fno-omit-frame-pointer -g -DNDEBUG]
--include [-I/Applications/MAMP/Library/include]
--libs [-L/Applications/MAMP/Library/lib -lmysqlclient -lz]
--libs_r [-L/Applications/MAMP/Library/lib -lmysqlclient_r -lz]
--plugindir [/Applications/MAMP/Library/lib/plugin]
--socket [/Applications/MAMP/tmp/mysql/mysql.sock]
--port [0]
--version [5.5.42]
--libmysqld-libs [-L/Applications/MAMP/Library/lib -lmysqld]
--variable=VAR VAR is one of:
pkgincludedir [/Applications/MAMP/Library/include]
pkglibdir [/Applications/MAMP/Library/lib]
plugindir [/Applications/MAMP/Library/lib/plugin]
and then, you add key socketPath for yourMysqlConnection.
bellow on my sample
MysqlServer: {
adapter: 'sails-mysql',
host: 'localhost',
user: 'root', //optional
password: 'root', //optional
database: 'nodejs', //optional,
socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock'
},
change this
database.init('uber', 'pass', 'mysql', 'localhost', 8000);
to
database.init('uber', 'pass', 'mysql', 'localhost', 3306);
and you should be through
Make sure that MySQL and express are running on the same port.
I had MySQL bundled from XAMPP, that ran on Port 3036.
On setting app.listen to 3036, my code worked. FINALLY!
Try this code it's work for me
var mysql = require('mysql');
var connection = mysql.createConnection(
{
host : 'localhost',
user : 'root',
password : '',
database : 'urdatabase',
}
);
connection.connect();
query = connection.query("SELECT * FROM UrTable;");
query
.on('error', function(err) {
console.log( err );
})
.on('result', function( data ) {
socket.emit('YourData',data);
});
I hope this will be helpful for you
In my case, anything happened, so I solved by generating new GRANTs with a new user for nodejs apps
Its due to the networking is turned off in MySQL configurations.
In Ubuntu 20.04.2 and MySQL 8.x.x(its worked in my case) you can find this settings in
/etc/systemd/system/mysql.service.d/override.conf
there will be a ExecStart key and its have multiple configurations.
Here you can provide --skip-networking as OFF
--skip-networking=OFF
And you have to restart your service
systemctl restart mysql.service
It will allow you to connect localhost

connect ECONNREFUSED - node js , sql

I have the next code in a js file:
var mysql = require('mysql');
var TEST_DATABASE = 'nodejs_mysql_test';
var TEST_TABLE = 'test';
var client = mysql.createClient({
user: 'root',
password: 'root',
});
client.query('CREATE DATABASE '+TEST_DATABASE, function(err) {
if (err && err.number != mysql.ERROR_DB_CREATE_EXISTS) {
throw err;
}
});
But I get this error:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: connect ECONNREFUSED
at errnoException (net.js:632:11)
at Object.afterConnect [as oncomplete] (net.js:623:18)
As I understand it, this is a connection problem - but how do I solve it?
( I'm working on windows 7)
Thanks!!
I know two ways to solve it:
In mysql.conf, comment skip-networking.
Try to set the socket like this:
var client = mysql.createClient({
user: uuuu,
password: pppp,
host: '127.0.0.1',
port: '3306',
_socket: '/var/run/mysqld/mysqld.sock',});
I got this error when MySQL Server was not running.
I changed my configuration via Initialize Database in MySQL.PrefPane, the System Preferences tool for MySQL on OS X - to Use Legacy Password Encryption - to fix ER_NOT_SUPPORTED_AUTH_MODE.
This config change stopped MySQL Server and then I got ECONNREFUSED when I tried to connect to MySQL from node.js.
Fixed by restarting MySQL Server from the MySQL System Preferences tool.
Try to fix your defined port in mysql and your Node.js script.
You can define the mysqld port in the *.cnf file inside the mysql directory,
and you can define that port when you connect to MySQL in your Node.js script.
Something like this in the cnf file
port = 3306