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
- Tạo một dự án mới.
2const 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. }];
- Tạo một tệp
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. }];
4const 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. }];
- Cài đặt mô-đun mysql.
5const 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. }];
- Tạo một tệp
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]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. }];
- Chạy tập tin.
7. Quan sát “Đã kết nối. " thông điệpconst 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. }];
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ácBướ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.
}];
3Kế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. jsconst 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àoSử 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ímHã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.
}];
0Bâ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ụngGhi 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.
}];
32const 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.
}];
3Sau 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.
}];
02const 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.
}];
9Khi 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ốiconst 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.
}];
2Dữ 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.
}];
37const 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.
}];
5Tạ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
0Lư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.
}];
38mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql
1phá 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
2Sử 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ủ MySQLmkdir 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.
}];
93Ghi 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
4Lư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
5Cù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 tinmkdir 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
7Bâ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
8Chú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.
}];
30Hầ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.
}];
95const 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.
}];
32Tiế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.
}];
33Như đã 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ọnconst 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.
}];
34Ghi 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.
}];
36Tuy 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ànhconst 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.
}];
37Chú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.
}];
38Bâ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.
}];
39Bạ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.
}];
0Tạ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.
}];
1Việ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 đến mức 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