是否可以直接从 Javascript 访问 MySQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9767614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to access a MySQL database straight from Javascript
提问by JMK
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#?
有没有办法完全使用客户端 Javascript 访问 MySql 数据库,还是需要通过服务器端语言(如 PHP 或 C#)?
Thanks
谢谢
回答by rodneyrehm
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".
Javascript,如果在浏览器中运行,则无法访问 MySQL 数据库。首先,这是一个技术限制,因为 Javascript 无法与任意协议进行通信(不,WebSockets 不是解决方案)。请注意,作为服务器端的 Node.js 是一种“不同类型的 javascript”。
Then there is the security issue. If yourjavascript could access the database directly, I could easily access the database myself. I would be able to read and manipulate the same data yourjavascript can. Security-wise calling this a nightmare would be an euphemism.
然后是安全问题。如果您的javascript 可以直接访问数据库,那么我自己就可以轻松访问数据库。我将能够读取和操作与您的javascript相同的数据。安全方面称这是一场噩梦将是一种委婉的说法。
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.
您必须 - 并且想要 - 通过服务器端应用程序路由数据库访问。如果该应用程序是用 PHP 编写的,则 C# 或 Assembly 并不重要。使用 Node.js,您甚至可以在服务器端使用 Javascript。使用你觉得舒服的东西。
回答by T.J. Crowder
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.
请注意,您可以在服务器上使用 JavaScript,而不必使用 PHP 或 C#。您可以使用多种服务器端 JavaScript 引擎,NodeJS(运行 Google 的 V8 引擎)和 Rhino(运行在 Java 堆栈上)是其中的两个。当然,Microsoft 已经在服务器上支持 JScript 至少 15 年了。
回答by deed02392
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?).
是的,您可以使用像 Node.js 这样的 Javascript 库在 Javascript 中执行 MySQL 查询 - 做这个客户端是一个非常糟糕的主意 (TM),因为您需要向用户发送身份验证详细信息并提供以下功能客户端连接到您的 MySQL 服务器,这意味着它必须可以通过任何 IP 访问互联网!不好。但是,如果您真的想要只是因为您更喜欢 Javascript,但您只想在服务器上执行此操作,请尝试:(node.js 有哪些 MySQL 驱动程序可用?)。
What you shoulddo 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.
您应该做的是实现一个小的服务器端脚本,可能是用 PHP 或 Perl 或 Python 或 C#,它只输出您想要的查询数据并使用 AJAX 处理响应。
回答by Jibi Abraham
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
对于像 MySql 这样的数据库,你绝对应该使用服务器端语言。如果您希望 javascript 成为您唯一的语言,我建议您查看 nodejs 或 couchDB
回答by Darin Dimitrov
You need to use a server side language. Otherwise that would be a huge security hole. Anyone could accessthis database and send arbitrary SQL queries to it.
您需要使用服务器端语言。否则,这将是一个巨大的安全漏洞。任何人都可以访问该数据库并向其发送任意 SQL 查询。
回答by Mario
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).
从理论上讲,应该可以使用 Web 套接字等较新的功能。但您不应该这样做,因为您必须公开您的凭据。使用您想要使用的任何可用语言创建服务器端包装器/谈判器脚本并调用该语言。您也可以使用它来验证输入(例如过滤太荒谬的高分或不可能的值;取决于您在做什么)。