Javascript 哪些 MySQL 驱动程序可用于 node.js?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3878818/
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
What MySQL drivers are available for node.js?
提问by Brad Barrows
Is there a Node.JS Driver for MySQL that is commonly used other than node-mysql?
除了node-mysql之外,是否有常用的 MySQL 的 Node.JS 驱动程序?
(It seems like there is not much activity with node.js database drivers. Is there a reason for this or is it just because Node.JS is so young?)
(似乎 node.js 数据库驱动程序没有太多活动。这是有原因的还是仅仅因为 Node.JS 太年轻了?)
采纳答案by NullUserException
Here are some options:
以下是一些选项:
- http://github.com/felixge/node-mysql(last update: Sep 29th)
- https://github.com/sidorares/node-mysql2(last update: Sep 04, 2018)
- http://github.com/felixge/node-mysql(最后更新:9 月 29 日)
- https://github.com/sidorares/node-mysql2(最后更新:2018 年 9 月 4 日)
回答by Mariano Iglesias
You can also try out a newer effort known as Node.js DBthat aims to provide a common framework for several database engines. It is built with C++ so performance is guaranteed.
您还可以尝试一种名为Node.js DB的新成果,旨在为多个数据库引擎提供一个通用框架。它是用 C++ 构建的,因此性能得到保证。
Specifically you could use its db-mysql driver for Node.js MySQL support.
具体来说,您可以将其 db-mysql 驱动程序用于Node.js MySQL 支持。
回答by Sannis
回答by sdepold
If you need an ORM for MySQL you might want to check out http://sequelizejs.com:)
如果您需要 MySQL 的 ORM,您可能需要查看http://sequelizejs.com:)
回答by jiy
For connecting to MySQL with node.js, I've had great success using node-odbc
为了使用 node.js 连接到 MySQL,我使用node-odbc取得了巨大的成功
It's also worked flawlessly for connecting to other databases such as IBM's DB2, and it's been surprisingly fast.
它还可以完美地连接到其他数据库,例如 IBM 的 DB2,而且速度非常快。
This pageis particularly useful for configuring ODBC on linux.
此页面对于在 linux 上配置 ODBC 特别有用。
After installing with yum install mysql-connector-odbc
, my /etc/odbc.ini file looks like this:
安装后yum install mysql-connector-odbc
,我的 /etc/odbc.ini 文件如下所示:
[MYSQL]
Description = MySQL ODBC Driver
Driver = /usr/lib64/libmyodbc3.so
I left out stuff such as server, user, database, port, password etc. so that I can set these from my connection string (I need to connect to multiple databases).
我遗漏了服务器、用户、数据库、端口、密码等内容,以便我可以从我的连接字符串中设置这些内容(我需要连接到多个数据库)。
After saving /etc/odbc.ini, it's installed with this command: odbcinst -i -s -l -f /etc/odbc.ini
保存 /etc/odbc.ini 后,使用以下命令安装它: odbcinst -i -s -l -f /etc/odbc.ini
And here's a code sample for testing it out:
这是用于测试的代码示例:
var odbc = require("odbc");
var db = new odbc.Database();
var conn = "dsn=mysql;server=localhost;user=root;database=mydb;port=3306;password=mypwd;command timeout=30000;";
db.open(conn, function(err) {
if(err) throw err;
var params = ['[email protected]'];
var qry = "select * users where email = ?";
db.query(qry, params, function(err, rows, def) {
if(err) console.log(err);
console.log(rows);
});
});
Or if you wanted to use coffeescript:
或者,如果您想使用 coffeescript:
odbc = require "odbc"
db = new odbc.Database()
conn = "dsn=mysql;server=localhost;user=root;database=mydb;port=3306;password=mypwd;command timeout=30000;"
db.open conn, (err) ->
throw err if err
qry = "select * from users where email = ?"
db.query sql, ["[email protected]"], (err, rows, def) ->
if err? then console.log err else
console.log rows