Truy vấn nhóm mysql của Nodejs

Máy khách MySQL cho Nút. js tập trung vào hiệu suất. Hỗ trợ các câu lệnh đã chuẩn bị, mã hóa không phải utf8, giao thức nhật ký nhị phân, nén, ssl nhiều hơn nữa

Mục lục

Lịch sử và Tại sao MySQL2

Dự án MySQL2 là sự tiếp nối của MySQL-Native. Mã trình phân tích cú pháp giao thức đã được viết lại từ đầu và api đã thay đổi để phù hợp với mysqljs/mysql phổ biến. Nhóm MySQL2 đang làm việc cùng với nhóm mysqljs/mysql để tìm ra mã được chia sẻ và chuyển nó vào tổ chức mysqljs

MySQL2 chủ yếu là API tương thích với mysqljs và hỗ trợ phần lớn các tính năng. MySQL2 cũng cung cấp các tính năng bổ sung này

  • Hiệu suất nhanh hơn / tốt hơn
  • báo cáo chuẩn bị
  • Giao thức nhật ký nhị phân MySQL
  • Máy chủ MySQL
  • Hỗ trợ mở rộng cho Mã hóa và Đối chiếu
  • Giấy gói lời hứa
  • Nén
  • Chuyển đổi SSL và xác thực
  • Luồng tùy chỉnh

Cài đặt

MySQL2 không có ràng buộc gốc và có thể được cài đặt trên Linux, Mac OS hoặc Windows mà không gặp bất kỳ sự cố nào

npm install --save mysql2

Truy vấn đầu tiên

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);

Sử dụng câu lệnh đã chuẩn bị

Với MySQL2, bạn cũng nhận được các câu lệnh đã chuẩn bị. Với các câu lệnh đã chuẩn bị, MySQL không phải chuẩn bị kế hoạch cho cùng một truy vấn mọi lúc, điều này dẫn đến hiệu suất tốt hơn. Nếu bạn không biết tại sao chúng lại quan trọng, vui lòng kiểm tra các cuộc thảo luận này

  • Làm thế nào các câu lệnh được chuẩn bị có thể bảo vệ khỏi các cuộc tấn công SQL Injection

MySQL cung cấp trình trợ giúp

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);
2 sẽ chuẩn bị và truy vấn câu lệnh. Bạn cũng có thể tự chuẩn bị/không chuẩn bị câu lệnh bằng phương pháp _________ 13 / _______ 14

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// execute will internally call prepare and query
connection.execute(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Rick C-137', 53],
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available

    // If you execute same statement again, it will be picked from a LRU cache
    // which will save query preparation time and give better performance
  }
);

Sử dụng nhóm kết nối

Nhóm kết nối giúp giảm thời gian kết nối với máy chủ MySQL bằng cách sử dụng lại kết nối trước đó, để chúng mở thay vì đóng khi bạn hoàn thành chúng

Điều này giúp cải thiện độ trễ của các truy vấn khi bạn tránh được tất cả các chi phí đi kèm với việc thiết lập kết nối mới

// get the client
const mysql = require('mysql2');

// Create the connection pool. The pool-specific settings are the defaults
const pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  database: 'test',
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0
});

Nhóm không tạo tất cả các kết nối trước mà tạo chúng theo yêu cầu cho đến khi đạt đến giới hạn kết nối

Bạn có thể sử dụng nhóm theo cách tương tự như các kết nối (sử dụng

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);
5 và
// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);
6)

// For pool initialization, see above
pool.query("SELECT field FROM atable", function(err, rows, fields) {
   // Connection is automatically released when query resolves
})

Ngoài ra, cũng có khả năng lấy kết nối thủ công từ nhóm và trả lại sau

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);
0

Sử dụng Promise Wrapper

MySQL2 cũng hỗ trợ Promise API. Cái nào hoạt động rất tốt với ES7 async đang chờ

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);
1

MySQL2 sử dụng đối tượng

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);
7 mặc định có sẵn trong phạm vi. Nhưng bạn có thể chọn triển khai
// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);
7 nào bạn muốn sử dụng

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);
4

MySQL2 cũng cho thấy một. promise() trên Nhóm, vì vậy bạn có thể tạo kết nối hứa hẹn/không hứa hẹn từ cùng một nhóm

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);
5

MySQL2 hiển thị một. promise() trên Kết nối, để "nâng cấp" kết nối không hứa hẹn hiện có để sử dụng lời hứa

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);
6

