Error: connect ECONNREFUSED 127.0.0.1:3306

5,376

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;
Share:
5,376
Kunal Kumar
Author by

Kunal Kumar

I am a full stack react developer with one year development experience with 🕸web technologies and 📱 mobile technologies. I am fluent with english and also speaks hindi. React is my first preference but i can work with python and php also and that makes me versatile. Thanks for reading my bio.😉

Updated on September 18, 2022

Comments

  • Kunal Kumar
    Kunal Kumar over 1 year

    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