6

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#?

Thanks

JMK
  • 27,273
  • 52
  • 163
  • 280

6 Answers6

14

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.

rodneyrehm
  • 13,442
  • 1
  • 40
  • 56
  • It is possible, yes however, not advised you can use for example `mysql-live-client` (from npm). I just fail to understand why would you expose your database credentials and security but maybe there's a case for it :) https://www.npmjs.com/package/mysql-live-client#principles-of-mysql-live – Klederson Bueno Nov 12 '19 at 18:12
  • If the user had to _enter_ their credentials into the page after it loaded rather than having them baked in there would be no security issue. There are some use cases for this. – Artelius Aug 05 '20 at 23:35
3

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

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.

Community
  • 1
  • 1
deed02392
  • 4,799
  • 2
  • 31
  • 48
0

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

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

Jibi Abraham
  • 4,636
  • 2
  • 31
  • 60
-1

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).

Mario
  • 35,726
  • 5
  • 62
  • 78