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?
Related
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.
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.
});
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
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();
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'.