JavaScript 可以连接 MySQL 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3020751/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:57:10  来源:igfitidea点击:

Can JavaScript connect with MySQL?

javascriptmysqlajaxjson

提问by Anonymous the Great

Can JavaScript connect with MySQL? If so, how?

JavaScript 可以连接 MySQL 吗?如果是这样,如何?

采纳答案by konradowy

No, JavaScript can not directly connect to MySQL. But you can mix JS with PHP to do so.

不,JavaScript 不能直接连接到 MySQL。但是您可以将 JS 与 PHP 混合使用。

JavaScript is a client-side language and your MySQL database is going to be running on a server

JavaScript 是一种客户端语言,您的 MySQL 数据库将在服务器上运行

回答by LeeGee

Client-side JavaScript cannot access MySQL without some kind of bridge. But the above bold statements that JavaScript is just a client-side language are incorrect -- JavaScript can run client-side and server-side, as with Node.js.

如果没有某种桥接器,客户端 JavaScript 就无法访问 MySQL。但是上面关于 JavaScript 只是一种客户端语言的大胆声明是不正确的——JavaScript 可以在客户端和服务器端运行,就像 Node.js 一样。

Node.js can access MySQL through something like https://github.com/sidorares/node-mysql2

Node.js 可以通过类似https://github.com/sidorares/node-mysql2 的方式访问 MySQL

You might also develop something using Socket.IO

你也可以使用 Socket.IO 开发一些东西

Did you mean to ask whether a client-side JS app can access MySQL? I am not sure if such libraries exist, but they are possible.

你的意思是问客户端JS应用程序是否可以访问MySQL?我不确定这样的库是否存在,但它们是可能的。

EDIT: Since writing, we now have MySQL Cluster:

编辑:自写作以来,我们现在有了MySQL 集群

