Mysql NodeJS

Cơ sở dữ liệu NoSQL khá phổ biến đối với các nhà phát triển Node, với MongoDB (chữ “M” trong ngăn xếp MEAN) dẫn đầu nhóm. Tuy nhiên, khi bắt đầu một dự án Node mới, bạn không nên chỉ chấp nhận Mongo làm lựa chọn mặc định. Thay vào đó, loại cơ sở dữ liệu bạn chọn sẽ phụ thuộc vào yêu cầu của dự án của bạn. Ví dụ: nếu bạn cần tạo bảng động hoặc chèn thời gian thực, thì giải pháp NoQuery là cách tốt nhất. Mặt khác, nếu dự án của bạn xử lý các truy vấn và giao dịch phức tạp, thì cơ sở dữ liệu SQL sẽ có ý nghĩa hơn nhiều

Trong hướng dẫn này, chúng ta sẽ xem xét cách bắt đầu với mô-đun mysql — một Node. js cho MySQL, được viết bằng JavaScript. Tôi sẽ giải thích cách sử dụng mô-đun để kết nối với cơ sở dữ liệu MySQL và thực hiện các thao tác CRUD thông thường, trước khi xem các thủ tục được lưu trữ và thoát đầu vào của người dùng

Bài viết phổ biến này đã được cập nhật vào năm 2020 để phản ánh các phương pháp hiện tại để sử dụng MySQL với Node. js. Để biết thêm về MySQL, hãy đọc Khởi động MySQL

Bắt đầu nhanh. Cách sử dụng MySQL trong nút

Nếu bạn đến đây để tìm kiếm một cách nhanh chóng để bắt đầu và chạy với MySQL trong Node, thì chúng tôi đã hỗ trợ bạn

Dưới đây là cách sử dụng MySQL trong Node theo năm bước đơn giản

  1. Tạo một dự án mới.
    const mysql = require('mysql');
    
    // First you need to create a connection to the database
    // Be sure to replace 'user' and 'password' with the correct values
    const con = mysql.createConnection({
      host: 'localhost',
      user: 'user',
      password: 'password',
    });
    
    con.connect((err) => {
      if(err){
        console.log('Error connecting to Db');
        return;
      }
      console.log('Connection established');
    });
    
    con.end((err) => {
      // The connection is terminated gracefully
      // Ensures all remaining queries are executed
      // Then sends a quit packet to the MySQL server.
    });
    
    2
  2. Tạo một tệp
    const mysql = require('mysql');
    
    // First you need to create a connection to the database
    // Be sure to replace 'user' and 'password' with the correct values
    const con = mysql.createConnection({
      host: 'localhost',
      user: 'user',
      password: 'password',
    });
    
    con.connect((err) => {
      if(err){
        console.log('Error connecting to Db');
        return;
      }
      console.log('Connection established');
    });
    
    con.end((err) => {
      // The connection is terminated gracefully
      // Ensures all remaining queries are executed
      // Then sends a quit packet to the MySQL server.
    });
    
    3.
    const mysql = require('mysql');
    
    // First you need to create a connection to the database
    // Be sure to replace 'user' and 'password' with the correct values
    const con = mysql.createConnection({
      host: 'localhost',
      user: 'user',
      password: 'password',
    });
    
    con.connect((err) => {
      if(err){
        console.log('Error connecting to Db');
        return;
      }
      console.log('Connection established');
    });
    
    con.end((err) => {
      // The connection is terminated gracefully
      // Ensures all remaining queries are executed
      // Then sends a quit packet to the MySQL server.
    });
    
    4
  3. Cài đặt mô-đun mysql.
    const mysql = require('mysql');
    
    // First you need to create a connection to the database
    // Be sure to replace 'user' and 'password' with the correct values
    const con = mysql.createConnection({
      host: 'localhost',
      user: 'user',
      password: 'password',
    });
    
    con.connect((err) => {
      if(err){
        console.log('Error connecting to Db');
        return;
      }
      console.log('Connection established');
    });
    
    con.end((err) => {
      // The connection is terminated gracefully
      // Ensures all remaining queries are executed
      // Then sends a quit packet to the MySQL server.
    });
    
    5
  4. Tạo một tệp
    const mysql = require('mysql');
    
    // First you need to create a connection to the database
    // Be sure to replace 'user' and 'password' with the correct values
    const con = mysql.createConnection({
      host: 'localhost',
      user: 'user',
      password: 'password',
    });
    
    con.connect((err) => {
      if(err){
        console.log('Error connecting to Db');
        return;
      }
      console.log('Connection established');
    });
    
    con.end((err) => {
      // The connection is terminated gracefully
      // Ensures all remaining queries are executed
      // Then sends a quit packet to the MySQL server.
    });
    
    6 và sao chép đoạn trích bên dưới (chỉnh sửa các trình giữ chỗ cho phù hợp)
  5. Chạy tập tin.
    const mysql = require('mysql');
    
    // First you need to create a connection to the database
    // Be sure to replace 'user' and 'password' with the correct values
    const con = mysql.createConnection({
      host: 'localhost',
      user: 'user',
      password: 'password',
    });
    
    con.connect((err) => {
      if(err){
        console.log('Error connecting to Db');
        return;
      }
      console.log('Connection established');
    });
    
    con.end((err) => {
      // The connection is terminated gracefully
      // Ensures all remaining queries are executed
      // Then sends a quit packet to the MySQL server.
    });
    
    7. Quan sát “Đã kết nối. " thông điệp
