Is it possible to access a MySQL database straight from Javascript

Is there a way to access a MySql database using entirely client side Javascript or do you need to go through a server side language such as PHP or C#?



Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Javascript, if run in the browser, has no way of accessing a MySQL Database. For one, this is a technical limitation, because Javascript has no way of communicating arbitrary protocols (no, WebSockets are not the solution). Note that Node.js, being server side and all, is a “different kind of javascript”.

Then there is the security issue. If your javascript could access the database directly, I could easily access the database myself. I would be able to read and manipulate the same data your javascript can. Security-wise calling this a nightmare would be an euphemism.

You’ll have to – and WANT TO – route database access through a server side application. If that app is written in PHP, C# or Assembly doesn’t really matter much. With Node.js you can even use Javascript on the server side. Use what you’re comfortable with.

Method 2

You need to go through the server. Giving the client-side code direct access to the database would be a massive security hole, even if you could do it.

Note that you can use JavaScript on the server, you don’t have to use PHP or C#. There are several server-side JavaScript engines you can use, NodeJS (which runs Google’s V8 engine) and Rhino (which runs on the Java stack) being two of them. And of course, Microsoft has supported JScript on the server for at least 15 years.

Method 3

Yes, you can use a Javascript library like Node.js to perform MySQL queries in Javascript – it’s just a really bad idea (TM) to do this client side, as you’d need to send users the authentication details and provide the ability for clients to connect to your MySQL server, meaning it’d have to be internet accessible to any IP! Not good. However if you really wanted just because you prefer Javascript but you only intend to do it on the server, try: (What MySQL drivers are available for node.js?).

What you should do is implement a small server-side script perhaps in PHP or Perl or Python or C#, which outputs just the data you want from the query and use AJAX to process the response.

Method 4

You need to use a server side language. Otherwise that would be a huge security hole. Anyone could access this database and send arbitrary SQL queries to it.

Method 5

For databases like MySql, you definitely should use a server side language. If you want javascript to be your only language, I suggest you check out nodejs or couchDB

Method 6

In theory, it should be possible using the newer features like web sockets, etc. But you shouldn’t do it, considering you’d have to make your credentials public. Create a server side wrapper/negotiator script in whatever available language you’d like to use and call that one. You can as well use it to validate inputs (e.g. filter too absurd highscores or impossible values; depending on what you’re doing).

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x