Ask coding questions

← Back to all posts
Configuring a Connection in MySQL and Node.js
vedprad1 (470)

Configuring a Connection in MySQL and Node.js

I am trying to use MySQL in Node.js to use databases. However, when i try to tun the program that can be accessed here, I get this error:

Process crashed with: Error: connect ECONNREFUSED
at TCPConnectWrap.afterConnect as oncomplete

at Protocol._enqueue (/home/runner/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/home/runner/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/home/runner/node_modules/mysql/lib/Connection.js:119:18)
at evalmachine.<anonymous>:11:5
at Script.runInContext (vm.js:107:20)
at Object.runInContext (vm.js:285:6)
at evaluate (/run_dir/repl.js:133:14)
at ReadStream.<anonymous> (/run_dir/repl.js:116:5)
at ReadStream.emit (events.js:189:13)
at addChunk (_stream_readable.js:284:12)


Does anyone know what I can do?

The code can be found here.

Answered by heyitsmarcus (280) [earned 5 cycles]
View Answer
heyitsmarcus (280)

You definitely can't run a MySQL database inside of as a localhost database. I don't know if that is a possible planned feature in the future or not.

But, another great option for NodeJS and SQL, that is easy to run and use, is AlaSQL. You can find it in the package manager under alasql. You should definitely check it out! I made a repl with some simple AlaSQL code that should be very familiar to you. EDIT: AlaSQL does not actually store data. It just interprets it from a resource.

Here is their website:

Here is their Github page:

Another option for storing data is It's totally free, and you get easy to use JSON responses. It's good for small projects with small amounts of data. They have a great library appropriately named in the package manager for working with their JSON data.

heyitsmarcus (280)

@heyitsmarcus sorry, @vedprad1, I accidentally used your repl link instead of mine! I edited the comment for my repl hahaha

Vandesm14 (486)

@heyitsmarcus Is mySQL able to work with nodejs via polygott?

heyitsmarcus (280)

@Vandesm14 There is a package for MySQL (aptly named mysql haha) that you can find in the Package manager here on You can connect to a remote MySQL server and work with it. You just can't spin up an instance of a localhost database yet, although whether local SQL databases are a planned feature or not, I have no idea.

heyitsmarcus (280)

And I made an update to the original post but I thought that AlaSQL would store the data into a server database, much the same way that WebSQL works, but that is not the case. AlaSQL just interprets and then allows you to query the data in a SQL format.

leon332157 (55)

I don't think you can use this code on because a repl won't have mysql installed locally. You can use an external Mysql instead.