The MySQL Cluster JavaScript Driver for Node.js is just what it sounds like it is – it's a connector that can be called directly from your JavaScript code to read and write your data. As it accesses the data nodes directly, there is no extra latency from passing through a MySQL Server and need to convert from JavaScript code//objects into SQL operations. If for some reason, you'd prefer it to pass through a MySQL Server (for example if you're storing tables in InnoDB) then that can be configured.

Node.js 的 MySQL 集群 JavaScript 驱动程序就像它听起来的那样——它是一个连接器,可以直接从你的 JavaScript 代码中调用来读取和写入你的数据。由于它直接访问数据节点,因此通过 MySQL 服务器没有额外的延迟,并且需要从 JavaScript 代码//对象转换为 SQL 操作。如果出于某种原因,您希望它通过 MySQL 服务器(例如,如果您将表存储在 InnoDB 中),则可以对其进行配置。

  • JSDB为 DB 提供了一个 JS 接口。

  • 来自 sindresorhus 的一精选的 Node.js数据库包

回答by Timbergus

If you want to connect to a MySQL database using JavaScript, you can use Node.js and a library called mysql. You can create queries, and get results as an array of registers. If you want to try it, you can use my project generatorto create a backend and choose MySQLas the database to connect. Then, just expose your new REST APIor GraphQLendpoint to your front and start working with your MySQL database.

如果要使用 JavaScript 连接到 MySQL 数据库,可以使用 Node.js 和名为mysql的库。您可以创建查询,并以寄存器数组的形式获得结果。如果你想尝试,你可以使用我的项目生成器创建一个后端并选择MySQL作为连接的数据库。然后,只需将您的新REST APIGraphQL端点公开到您的前端并开始使用您的 MySQL 数据库。



OLD ANSWER LEFT BY NOSTALGIA

怀旧留下的旧答案

THEN

然后

As I understand the question and correct me if I am wrong, it refers to the classic server model with JavaScript only on the client-side. In this classic model, with LAMPservers (Linux, Apache, MySQL, PHP) the language in contact with the database was PHP, so to request data to the database you need to write PHP scripts and echothe returning data to the client. Basically, the distribution of the languages according to physical machines was:

由于我理解这个问题并纠正我,如果我错了,它指的是仅在客户端使用 JavaScript 的经典服务器模型。在这款经典机型,与LAMP服务器(Linux操作系统,Apache,MySQL和PHP)与数据库中的联系人的语言是PHP,所以请求数据,你需要编写PHP脚本和数据库呼应的将数据返回到客户端。基本上,根据物理机器的语言分布是:

  1. Server Side:PHP and MySQL.
  2. Client Side:HTML/CSS and JavaScript.
  1. 服务器端:PHP 和 MySQL。
  2. 客户端:HTML/CSS 和 JavaScript。

This answered to an MVC model (Model, View, Controller) where we had the following functionality:

这回答了我们具有以下功能的 MVC 模型(模型、视图、控制器):

  1. MODEL:The model is what deals with the data, in this case, the PHP scripts that manage variables or that access data stored, in this case in our MySQL database and send it as JSON data to the client.
  2. VIEW:The view is what we see and it should be completely independent of the model. It just needs to show the data contained in the model, but it shouldn't have relevant data on it. In this case, the view uses HTML and CSS. HTML to create the basic structure of the view, and CSS to give the shape to this basic structure.
  3. CONTROLLER:The controller is the interface between our model and our view. In this case, the language used is JavaScript and it takes the data the model send us as a JSON package and put it inside the containers that offer the HTML structure. The way the controller interacts with the model is by using AJAX. We use GETand POSTmethods to call PHP scripts on the server-side and to catch the returned data from the server.
  1. 模型:模型处理数据,在这种情况下,是管理变量或访问存储的数据的 PHP 脚本,在这种情况下,在我们的 MySQL 数据库中并将其作为 JSON 数据发送到客户端。
  2. VIEW:视图就是我们所看到的,它应该完全独立于模型。它只需要显示模型中包含的数据,但不应该有相关数据。在这种情况下,视图使用 HTML 和 CSS。HTML 创建视图的基本结构,CSS 为这个基本结构赋予形状。
  3. 控制器:控制器是我们的模型和视图之间的接口。在这种情况下,使用的语言是 JavaScript,它将模型作为 JSON 包发送给我们的数据放入提供 HTML 结构的容器中。控制器与模型交互的方式是使用AJAX。我们使用GETPOST方法在服务器端调用 PHP 脚本并捕获从服务器返回的数据。

For the controller, we have really interesting tools like jQuery, as "low-level" library to control the HTML structure (DOM), and then new, more high-level ones as Knockout.jsthat allow us to create observers that connect different DOM elements updating them when events occur. There is also Angular.jsby Google that works in a similar way, but seems to be a complete environment. To help you to choose among them, here you have two excellent analyses of the two tools: Knockout vs. Angular.jsand Knockout.js vs. Angular.js. I am still reading. Hope they help you.

对于控制器,我们有非常有趣的工具,如jQuery,作为控制 HTML 结构 (DOM) 的“低级”库,然后是新的、更高级的工具,如Knockout.js,允许我们创建连接不同的观察者DOM 元素在事件发生时更新它们。Google 的Angular.js也以类似的方式工作,但似乎是一个完整的环境。为了帮助您在其中进行选择,这里有两个对这两个工具的出色分析:Knockout 与 Angular.jsKnockout.js 与 Angular.js。我还在读书。希望他们能帮助你。

NOW

现在

In modern servers based in Node.js, we use JavaScript for everything. Node.js is a JavaScript environment with many libraries that work with Google V8, Chrome JavaScript engine. The way we work with these new servers is:

在基于 Node.js 的现代服务器中,我们对一切都使用 JavaScript。Node.js 是一个 JavaScript 环境,其中包含许多与 Google V8、Chrome JavaScript 引擎配合使用的库。我们使用这些新服务器的方式是:

  1. Node.jsand Express:The mainframe where the server is built. We can create a server with a few lines of code or even use libraries like Express to make even easier to create the server. With Node.js and Express, we will manage the petitions to the server from the clients and will answer them with the appropriate pages.
  2. Jade:To create the pages we use a templating language, in this case, Jade, that allow us to write web pages as we were writing HTML but with differences (it take a little time but is easy to learn). Then, in the code of the server to answer the client's petitions, we just need to render the Jade code into a "real" HTML code.
  3. Stylus:Similar to Jade but for CSS. In this case, we use a middleware function to convert the stylus file into a real CSS file for our page.
  1. Node.jsExpress构建服务器的大型机。我们可以用几行代码创建一个服务器,甚至可以使用像 Express 这样的库来更轻松地创建服务器。使用 Node.js 和 Express,我们将管理从客户端到服务器的请求,并用适当的页面回答它们。
  2. Jade为了创建页面,我们使用模板语言,在这种情况下,Jade,它允许我们像编写 HTML 一样编写网页,但有所不同(这需要一点时间,但很容易学习)。然后,在响应客户端请求的服务器代码中,我们只需要将 Jade 代码渲染成“真正的”HTML 代码。
  3. 手写笔类似于 Jade,但用于 CSS。在这种情况下,我们使用中间件函数将手写笔文件转换为我们页面的真实 CSS 文件。

Then we have a lot of packages we can install using the NPM(Node.js package manager) and use them directly in our Node.js server just requiring it (for those of you that want to learn Node.js, try this beginner tutorialfor an overview). And among these packages, you have some of them to access databases. Using this you can use JavaScript on the server-side to access My SQL databases.

然后我们有很多包,我们可以使用NPM(Node.js 包管理器)安装并直接在我们的 Node.js 服务器中使用它们,只需要它(对于那些想要学习 Node.js 的人,试试这个初学者教程概述)。在这些包中,您有一些可以访问数据库。使用它,您可以在服务器端使用 JavaScript 来访问我的 SQL 数据库。

But the best you can do if you are going to work with Node.js is to use the new NoSQL databases like MongoDB, based on JSON files. Instead of storing tables like MySQL, it stores the data in JSON structures, so you can put different data inside each structure like long numeric vectors instead of creating huge tables for the size of the biggest one.

但是,如果您打算使用 Node.js,您可以做的最好的事情就是使用基于 JSON 文件的新 NoSQL 数据库,例如MongoDB。它不是像 MySQL 那样存储表,而是将数据存储在 JSON 结构中,因此您可以将不同的数据放入每个结构中,例如长数值向量,而不是为最大的大小创建巨大的表。

I hope this brief explanation becomes useful to you, and if you want to learn more about this, here you have some resources you can use:

我希望这个简短的解释对你有用,如果你想了解更多关于这方面的信息,这里有一些你可以使用的资源:

  • Egghead: This site is full of great short tutorials about JavaScript and its environment. It worths a try. And the make discounts from time to time.
  • Code School: With a free and very interesting course about Chrome Developer tools to help you to test the client-side.
  • Codecademy: With free courses about HTML, CSS, JavaScript, jQuery, and PHP that you can follow with online examples.
  • 10gen Education: With everything you need to know about MongoDB in tutorials for different languages.
  • W3Schools: This one has tutorials about all this and you can use it as a reference place because it has a lot of shortcode examples really useful.
  • Udacity: A place with free video courses about different subjects with a few interesting ones about web development and my preferred, an amazing WebGL course for 3D graphics with JavaScript.
  • Egghead:这个站点充满了关于 JavaScript 及其环境的简短教程。值得一试。并且不时打折。
  • 代码学校:提供关于 Chrome 开发者工具的免费且非常有趣的课程,可帮助您测试客户端。
  • Codecademy:提供有关 HTML、CSS、JavaScript、jQuery 和 PHP 的免费课程,您可以通过在线示例进行学习。
  • 10gen 教育:在不同语言的教程中包含您需要了解的有关 MongoDB 的所有信息。
  • W3Schools:这个有关于所有这些的教程,你可以将它用作参考位置,因为它有很多非常有用的短代码示例。
  • Udacity:一个提供关于不同主题的免费视频课程的地方,其中有一些关于 Web 开发的有趣课程和我最喜欢的,一个令人惊叹的 WebGL 课程,用于使用 JavaScript 进行 3D 图形。

I hope it helps you to start.

我希望它可以帮助你开始。

Have fun!

玩得开心!

回答by Kerry

I think you would need to add something like PHP into the equation. PHP to interact with the database and then you could make AJAX calls with Javascript.

我认为您需要在等式中添加诸如 PHP 之类的东西。PHP 与数据库交互,然后您可以使用 Javascript 进行 AJAX 调用。

回答by Atul Chaudhary

Bit late but recently I have found out that MySql 5.7 got http plugin throuh which user can directly connect to mysql now.

有点晚了,但最近我发现 MySql 5.7 有了 http 插件,用户现在可以直接连接到 mysql。

Look for Http Client for mysql 5.7

为 mysql 5.7 寻找 Http 客户端

回答by Dean Harding

Simple answer is: no.

简单的回答是:不。

JavaScript is a client-side language that runs in the browser (node.jsnotwithstanding) and MySQL is a server-side technology that runs on the server.

JavaScript 是一种在浏览器中运行的客户端语言(尽管有node.js),而 MySQL 是一种在服务器上运行的服务器端技术。

That means you typically use a server-side language like ASP.NET or PHP to connect to the database.

这意味着您通常使用服务器端语言(如 ASP.NET 或 PHP)连接到数据库。

回答by Ivan Wang

YES? Have a look a meteor. Links:

是的?看看流星。链接:

http://meteor.com/screencastand http://net.tutsplus.com/tutorials/javascript-ajax/whats-this-meteor-thing/

http://meteor.com/screencasthttp://net.tutsplus.com/tutorials/javascript-ajax/whats-this-meteor-thing/

I don't understand how it is done. But Nettuts+ put this thing in the javascript-ajax section, maybe magic happens.

我不明白它是如何完成的。但是 Nettuts+ 把这个东西放在了 javascript-ajax 部分,也许奇迹发生了。

It also shows some way to connect and insert to MongoDB with JS, like this:

它还展示了一些使用 JS 连接和插入 MongoDB 的方法,如下所示:

Products.insert({Name : "Hammer", Price : 4.50, InStock : true});
Products.insert({Name : "Wrench", Price : 2.70, InStock : true});
Products.insert({Name : "Screw Driver", Price : 3.00, InStock : false});
Products.insert({Name : "Drill", Price : 5.25, InStock : true});

回答by Nick Manning

Yes. There is an HTTP plugin for MySQL.

是的。MySQL 有一个 HTTP 插件。

http://blog.ulf-wendel.de/2014/mysql-5-7-http-plugin-mysql/

http://blog.ulf-wendel.de/2014/mysql-5-7-http-plugin-mysql/

I'm just googling about it now, which led me to this stackoverflow question. You should be able to AJAX a MySQL database now or in the near future (they claim it's not ready for production).

