4

Firstly i was a windows programmer and recently switched to linux (ubuntu 18.04 distro) . On Windows my code was working fine but on linux it started showing the error.Error: connect ECONNREFUSED 127.0.0.1:3306

my connection code :

import { createConnection } from 'mysql';

export default createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'rent_compair',
});

server.js:

import express from 'express';
import cors from 'cors';
import admin from './routes/admin';
import db from './config/mysql';

const app = express();

db.connect((err) => {
if (err) throw err;

console.log('Db Connected!');
});

app.use(cors());
app.use(express.json());

app.use('/', admin);
app.listen(process.env.PORT || 5000, console.log('Server Connected!'));

xampp started with sudo privileges: xampp server snap

1 Answer 1

2

The reason for this is that you cannot connect to MySQL with the root account anymore. This rule was changed — and enforced — within MySQL starting a few years ago. While it is possible to update the configuration to allow it, it is strongly discouraged even for development environments.

Instead, if you need the application to have complete control over a database, you can create an account and give it full control like this:

CREATE USER 'app_name'@'localhost' IDENTIFIED WITH mysql_native_password BY 'superSecretPassword!123';
GRANT ALL ON `database`.* TO 'app_name'@'localhost';

In the event you want the application to have even more control, you can allow it access to everything, including the ability to create new accounts, like this:

GRANT ALL ON *.* TO 'app_name'@'localhost' WITH GRANT OPTION;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .