Is it possible to access the MySQL database directly from Javascript

Is there a way to access the MySql database using fully client-side Javascript, or do you need to go through a server-side language like PHP or C #?

thanks

+3
javascript mysql
source share
6 answers

Javascript, if it is running in a browser, does not have access to the MySQL database. Firstly, this is a technical limitation, because Javascript is not able to transmit arbitrary protocols (no, WebSockets is not a solution). Please note that Node.js, which is server-side and everything, is a "different kind of javascript."

Then there is a security problem. If your javascript can directly access the database, I could easily access the database myself. I could read and manipulate the same data that your javascript can use. Security, calling it a nightmare, would be a euphemism.

You will have to - and WANT - to route access to the database through the server-side application. If this application is written in PHP, C # or Assembly is not a big deal. With Node.js, you can even use server-side Javascript. Use what is convenient for you.

+10
source share

You need to go through the server. Providing direct access to the database on the client side would be a serious security issue, even if you could do it.

Please note that you can use JavaScript on the server, you do not need to use PHP or C #. There are several server-side JavaScript engines that you can use, NodeJS (which runs the Google V8 engine) and Rhino (which runs on the Java stack) are two of them. And, of course, Microsoft has supported JScript on the server for at least 15 years.

+2
source share

Yes, you can use a Javascript library such as Node.js to execute MySQL queries in Javascript is just a bad idea (TM) for this client side, as you will need to send authentication data to users and allow clients to connect to your MySQL server, that is, it should be accessible to the Internet for any IP! Not good. However, if you really wanted to just because you prefer Javascript, but are only going to do it on the server, try: ( What MySQL drivers are available for Node.js? ).

What you have to do is implement a small server-side script, possibly in PHP or Perl or Python or C #, which only displays the data you need from the request and uses AJAX to process the response.

+1
source share

You need to use the server language. Otherwise, it will be a huge security hole. Any user can access this database and send him arbitrary SQL queries.

0
source share

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

0
source share

In theory, this should be possible using new features such as web sockets, etc. But you should not do this, given that you have to publish your credentials. Create a server side wrapper / negotiator script in any available language that you want to use and call it. You can also use it to check inputs (for example, filter too absurd records or impossible values, depending on what you do).

-one
source share

All Articles