0

I am trying to connect to MySQL database using Javascript through the code I found online:

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "Restaurants",
  socketPath: '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock'
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  var sql = "INSERT INTO customers (name, address) VALUES ('Company    
    Inc', 'Highway 37')";
  con.query(sql, function (err, result) {
  if (err) throw err;
  console.log("1 record inserted");
  });
});

Per online suggestions, I have added the following path to the socket:

socketPath: '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock'

Running this file through the Terminal, I get the following error:

Error: connect ELOOP   
/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
   at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
  at Protocol._enqueue   
    (/Users/aliaksandrnenartovich/node_modules/mysql
    /lib/protocol/Protocol.js:144:48)
  at Protocol.handshake  
    (/Users/aliaksandrnenartovich/node_modules/mysql
    /lib/protocol/Protocol.js:51:23)
  at Connection.connect (/Users/aliaksandrnenartovich/node_modules
    /mysql/lib/Connection.js:119:18)
  at Object.<anonymous>  
     (/Users/aliaksandrnenartovich/Desktop/JScript/te.js:11:5)
  at Module._compile (module.js:653:30)
  at Object.Module._extensions..js (module.js:664:10)
  at Module.load (module.js:566:32)
  at tryModuleLoad (module.js:506:12)
  at Function.Module._load (module.js:498:3)
  at Function.Module.runMain (module.js:694:10)

I have XAMPP 5.6.33-0 installed, and I can successfully start the Apache server and MySQL database manually, but when I try to connect through Node.js it does not work. I have researched this error online but there seems to be little information as to what may be going on. I understand that this has something to do with mysql.sock file but I am just not sure how to deal with it.

Any suggestions will be greatly appreciated. Thank you in advance!

4
  • Have you successfully connected to the database with those credentials/parameters using some SQL client like MySQL Workbench? Just to be sure they are correct. Commented Apr 28, 2019 at 8:10
  • Thanks for responding. I can successfully start Apache server and MySQL database by opening XAMMP UI and clicking Start button. I can then use the database through Java. I am not sure what username and password I use though (I think I use root for username). Do you think the problem may be that I’m using the wrong username and/or password? Commented Apr 28, 2019 at 14:25
  • Michal, changed my credentials, and it works now! How dumb of me. Can you post it as an answer so I can give you credit for it? Thanks! Commented Apr 28, 2019 at 14:56
  • No worries, from time to time it happens to everyone. Commented Apr 28, 2019 at 17:14

1 Answer 1

1

Have you successfully connected to the database with those credentials/parameters using some SQL client like MySQL Workbench? Just to be sure they are correct.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.