Kết quả mảng

Nếu bạn có hai cột có cùng tên, bạn có thể muốn nhận kết quả dưới dạng một mảng thay vì một đối tượng để ngăn chúng xung đột. Đây là một sai lệch so với thư viện Node MySQL

Ví dụ.

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);
9

Bạn có thể bật cài đặt này ở cấp độ kết nối (áp dụng cho tất cả các truy vấn) hoặc ở cấp độ truy vấn (chỉ áp dụng cho truy vấn cụ thể đó)

Tùy chọn kết nối

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);
0

Tùy chọn truy vấn

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

// with placeholder
connection.query(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Page', 45],
  function(err, results) {
    console.log(results);
  }
);
1

API và cấu hình

MySQL2 chủ yếu là API tương thích với Node MySQL. Bạn nên kiểm tra tài liệu API của họ để xem tất cả các tùy chọn API có sẵn

Nếu bạn tìm thấy bất kỳ sự không tương thích nào với Node MySQL, vui lòng báo cáo qua Trình theo dõi sự cố. Chúng tôi sẽ khắc phục sự không tương thích được báo cáo trên cơ sở ưu tiên

Tài liệu

Bạn có thể tìm thêm tài liệu chi tiết tại đây. Bạn cũng nên kiểm tra các ví dụ mã khác nhau để hiểu các khái niệm nâng cao

Sự nhìn nhận

  • Giao thức nội bộ được viết bởi @sidorares MySQL-Native
  • Các hằng số, phép nội suy tham số SQL, Pooling, lớp
    // get the client
    const mysql = require('mysql2');
    
    // create the connection to database
    const connection = mysql.createConnection({
      host: 'localhost',
      user: 'root',
      database: 'test'
    });
    
    // execute will internally call prepare and query
    connection.execute(
      'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
      ['Rick C-137', 53],
      function(err, results, fields) {
        console.log(results); // results contains rows returned by server
        console.log(fields); // fields contains extra meta data about results, if available
    
        // If you execute same statement again, it will be picked from a LRU cache
        // which will save query preparation time and give better performance
      }
    );
    0 được lấy từ nút-mysql
  • Mã nâng cấp SSL dựa trên mã @TooTallNate
  • Cờ api kết nối an toàn / kết nối nén tương thích với máy khách MariaSQL
  • người đóng góp

Đóng góp

Muốn cải thiện điều gì đó trong

// get the client
const mysql = require('mysql2');

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});

// execute will internally call prepare and query
connection.execute(
  'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
  ['Rick C-137', 53],
  function(err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available

    // If you execute same statement again, it will be picked from a LRU cache
    // which will save query preparation time and give better performance
  }
);
1. Vui lòng kiểm tra Đóng góp. md để được hướng dẫn chi tiết về cách bắt đầu

Làm cách nào để tạo nhóm kết nối MySQL trong Node js?

Tích hợp Nodejs MySQL. Kết nối hồ bơi . var pool = mysql. createPool({ connectionLimit. 7, máy chủ. 'máy chủ cục bộ', người dùng. 'gốc', mật khẩu. '', cơ sở dữ liệu. 'todoapp' });

Làm cách nào để sử dụng nhóm kết nối trong nút js?

Mục lục .
Định cấu hình nút. js Driver (Cài đặt máy khách)
Chạy nút. js Ví dụ
Nút. thuộc tính kết nối js
Nút. tổng hợp kết nối js. Cải thiện nút. js Hiệu suất với Tổng hợp Kết nối
lớp kết nối
Lớp Kết quả
Lớp tuyên bố
Mô-đun luồng

Nodejs có thể giao tiếp với MySQL không?

Khi bạn đã thiết lập và chạy MySQL trên máy tính của mình, bạn có thể truy cập nó bằng cách sử dụng Nút. js . Để truy cập cơ sở dữ liệu MySQL bằng Node. js, bạn cần có trình điều khiển MySQL.

Sử dụng MySQL với nút js có tốt không?

js được kết hợp với MongoDB và các cơ sở dữ liệu NoSQL khác, nhưng Node. js cũng hoạt động tốt với các cơ sở dữ liệu quan hệ như MySQL . Nếu bạn muốn viết một microservice mới với Node. js cho cơ sở dữ liệu hiện có, rất có thể bạn sẽ sử dụng MySQL, một trong những cơ sở dữ liệu mã nguồn mở phổ biến nhất thế giới.