const mysql = require('mysql');
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
  database: 'database name'
});
connection.connect((err) => {
  if (err) throw err;
  console.log('Connected!');
});

Cài đặt Mô-đun mysql

Bây giờ chúng ta hãy xem xét kỹ hơn từng bước đó

mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql

Trước hết, chúng tôi đang sử dụng dòng lệnh để tạo một thư mục mới và điều hướng đến nó. Sau đó, chúng tôi đang tạo tệp

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
3 bằng lệnh
const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
4. Cờ
const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
00 có nghĩa là npm sẽ sử dụng giá trị mặc định mà không trải qua quá trình tương tác

Bước này cũng giả định rằng bạn đã cài đặt Node và npm trên hệ thống của mình. Nếu đây không phải là trường hợp, hãy xem bài viết SitePoint này để tìm hiểu cách thực hiện điều đó. Cài đặt nhiều phiên bản nút. js sử dụng nvm

Sau đó, chúng tôi đang cài đặt mô-đun mysql từ npm và lưu nó dưới dạng phần phụ thuộc của dự án. Các phần phụ thuộc của dự án (trái ngược với các phần phụ thuộc của nhà phát triển) là những gói cần thiết để ứng dụng chạy. Bạn có thể đọc thêm về sự khác biệt giữa hai ở đây

Nếu bạn cần trợ giúp thêm về cách sử dụng npm, hãy nhớ xem hướng dẫn này hoặc hỏi trong diễn đàn của chúng tôi

Bắt đầu

Trước khi chúng tôi bắt đầu kết nối với cơ sở dữ liệu, điều quan trọng là bạn đã cài đặt và định cấu hình MySQL trên máy của mình. Nếu đây không phải là trường hợp, vui lòng tham khảo hướng dẫn cài đặt trên trang chủ của họ

Việc tiếp theo chúng ta cần làm là tạo cơ sở dữ liệu và bảng cơ sở dữ liệu để làm việc với. Bạn có thể thực hiện việc này bằng
giao diện đồ họa, chẳng hạn như Adminer hoặc sử dụng dòng lệnh. Đối với bài viết này, tôi sẽ sử dụng cơ sở dữ liệu có tên là

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
01 và một bảng có tên là
const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
02. Đây là kết xuất cơ sở dữ liệu để bạn có thể thiết lập và chạy nhanh nếu muốn theo dõi.

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
3

Mysql NodeJS

Kết nối với cơ sở dữ liệu

Bây giờ, hãy tạo một tệp có tên

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
6 trong thư mục
const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
04 của chúng tôi và xem cách kết nối với MySQL từ Node. js

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});

Bây giờ hãy mở một thiết bị đầu cuối và nhập

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
7. Khi kết nối được thiết lập thành công, bạn sẽ có thể thấy thông báo “Đã thiết lập kết nối” trong bảng điều khiển. Nếu xảy ra sự cố (ví dụ: bạn nhập sai mật khẩu), một cuộc gọi lại sẽ được kích hoạt, cuộc gọi lại này được chuyển qua một phiên bản của đối tượng Lỗi JavaScript (_______106). Hãy thử ghi nhật ký này vào bảng điều khiển để xem nó chứa thông tin hữu ích bổ sung nào

