Error when connecting to mysql in Mamp - mysql

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

Related

Node.js Mysql: connection failed

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.

Can't connect to mysql (express) with Node.js

Hello Stacksoverflowers,
I have a problem to connect my server.js file to Mysql via Express.
I've already insatlled mysql and express via npm.
when i run my server.js
The only thing it says in my console (MacOS default terminal) is:
"node server running now - using http://localhost:3000"
(I got no errors)
and my webpage also showing correct - "Node Server running - startpage" in the browser.
The problem is that they say nothing like "Connected to the MySQL server" or "Timeout: error messages" ?? (look at #3)
// Dan k
//#1
// My codes from server.js
const express = require('express');
const mysql = require('mysql');
const hostname = "localhost:";
const port = 3000;
//#2
// create connection to DB
const connection = mysql.createConnection({
host: "localhost",
user: 'root',
password: 'root',
port: 3000
});
//#3
// Connect to DB
connection.connect(function(err) {
if (!err) {
console.log('Connected to the MySQL server.');
}
else if (err)
{
return console.error('Timeout: ' + err.message);
}
});
// Routing with Express
const app = express();
//#4
// Startpage (root)
app.get('/', function(request, response){
response.send('Node Server running - startpage...');
response.end();
})
connection.end();
//#5
app.listen(port, (err) => {
if(err) throw err;
console.log("node server running now - using http://"+(hostname)+ .
(port)");
});
I got what is causing the issue. You are using the same port number 3000 to connect to your mysqlDB and at the same time using the same port to run your nodejs application.
This does throw an error but it takes a really long time to throw the error.
So the simple answer here is that the port that you are mentioning in your createConnection() method is incorrect and needs to be changed. This port should be port number on which your mysql DB is running. From the error it is clear that your mysql DB is not running on port 3306. You can check this post to figure out which port is your mysql DB running on. https://serverfault.com/questions/116100/how-to-check-what-port-mysql-is-running-on. Below is a reference for you. Please see the comments.
// create connection to DB
const connection = mysql.createConnection({
host: "localhost",
user: 'root',
password: 'root',
port: 3000 // You need to change this and put in the correct port number.
});

Error when connecting mysql database with node js

I am using Node JS and I am trying to connect to a mysql database. It keeps getting disconnected.When I run npm start,error shows as follows
D:\face-api.js\face-api.js\examples\FaceLogInBackend\app.js:26
app.use( bodyParser.json() ); // to support JSON-encoded bodies
^
TypeError: Cannot read property 'use' of undefined
at Object. (D:\face-api.js\face-api.js\examples\FaceLogInBackend\app.js:26:5)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
Code in app.js is as belows
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'bookstore'
});
connection.connect(function(err) {
if (err) throw err
console.log('You are now connected...')
})
You need this
var mysql = require('mysql');
// instead of storing the result in a variable, just make the connection,
mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'bookstore'
});
// now connect to mysql
mysql.connect(function(err) {
if (err) throw err
console.log('You are now connected...')
})
First off check you have something like this declared on top of file
var app = express();
you can use the below code to connect to the MySQL. It works fine.
const express = require('express'),
app = express(),
bodyParser = require('body-parser');
port = process.env.PORT || 3000;
const mysql = require('mysql');
// connection configurations
const connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'bookstore'
});
// connect to database
connection.connect(function(err) {
if (err) throw err
console.log('You are now connected...')
})
app.listen(port);
console.log('API server started on: ' + port);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

Connect to MySQL Server from node.js, but server in remote loaction

Connect to MySQL Server from Node.js But MySQL Server in remote location, How can i connect?
I tried below example :
var mysql = require("mysql");
var connection = mysql.createConnection({
host:"http://somelocation",
user:"root",
password:"roor"
});
connection.connect(function(err){
if (err) {
console.log("Error Connection to DB" + err);
return;
}
console.log("Connection established...");
});
connection.end(function(err){
});
But I am getting Error :
Error Connection to DBError: getaddrinfo ENOTFOUND https://exmaple.com
Thanks in advance !
Did you try to estimate connection without set protocol type in hostname field:
var connection = mysql.createConnection({
host:"somelocation.com",
user:"root",
password:"roor"
});
In docs I see only this case for hostmane field: https://www.npmjs.com/package/mysql

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?