我刚刚开始接触Node.js。我来自PHP背景,所以我相当习惯使用MySQL来满足我的所有数据库需求。

我如何使用MySQL与Node.js?


当前回答

KnexJs可以在Node.JS和浏览器中用作SQL查询构建器。 我发现它很容易使用。让我们试试它- Knex.js

$ npm install knex --save
# Then add one of the following (adding a --save) flag:
$ npm install pg
$ npm install sqlite3
$ npm install mysql
$ npm install mysql2
$ npm install mariasql
$ npm install strong-oracle
$ npm install oracle
$ npm install mssql


var knex = require('knex')({
  client: 'mysql',
  connection: {
    host : '127.0.0.1',
    user : 'your_database_user',
    password : 'your_database_password',
    database : 'myapp_test'
  }
});

你可以这样用

knex.select('*').from('users')

or

knex('users').where({
  first_name: 'Test',
  last_name:  'User'
}).select('id')

其他回答

查看node.js模块列表

node-mysql -一个实现MySQL协议的node.js模块 node-mysql2 -另一个纯JS异步驱动程序。流水线,准备好的语句。 node-mysql-libmysqlclient -基于libmysqlclient的MySQL异步绑定

Node-mysql看起来很简单:

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret',
});

connection.connect(function(err) {
  // connected! (unless `err` is set)
});

查询:

var post  = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {
  // Neat!
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'

通过安装库连接mysql数据库。在这里,选择了稳定且易于使用的node-mysql模块。

npm install mysql@2.0.0-alpha2

var http = require('http'),
   mysql = require('mysql');

var sqlInfo = {
   host: 'localhost',
   user: 'root',
   password: 'urpass',
   database: 'dbname'
}
client = mysql.createConnection(sqlInfo);

client.connect();

用于NodeJS mysql连接和查询的例子

KnexJs可以在Node.JS和浏览器中用作SQL查询构建器。 我发现它很容易使用。让我们试试它- Knex.js

$ npm install knex --save
# Then add one of the following (adding a --save) flag:
$ npm install pg
$ npm install sqlite3
$ npm install mysql
$ npm install mysql2
$ npm install mariasql
$ npm install strong-oracle
$ npm install oracle
$ npm install mssql


var knex = require('knex')({
  client: 'mysql',
  connection: {
    host : '127.0.0.1',
    user : 'your_database_user',
    password : 'your_database_password',
    database : 'myapp_test'
  }
});

你可以这样用

knex.select('*').from('users')

or

knex('users').where({
  first_name: 'Test',
  last_name:  'User'
}).select('id')

在我看来,你应该尝试MySQL Connector/Node.js,这是MySQL的官方Node.js驱动程序。 详见ref-1和ref-2。 我尝试了mysqljs/mysql,这里是可用的,但我没有找到这个库的类,方法,属性的详细文档。

所以我切换到标准的MySQL Connector/Node.js和X DevAPI,因为它是一个异步的基于承诺的客户端库,并提供了良好的文档。 看一下下面的代码片段:

const mysqlx = require('@mysql/xdevapi');
const rows = [];

mysqlx.getSession('mysqlx://localhost:33060')
.then(session => {
    const table = session.getSchema('testSchema').getTable('testTable');

    // The criteria is defined through the expression.
    return table.update().where('name = "bar"').set('age', 50)
        .execute()
        .then(() => {
            return table.select().orderBy('name ASC')
                .execute(row => rows.push(row));
        });
})
.then(() => {
    console.log(rows);
});

node-mysql可能是用于MySQL数据库的最好的模块之一,它是积极维护和良好的文档。