Sử dụng gật đầu để xem các tệp để thay đổi

Chạy

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
7 bằng tay mỗi khi chúng tôi thực hiện thay đổi đối với mã của mình sẽ hơi tẻ nhạt, vì vậy hãy tự động hóa việc đó. Phần này không cần thiết phải làm theo cùng với phần còn lại của hướng dẫn, nhưng chắc chắn sẽ giúp bạn tiết kiệm một số thao tác gõ phím

Hãy bắt đầu bằng cách cài đặt gói gật đầu. Đây là công cụ tự động khởi động lại ứng dụng Node khi phát hiện thay đổi tệp trong thư mục

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
0

Bây giờ hãy chạy

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
08 và thay đổi thành
const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
6. gật đầu sẽ phát hiện thay đổi và khởi động lại ứng dụng

Ghi chú. chúng tôi đang chạy gật đầu trực tiếp từ thư mục

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
30. Bạn cũng có thể cài đặt nó trên toàn cầu hoặc tạo tập lệnh npm để khởi động nó

Thực hiện truy vấn

Đọc

Bây giờ bạn đã biết cách thiết lập kết nối tới cơ sở dữ liệu MySQL từ Node. js, hãy xem cách thực thi các truy vấn SQL. Chúng tôi sẽ bắt đầu bằng cách chỉ định tên cơ sở dữ liệu (

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
01) trong lệnh
const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
32

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
3

Sau khi kết nối được thiết lập, chúng tôi sẽ sử dụng biến

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
33 để thực hiện truy vấn đối với bảng cơ sở dữ liệu
const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
02

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
9

Khi bạn chạy

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
6 (bằng cách sử dụng gật đầu hoặc bằng cách nhập
const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
7 vào thiết bị đầu cuối của bạn), bạn sẽ có thể xem dữ liệu được trả về từ cơ sở dữ liệu được ghi vào thiết bị đầu cuối

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
2

Dữ liệu được trả về từ cơ sở dữ liệu MySQL có thể được phân tích cú pháp đơn giản bằng cách lặp qua đối tượng

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
37

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
4

Điều này mang lại cho bạn những điều sau đây

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
5

Tạo

Bạn có thể thực hiện truy vấn chèn đối với cơ sở dữ liệu, như vậy

mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql
0

Lưu ý cách chúng tôi có thể lấy ID của bản ghi được chèn bằng tham số gọi lại

Đang cập nhật

Tương tự, khi thực hiện truy vấn cập nhật, số hàng bị ảnh hưởng có thể được truy xuất bằng cách sử dụng

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
38

mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql
1

phá hoại

Điều tương tự cũng xảy ra với truy vấn xóa

mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql
2

Sử dụng nâng cao

Tôi muốn kết thúc bằng cách xem mô-đun mysql xử lý các thủ tục được lưu trữ và thoát đầu vào của người dùng như thế nào

thủ tục lưu trữ

Nói một cách đơn giản, thủ tục lưu sẵn là mã SQL được chuẩn bị sẵn mà bạn có thể lưu vào cơ sở dữ liệu để có thể dễ dàng sử dụng lại. Nếu bạn cần xem lại các thủ tục được lưu trữ, hãy xem hướng dẫn này

Hãy tạo một thủ tục được lưu trữ cho cơ sở dữ liệu

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
01 của chúng tôi để tìm nạp tất cả các chi tiết về tác giả. Chúng tôi sẽ gọi nó là
const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
90. Để làm điều này, bạn sẽ cần một số loại giao diện cho cơ sở dữ liệu. Tôi đang sử dụng Adminer. Chạy truy vấn sau đối với cơ sở dữ liệu
const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
01, đảm bảo rằng người dùng của bạn có quyền quản trị trên máy chủ MySQL

mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql
3

Điều này sẽ tạo và lưu trữ thủ tục trong cơ sở dữ liệu

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
92 trong bảng
const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
93

Mysql NodeJS

Ghi chú. nếu cú ​​pháp dấu phân cách có vẻ lạ đối với bạn, nó sẽ được giải thích tại đây

