Node.js Mysql: connection failed - mysql

I'm trying to connect node.js to mysql and it's failed, I already install mysql. Can anyone help me ?
const express = require("express");
const mysql = require('mysql');
var app = express();
var mysqlConnection = mysql.createConnection({
host: "localhost",
user: "root",
password: "******",
database: "otablo",
});
mysqlConnection.connect((err)=> {
if(!err)
{
console.log("Connected");
}
else
{
console.log("Connection Failed");
}
})
app.listen(3000);

const mysql = require('mysql');
var app = express();
var mysqlConnection = mysql.createConnection({
host: "localhost",
user: "root",
password: "****",
database: "****",
});
mysqlConnection.connect((err)=> {
if(!err)
{
console.log("Connected");
}
else
{
console.log("Connection Failed");
}
})
app.listen(3000);
Your code is working fine, make sure you have everything installed and the values for the database are right. It worked for me.

I found the problem, I get the error message: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support auth
entication protocol requested by server; consider upgrading MyS
QL client
The solution:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password'

I had the same issue. The problem was in password. I tried to use same password that I saw in the tutorial, instead of using my own password that I took for my database during mysql installation process.

Related

MySQL error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

Could someone tell what error "ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client" means?
My code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "user",
password: "password",
database: 'database'
});
con.connect(function(err) {
if (err) throw 'THE ERROR IS: ' + err;
console.log("Connected!");
});
In the link that I sent for you, was added this property: insecureAuth : true.
var con = mysql.createConnection({
host: "localhost",
user: "user",
password: "password",
database: 'database',
insecureAuth : true
});
I also faced the same issue but the error got resolved by using mysql2 instead of mysql.
so type
npm install mysql2
in your terminal.
and update your code with
const mysql = require("mysql2");

Nodejs mysql2 not throwing connection or query error

I am trying to capture connection error on nodejs while using mysql or mysql2 node module.
My code is
console.log("Connecting to database")
try{
var connection = mysql.createConnection({
host : process.env.DB_HOST,
user : process.env.DB_USERNAME,
password : process.env.DB_PASSWORD,
database : process.env.DB_NAME
});
connection.connect();
console.log("Connected")
}
catch(exce)
{
console.log(exce)
}
The mysql server is not running and all details are wrong here. I am expecting this code to throw a connection error but this does not happen. Instead, I am receiving the following console output.
Connecting to database
Connected
I don`t know why there is no connection error thrown by mysql module.
The following solution suggested by #Anatoly worked for me.
var mysql=require("mysql2/promise")
var connected=true
console.log("Connecting to database")
try{
var connection =await mysql.createConnection({
host : process.env.DB_HOST,
user : process.env.DB_USERNAME,
password : process.env.DB_PASSWORD,
database : process.env.DB_NAME
});
// connection.connect();
console.log("Connected")
}
catch(exce)
{
console.log(exce)
connected=false
}
```

not able to connect to the sql server using sql + node.js + virtual studio code

I am trying to connect MySQL server using SQl and Node.js with Virtual studio code but couldn't. My code is as followed:
const mysql = require("mysql");
const express = require("express");
const bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.json());
var mysqlConnection = mysql.createConnection({
host : "localhost",
user: "root",
password: "password",
database: "test",
multipleStatements : true
});
mysqlConnection.connect((err)=> {
if(!err)
{
console.log("Connected");
}
else{
console.log("Connection Failed");
}
})
app.listen(3000);
Any help will be much appreciated.
Solved it! actually it is a problem of MySQL 8.0, which uses caching_sha2_password as default authentication plugin.
Run the following code in MySQL line cmd
ALTER USER 'root'#'localhost' IDENTIFIED BY 'your new password';
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'your new password';
And it worked!

Error when connecting to mysql in Mamp

I get this error when I try to connect to mysql database on phpmyadmin from nodejs . I am using same port number 3306 that is for mysql.After 4,5 seconds i get this error. I am using mamp free version for this .
connect ETIMEDOUT at Connection._handleConnectTimeout
var express = require('express');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
//constants
const STATUS_SUCCESS = "success";
const STATUS_FAILURE = "failure";
mysql
var connection = mysql.createConnection({
host : '172.0.0.1',
user : '****',
password : '****',
database : 'parking_application'
});
connection.connect();
app.listen(4567,'localhost',function(){
console.log("server started");
});
Please Help.
Thanks in advance.
var mysql = require('mysql');
var connection = mysql.createConnection({
socketPath : '/Applications/MAMP/tmp/mysql/mysql.sock', //path to mysql sock in MAMP
user : 'userdb',
password : 'password',
host : '127.0.0.1',
database : 'yourdatabase'
});
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} else {
console.log("Error connecting database ... nn");
}
Add the socket MAMP path to mysql (mysql.sock) in the json object connection.
Restart your server.
172.0.0.1. it's actually 127.0.0.1 or just use localhost

nodejs on mac error when connecting to mysql

This code works well on windows using nodejs and wamp but dont work on mac using mamp any idea why ? (the same username,password and db are used on both mac and pc !)
Im receiving this error Error: connect ECONNREFUSED
var mysql = require('mysql');
var TEST_DATABASE = 'krekib';
var TEST_TABLE = 'users';
var client = mysql.createClient({
user: 'root',
password: '12345',
});
client.query('USE '+TEST_DATABASE);
client.query(
'SELECT * FROM '+TEST_TABLE,
function selectCb(err, results, fields) {
if (err) {
throw err;
}
console.log(results);
console.log(fields);
client.end();
}
);
You probably need to specify your host and port. Check which port mysql is running in MAMP and add this.
var mysql = require('mysql');
var TEST_DATABASE = 'krekib';
var TEST_TABLE = 'users';
var client = mysql.createClient({
user: 'root',
password: '12345',
host: "127.0.0.1 or localhost, depends on your mysql user permissions",
port: "your port number in mamp",
});
Why would you connect to a database with JavaScript? It's really easy to find the JavaScript file and get your username + password.
Is the database running in Mamp?