20

Why can't I connect to the mysql server?

On the same server an Apache/PHP server is running and it connects without problems!?

var mysql_link = {
    host : 'localhost',
    port : 3308,
    database: 'nodetest',
    user : 'root',
    password : 'xxx'
};

var connection = mysql.createConnection(mysql_link);

connection.connect(function(err){
    console.log(err);
    if(err != null){
        response.write('Error connecting to mysql:' + err+'\n');
    }
});

connection.end();

error

{ [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  fatal: true }

update

root@dyntest-amd-6000-8gb /var/www/node/dyntest # ps ax | grep mysqld
 7928 pts/0    S+     0:00 grep mysqld
28942 ?        S      0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/run/mysqld/mysqld.pid
29800 ?        Sl    17:31 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/var/lib/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/lib/mysql/mysql-error.log --open-files-limit=65535 --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
2
  • Can you post the output of ps ax | grep mysqld I'd like to see what tags mysql is running with Commented Jan 18, 2014 at 16:57
  • 2
    --port=3306 vs. port : 3308,. Change the port in your script to 3306 and it should work. Commented Jan 18, 2014 at 18:37

9 Answers 9

45

I know this question has been answered, but for me the problem was that the mysql server listens on a Unix socket not on a tcp socket. So the solution was to add:

port: '/var/run/mysqld/mysqld.sock'

to the connection options.

Sign up to request clarification or add additional context in comments.

6 Comments

Tested this solution on Ubuntu and it worked for me as well over the other solutions.
for osx MAMP, the port is /Applications/MAMP/tmp/mysql/mysql.sock
just chiming in for future readers: This solves the issue connecting to mysql on Angstrom Distribution as well via node (at least for me on a BeagleBoneBlack it did) and the path was actually /tmp/mysql.sock
For centos port: '/var/lib/mysql/mysql.sock' works fine, thanks
Thanks, @rlemon, it's the same for Mac.
|
13

If this has worked before, my first guess would be that you've already got a copy of your node.js script running in the background which is holding the connection.

I believe connection refused is a tcp/ip error message, rather than something from MySQL which suggests that it is either not running or is running on another port or with sockets.

Could you try telnet'ing to port 3308? To see if the server is running on that port?

telnet localhost 3308

Can you also try:

mysql -hlocalhost -uroot -pxxx

5 Comments

running this from the command line works fine mysql -hlocalhost -uroot -pxxx.. How can I kill all node.js scripts?!
sorry mysql -hlocalhost -P 3308 -uroot -pxxx forgot the port.. but it works
Have now tried to create a 'root'@'%' with all privileges granted.. same error.. hmm..
hmm.. I don't know why, but when I change port to the default port 3306 it works!? fact is that in the PHP application it connects to port 3308 and port 3308 is also assigned in my.cnf...
I'd like to guess PHP is actually using sockets and 3308 in the PHP config is incorrect.
12

Overview

For anyone else having this problem and is running . I suspected the problem had to do with the network and not MySQL or Node.js.

Solution

If you open MAMP and click MySQL in the left navigation panel it will pull up the MySQL options page. In the center of the page you will see a checkbox that says,

"Allow network access to MySQL".

Check this box and then restart your MAMP. At this point you can now test your connection to MySQL with telnet or a node.js script.

Hint

Remember you can check which port your MySQL is running on by opening MAMP and clicking the ports link on the left navigation panel.

Visual Aid

enter image description here

Comments

9

Mac OS on M1 MacBook Pro here with MySQL installation via brew:

Changing the host from 'localhost' to '127.0.0.1' and creating a different user with a password solved this issue for me. For some reason I cannot connect to my DB with root user and no password. I created the new user on MySQL Workbench, but you can create a new user with admin privileges via the mysql CLI also. Just google it.

This is how my backend looks:

const express = require('express');
const mysql = require('mysql');
const app = express();
const port = 3500;

const db = mysql.createConnection({
    host: '127.0.0.1',
    user: 'admin',
    password: 'admin',
});
db.connect((err) => {
    if (err) throw err;
    console.log('connected to database');
});

app.listen(port, () => {
    console.log('server listening on port 3500');
});

3 Comments

Thanks! I only needed to replace localhost with 127.0.0.1 or 0.0.0.0 to make it work.
127.0.0.1 did the trick for me. It's weird because it worked with localhost for a while. Mac OS 13.6 Intel
This also solved my problem running Node on ChromeOS Debian container (penguin).
3

I wanted to comment my solution here, just in case there were people as newbie as me in databases.

I was getting this error because I had installed the mysql NPM package correctly but I hadn't installed any implementation of MySQL on my computer (I didn't know I had to).

I'm using Arch Linux so, in my case, with the NPM package already installed in my project, I did pacman -Syu mariadb (MariaDB is the default implementation of MySQL in Arch Linux) and then configured it following the guide.

Then, you can use the root user you just configured or create a new one to use in your project. For the latter:

  • Enter mysql CLI by running mysql -u root -p.

  • Enter the password for root user.

  • Create a new database with CREATE DATABASE mydatabase;.

  • Create a new user with CREATE USER test IDENTIFIED BY "testpass";.

  • Grant privileges to test user to use your new database with GRANT ALL PRIVILEGES ON mydatabase.* TO test@localhost IDENTIFIED BY "testpass";. See for more information on this.

And then, in my project, I would have:

let connection = mysql.createConnection({
    host: "localhost",
    user: "test",
    password: "testpass",
    database: "mydatabase"
});

Comments

2

For some very odd reason, my computer only allowed me to have port 3306 as default port for my connection in order for it to work.

Screenshot

2 Comments

Not sure, but this could be because your Apache Web Server was not running.
Please do not store credentials in code. Every time someone does that a baby penguin suffers...
1

If you are using MAMP please note that mysql default db_port is set to 8889 so you for this purpose I had to change it to 3306 (or whatever your app mysql db_port is set to).

My issue was that node server was connecting using port 3306, so it was giving the error below then crashing but mysql was up and seemed to establishing a connection through localhost, although I couldnt test it because node server was down.

errno: -61, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 3306, fatal: true

Once I changed the port on MAMP mysql from 8889 to 3306, node server established connection through port 3000 and giving the statement below:

server running on port 3000 The solution is: 2

Comments

0

I use Windows 10 and I have Windows Subsystem For Linux (WSFL). I can execute my project from the WSFL console, but my MySQL is installed in Windows and they can not connect, however when I execute my project from a Windows console then it works without any problems.

Comments

0

I got ECONNREFUSED ::1:3306 on Ubuntu Server when I had

host="localhost"

For me the fix was

host="127.0.0.1"

Because my configs had bind-address 127.0.0.1 and no IPv6 address, when localhost got translated to IPv6 ::1 instead of IPv4 127.0.0.1 it wouldn't connect.

I found the bind address like this:

$ cd /etc/mysql/
$ grep -R bind-address

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.