Tiếp theo, thiết lập kết nối và sử dụng đối tượng kết nối để gọi thủ tục được lưu trữ như được hiển thị

mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql
4

Lưu các thay đổi và chạy tệp. Khi nó được thực thi, bạn sẽ có thể xem dữ liệu được trả về từ cơ sở dữ liệu

mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql
5

Cùng với dữ liệu, nó trả về một số thông tin bổ sung, chẳng hạn như số hàng bị ảnh hưởng,

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
94, v.v. Bạn cần lặp lại chỉ mục thứ 0 của dữ liệu được trả về để tách chi tiết nhân viên khỏi phần còn lại của thông tin

mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql
6

Điều này mang lại cho bạn những điều sau đây

mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql
7

Bây giờ, hãy xem xét một thủ tục được lưu trữ yêu cầu tham số đầu vào

mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql
8

Chúng ta có thể truyền tham số đầu vào trong khi thực hiện cuộc gọi đến thủ tục được lưu trữ

mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql
9

Điều này mang lại cho bạn những điều sau đây

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
30

Hầu hết khi chúng tôi cố gắng chèn một bản ghi vào cơ sở dữ liệu, chúng tôi cần ID được chèn lần cuối được trả về dưới dạng tham số out. Xem xét thủ tục lưu trữ chèn sau đây với tham số out

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
31

Để thực hiện lời gọi thủ tục với tham số out, trước tiên chúng ta cần kích hoạt nhiều lời gọi trong khi tạo kết nối. Vì vậy, hãy sửa đổi kết nối bằng cách đặt thực thi nhiều câu lệnh thành

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
95

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
32

Tiếp theo, khi gọi thủ tục, hãy đặt tham số out và truyền vào

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
33

Như đã thấy trong đoạn mã trên, chúng tôi đã đặt một tham số

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
96 out và chuyển nó trong khi thực hiện cuộc gọi đến thủ tục được lưu trữ. Khi cuộc gọi đã được thực hiện, chúng ta cần chọn tham số out để truy cập ID được trả về

Chạy

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
6. Khi thực thi thành công, bạn sẽ có thể thấy tham số đã chọn cùng với nhiều thông tin khác.
const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
98 sẽ cấp cho bạn quyền truy cập vào tham số đã chọn

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
34

Ghi chú. Để xóa một thủ tục đã lưu trữ, bạn cần chạy lệnh

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
99 đối với cơ sở dữ liệu mà bạn đã tạo cho nó

Thoát đầu vào của người dùng

Để tránh các cuộc tấn công SQL Injection, bạn phải luôn thoát bất kỳ dữ liệu nào bạn nhận được từ người dùng trước khi sử dụng nó trong một truy vấn SQL. Hãy chứng minh tại sao

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
35

Điều này dường như đủ vô hại và thậm chí trả về kết quả chính xác

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
36

Tuy nhiên, hãy thử thay đổi

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
20 thành

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
37

Chúng tôi đột nhiên có quyền truy cập vào toàn bộ tập dữ liệu. Bây giờ thay đổi nó thành cái này

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
38

Bây giờ chúng ta đang gặp rắc rối

Tin tốt là sự giúp đỡ luôn sẵn sàng. Bạn chỉ cần sử dụng mysql. phương pháp thoát hiểm

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
39

Bạn cũng có thể sử dụng một trình giữ chỗ có dấu chấm hỏi, như chúng tôi đã làm trong các ví dụ ở đầu bài viết

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
0

Tại sao không chỉ sử dụng ORM?

Trước khi chúng ta đi vào những ưu và nhược điểm của phương pháp này, hãy dành một chút thời gian để xem ORM là gì. Phần sau được lấy từ câu trả lời trên Stack Overflow

Object-Relational Mapping (ORM) là một kỹ thuật cho phép bạn truy vấn và thao tác dữ liệu từ cơ sở dữ liệu bằng cách sử dụng mô hình hướng đối tượng. Khi nói về ORM, hầu hết mọi người đang đề cập đến một thư viện triển khai kỹ thuật Ánh xạ quan hệ đối tượng, do đó có cụm từ “một ORM”