我现在只是在谷歌上搜索它,这让我想到了这个 stackoverflow 问题。您现在或在不久的将来应该能够对 MySQL 数据库进行 AJAX(他们声称它还没有准备好用于生产)。

回答by Kusmeroglu

Depending on your environment, you could use Rhino to do this, see Rhino website. This gives you access to all of the Java libraries from within JavaScript.

根据您的环境,您可以使用 Rhino 来执行此操作,请参阅Rhino 网站。这使您可以从 JavaScript 中访问所有 J​​ava 库。

回答by Fiach Reid

Typically, you need a server side scripting language like PHP to connect to MySQL, however, if you're just doing a quick mockup, then you can use http://www.mysqljs.comto connect to MySQL from Javascript using code as follows:

通常,您需要像 PHP 这样的服务器端脚本语言来连接到 MySQL,但是,如果您只是在做一个快速模型,那么您可以使用http://www.mysqljs.com使用以下代码从 Javascript 连接到 MySQL如下:

MySql.Execute(
    "mysql.yourhost.com", 
    "username", 
    "password", 
    "database", 
    "select * from Users", 
    function (data) {
        console.log(data)
});

It has to be mentioned that this is not a secure way of accessing MySql, and is only suitable for private demos, or scenarios where the source code cannot be accessed by end users, such as within Phonegap iOS apps.

需要说明的是,这不是一种安全的MySql访问方式,只适用于私有demo,或者终端用户无法访问源代码的场景,比如Phonegap iOS应用内。