-1

I got this error when I tried to connect to the mysql database -

{ errno: -111, code: 'ECONNREFUSED', syscall: 'connect',
address: '127.0.0.1', port: 3306, fatal: true }

I used the same host, user and password as when I am using popsql which works.

const databasePassword = process.env.DATABASE_PASSWORD
const mysql = require('mysql');
function saveIDInformation(titleID){
    let savingStatus = "passed"
    const con = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: databasePassword,
        database: "idmarketplace",
    });
    con.connect(function(err) {
      if (err) throw err;
      console.log("Connected!");
        const IDinformation = `INSERT INTO idInformation(titleID) VALUES(${titleID});`;

        con.query( IDinformation,(err, result)=> {
          if (err) {
            savingStatus = "query error";
            throw err;
          }
          console.log("Data inserted");
        });
      });
    return{
        status: savingStatus
    }
}
exports.saveIDInformation = saveIDInformation; 

I research this error in github, stackoverflow, chatGPT and other websites. Furthermore, I also watched Youtube to solve this problem, but I still can't find the solution.

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • Does this answer your question? [node.js mysql error: ECONNREFUSED](https://stackoverflow.com/questions/21206801/node-js-mysql-error-econnrefused) – Rahmat Waisi Jul 16 '23 at 10:46

1 Answers1

0

Try connecting through the commandline first using

mysql --host=localhost --user=root --password=<?> idmarketplace

That'll allow you to figure out if the issue is with the MySQL Javascript library or the MySQL configuration. If you're able to connect through the commandline, try changing "localhost" to "127.0.0.1". I had a similar problem when using MongoDB, where "localhost" was being interpreted as IPv6 ::1 and not as 127.0.0.1. So you might need to be more explicit. From your error message, it seems to be resolving to 127.0.0.1, but worth a shot.

If you aren't able to connect via commandline, then it's going to be a server configuration issue. These will be pretty basic suggestions, but doesn't hurt to check twice:

  • Make sure the binding is correct. If you're connecting from the same machine, it should be 127.0.0.1. If you're expecting remote connections, 0.0.0.0. If it's a remote connection, make sure port 3306 is open.
  • Make sure the username and password is correct. I would create a user specific to your database, as you should follow the principle of least privilege.
  • Make sure the database exists
  • I tried to use mysql --host=localhost --user=root --password=> idmarketplace, and it threw this error to me "mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)". I also tried to change localhost to 127.0.0.1 as well. The second bullet point that you mentioned is to make sure that my password and username is correct. I think that it's correct since when I use PopSQL for the same password, it works. For third point, I use the sql client command line to check. – Elmond Pattanan Jul 16 '23 at 13:57
  • If it isn’t able to connect via commandline, then you’ll need to look into the actual configuration of MySQL. Something is preventing the connection, not limited to the Javascript application. – Landon Crabtree Jul 16 '23 at 17:32