Vì vậy, điều này có nghĩa là bạn viết logic cơ sở dữ liệu của mình bằng ngôn ngữ dành riêng cho miền của ORM, trái ngược với cách tiếp cận thông thường mà chúng tôi đã thực hiện cho đến nay. Để cung cấp cho bạn ý tưởng về điều này có thể trông như thế nào, đây là một ví dụ sử dụng Sequelize, truy vấn cơ sở dữ liệu cho tất cả các tác giả và ghi chúng vào bảng điều khiển

const mysql = require('mysql');

// First you need to create a connection to the database
// Be sure to replace 'user' and 'password' with the correct values
const con = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  password: 'password',
});

con.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end((err) => {
  // The connection is terminated gracefully
  // Ensures all remaining queries are executed
  // Then sends a quit packet to the MySQL server.
});
1

Việc sử dụng ORM có ý nghĩa với bạn hay không sẽ phụ thuộc rất nhiều vào những gì bạn đang làm và với ai. Một mặt, ORMS có xu hướng làm cho các nhà phát triển làm việc hiệu quả hơn, một phần bằng cách trừu tượng hóa một phần lớn SQL để không phải tất cả mọi người trong nhóm đều cần biết cách viết các truy vấn cụ thể cho cơ sở dữ liệu siêu hiệu quả. Nó cũng dễ dàng chuyển sang phần mềm cơ sở dữ liệu khác, bởi vì bạn đang phát triển theo cách trừu tượng

Tuy nhiên, mặt khác, có thể viết một số SQL thực sự lộn xộn và không hiệu quả do không hiểu cách ORM thực hiện những gì nó làm. Hiệu suất cũng là một vấn đề ở chỗ việc tối ưu hóa các truy vấn không phải thông qua ORM sẽ dễ dàng hơn nhiều

Dù bạn đi theo con đường nào là tùy thuộc vào bạn, nhưng nếu đây là quyết định mà bạn đang trong quá trình đưa ra, hãy xem chủ đề Stack Overflow này. Tại sao bạn nên sử dụng ORM?. Ngoài ra, hãy xem bài đăng này trên SitePoint. 3 ORM JavaScript có thể bạn chưa biết

Sự kết luận

Trong hướng dẫn này, chúng tôi đã cài đặt ứng dụng khách mysql cho Node. js và cấu hình nó để kết nối với cơ sở dữ liệu. Chúng ta cũng đã thấy cách thực hiện các thao tác CRUD, làm việc với các câu lệnh đã chuẩn bị sẵn và thoát khỏi đầu vào của người dùng để giảm thiểu các cuộc tấn công SQL injection. Chưa hết, chúng tôi mới chỉ vạch ra bề nổi của những gì ứng dụng khách mysql cung cấp. Để biết thêm thông tin chi tiết, tôi khuyên bạn nên đọc tài liệu chính thức

Và xin lưu ý rằng mô-đun mysql không phải là chương trình duy nhất trong thị trấn. Ngoài ra còn có các tùy chọn khác, chẳng hạn như nút phổ biến-mysql2

MySQL có thể được sử dụng với nút js 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.

MySQL NodeJ là gì?

Nút. js và MySQL là một số ràng buộc cần thiết cho mọi ứng dụng web . MySQL là một trong những cơ sở dữ liệu nguồn mở phổ biến nhất trên thế giới và cũng hiệu quả. Hầu như mọi ngôn ngữ lập trình phổ biến như Java và PHP đều cung cấp trình điều khiển để truy cập và thực hiện các thao tác với MySQL.

NodeJs có tốt cho cơ sở dữ liệu không?

Nút. js hỗ trợ tất cả các loại cơ sở dữ liệu bất kể đó là cơ sở dữ liệu quan hệ hay cơ sở dữ liệu NoSQL . Tuy nhiên, cơ sở dữ liệu NoSQL như MongoDb phù hợp nhất với Node. js.

Cái nào tốt hơn với nút js MySQL hoặc MongoDB?

Nếu bạn có dữ liệu quan hệ thì MySQL là lựa chọn tốt hơn MongoDB . Đối với những thứ đơn giản, MongoDB vẫn ổn, nhưng khi cố gắng liên kết nhiều dữ liệu quan hệ lại với nhau thì MongoDB là một cơn ác mộng.