Làm cách nào để tìm tập lệnh cơ sở dữ liệu mysql?
Bài viết này giải thích một số khái niệm chuyên sâu, chẳng hạn như tập lệnh và kiểu dữ liệu. Đọc "MySQL cho người mới bắt đầu" để biết thông tin cơ bản Show Tạo và chạy tập lệnhTôi sẽ bắt đầu bằng cách mô tả cú pháp của một tập lệnh MySQL, vì các tập lệnh sẽ được sử dụng cho tất cả các ví dụ trong hướng dẫn này Thay vì đưa ra từng câu lệnh SQL từ một máy khách mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......1 một cách tương tác, việc giữ các câu lệnh trong một tập lệnh thường sẽ thuận tiện hơn. Sau đó, bạn có thể chạy toàn bộ tập lệnh hoặc sao chép và dán các câu lệnh đã chọn để chạy Sử dụng trình soạn thảo văn bản lập trình để tạo tập lệnh sau và lưu dưới dạng " mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......2" trong thư mục đã chọn (e. g. , " mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......3"). Bạn nên sử dụng " mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......4" làm phần mở rộng tệp Tôi khuyên dùng NetBeans cung cấp hỗ trợ trực tiếp cho cơ sở dữ liệu MySQL (đọc) hoặc NotePad++ (@ http. // notepad-plus. nguồn. net/uk/trang web. htm), nhận dạng tệp " mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......4" dưới dạng tập lệnh SQL với cú pháp tô sáng Bạn có thể chạy tập lệnh bằng ứng dụng khách mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......1 theo hai cách. chế độ hàng loạt hoặc sử dụng lệnh mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......7 Chạy tập lệnh ở chế độ hàng loạtĐể chạy tập lệnh ở chế độ hàng loạt (không tương tác), hãy khởi động ứng dụng khách mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......1 và chuyển hướng tập lệnh làm đầu vào, như sau > mysql -u username -p < path-to\scriptName.sql The input redirection operator mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......9 re-directs the input from the file, instead of the default standard input (i. e. , keyboard). You may provide an absolute or relative path of the filename. You may need to double quote the filename if it contains special characters such as blank (strongly discouraged. ) For example, we invoke the mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......1 client with user " CREATE DATABASE databaseName DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;1" in batch mode running the script " mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......2" created earlier. I assume that CREATE DATABASE databaseName DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;1 is authorized to access mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......1 database The output contains the column headers and the rows selected. The column values are separated by 'tab'. This is to facilitate direct processing by another program. This format is known as TSV (Tab-Separated Values), similar to CSV (Comma-Separated Values) You could also redirect the output to a text file (via the output redirection operator CREATE DATABASE databaseName DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;5), for example, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt Để có đầu ra "giống như bảng", hãy sử dụng tùy chọn CREATE DATABASE databaseName DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;6 (bảng), ví dụ: > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ Bạn có thể lặp lại các lệnh đầu vào thông qua tùy chọn CREATE DATABASE databaseName DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;7 (dài dòng), ví dụ: shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye Trong chế độ hàng loạt, bạn cũng có thể thực hiện (các) câu lệnh trực tiếp thông qua tùy chọn (đánh giá) CREATE DATABASE databaseName DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;8. Ví dụ, > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye Chạy tập lệnh thông qua lệnh SOURCETrong phiên khách hàng mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......1 tương tác, bạn có thể sử dụng lệnh mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......7 (hoặc lệnh tốc ký > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt01) để chạy tập lệnh. Ví dụ, ghi chú
Cú pháp ngôn ngữ kịch bản MySQLBình luậnChú thích nhiều dòng bắt đầu bằng > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt07 và kết thúc bằng > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt08 (giống như ngôn ngữ C/C++/Java). Nhận xét cuối dòng bắt đầu bằng > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt09 (hai dấu gạch ngang và dấu cách) hoặc #, kéo dài đến cuối dòng hiện tại Nhận xét bị công cụ xử lý bỏ qua nhưng rất quan trọng để cung cấp giải thích và tài liệu cho tập lệnh. Tôi thực sự khuyến khích bạn sử dụng bình luận một cách tự do Mã cụ thể của MySQLCác câu lệnh kèm theo trong > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt10 được gọi là mã cụ thể của MySQL. Chúng được công cụ MySQL nhận ra, nhưng bị các công cụ cơ sở dữ liệu khác bỏ qua. Nói cách khác, chúng sẽ được xử lý bởi MySQL nhưng được coi là nhận xét của các cơ sở dữ liệu khác Bạn có thể bao gồm một số phiên bản MySQL tùy chọn, e. g. , > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt11. Các câu lệnh sẽ được xử lý bởi MySQL nếu phiên bản của máy chủ ít nhất là phiên bản được chỉ định, e. g. , phiên bản 4. 00. 14 Bạn có thể tìm thấy phiên bản máy chủ MySQL thông qua lệnh > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt12 Các mã cụ thể của MySQL (với số phiên bản) thường được tạo khi bạn xuất cơ sở dữ liệu qua tiện ích > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt13. Ví dụ, /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ...... Định danh và BackquotesCác định danh (chẳng hạn như tên cơ sở dữ liệu, tên bảng và tên cột) phải được trích dẫn lại nếu chúng chứa khoảng trống và ký tự đặc biệt; . g. , > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt14, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt15, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt16 (từ dành riêng), > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt17 (chứa khoảng trống) Đó là một cách thực hành tốt để trích dẫn lại tất cả các mã định danh trong tập lệnh để phân biệt tên với các từ dành riêng (có thể trong các phiên bản MySQL trong tương lai) Phân biệt chữ hoa chữ thườngTừ khóa MySQL không phân biệt chữ hoa chữ thường. Để rõ ràng, tôi thường hiển thị các từ khóa bằng chữ in hoa (e. g. , > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt18, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt19) Các mã định danh (chẳng hạn như tên cơ sở dữ liệu, tên bảng và tên cột) phân biệt chữ hoa chữ thường trong một số nền tảng; . g. , Nói chung, mã định danh phân biệt chữ hoa chữ thường trong Unix nhưng không phân biệt chữ hoa chữ thường trong Windows). Do đó, nên coi các mã định danh là phân biệt chữ hoa chữ thường trong tập lệnh So sánh và sắp xếp chuỗi phụ thuộc vào trình tự đối chiếu ký tự được sử dụng. Theo mặc định, so sánh chuỗi trong MySQL (Windows) không phân biệt chữ hoa chữ thường. (Cần kiểm tra thêm trên Unixes và Macs. ) chữChuỗi ký tự. Một chuỗi ký tự (hoặc giá trị chuỗi) được bao quanh bởi một cặp dấu nháy đơn (e. g. , > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt20) (khuyên dùng); . g. , > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt21) Một số ký tự có thể tạo ra sự mơ hồ khi được đặt bên trong một chuỗi trích dẫn đơn hoặc trích dẫn kép, e. g. , bạn không thể bao gồm một trích dẫn trong một chuỗi trích dẫn đơn. Một số ký tự, chẳng hạn như tab, dòng mới, không thể in được và yêu cầu một ký hiệu đặc biệt được đưa vào một sting. Dấu gạch chéo ngược > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt03 được biết đến như một ký tự thoát, giúp sửa đổi ý nghĩa của ký tự theo sau, như được lập bảng dưới đâyTrình tự thoát Ý nghĩaTại sao_______16_______23Trích dẫn duy nhất > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt24Để giải quyết sự mơ hồ > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt24 và > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt26 dùng để bao chuỗi, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt27 để trốn thoát, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt28 and > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt29 for pattern matching > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt30Double quote > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt26 > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt32Back slash > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt27 > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt34Percent > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt28 > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt36Underscore > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt29 > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt38Newline or LineFeed (FF) ( > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt39)For non-printable symbols > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt40Carriage Return (CR) ( > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt41) > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt42Back Space ( > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt43) > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt44Tab ( > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt45) > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt46ASCII NUL ( > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt47) > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt48Control-Z ( Một lối thoát theo sau bởi bất kỳ ký tự nào khác được liệt kê ở trên được coi là ký tự, e. g. , > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt50 là > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt51. Trình tự thoát phân biệt chữ hoa chữ thường, i. e. , > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt52 là tab, nhưng > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt53 là > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt54. Chuỗi có thể được trích dẫn đơn hoặc trích dẫn kép để mang đến cho bạn sự linh hoạt trong việc bao gồm các trích dẫn trong chuỗi mà không cần sử dụng trình tự thoát, ví dụ:. g. , > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt55, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt56 Chữ Hex. Giá trị hex được viết là > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt57 hoặc > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt58 hoặc > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt59, e. g. , > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt60, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt61, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt62, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt63. Bạn có thể lấy giá trị hex của một chuỗi bằng hàm > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt64. Ví dụ, chữ bit. Tương tự, một chút nghĩa đen được viết là > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt65 hoặc > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt66, e. g. , > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt67, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt68 BiếnCó nhiều loại biến trong MySQL. Biến hệ thống (toàn hệ thống), biến do người dùng xác định (trong kết nối) và biến cục bộ (trong hàm/thủ tục được lưu trữ) Biến do người dùng xác định. Biến do người dùng định nghĩa bắt đầu bằng ký hiệu > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt69, e. g. , > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt70, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt71. Biến do người dùng xác định dành riêng cho kết nối và có sẵn trong kết nối. A variable defined in one client session is not visible by another client session. Bạn có thể sử dụng một biến do người dùng định nghĩa để chuyển một giá trị giữa các câu lệnh SQL trong cùng một kết nối Trong MySQL, bạn có thể xác định biến người dùng thông qua
Ví dụ như, Giống như tất cả các ngôn ngữ kịch bản, ngôn ngữ kịch bản SQL là kiểu lỏng lẻo. Bạn không cần phải khai báo rõ ràng kiểu biến mà chỉ cần gán giá trị Biến hệ thống. Máy chủ MySQL duy trì các biến hệ thống, được nhóm thành hai loại. toàn cầu và phiên. Các biến toàn cục ảnh hưởng đến hoạt động chung của máy chủ. Các biến phiên ảnh hưởng đến các kết nối máy khách cá nhân. Một biến hệ thống có thể có cả giá trị toàn cục và giá trị phiên Các biến toàn cục được tham chiếu qua > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt76 hoặc > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt77. Các biến phiên được tham chiếu qua > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt78, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt79 hoặc đơn giản là > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt80 Bạn có thể sử dụng câu lệnh > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt81 để thay đổi giá trị của một biến. Ví dụ, Sử dụng > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt82 để hiển thị giá trị của các biến. Bạn có thể sử dụng mệnh đề khớp mẫu > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt83 để giới hạn kết quả đầu ra. Ví dụ, Biến cục bộ (trong Chương trình được lưu trữ). You could define local variables for stored programs (such as function and procedure). Phạm vi của biến cục bộ nằm trong chương trình. Bạn cần sử dụng câu lệnh > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt84 để khai báo một biến cục bộ. Local variable will be discussed later Các chức năng tích hợp của MySQLĐể biết chi tiết về các chức năng tích hợp sẵn của MySQL, hãy tham khảo hướng dẫn sử dụng MySQL "Hàm và toán tử" @ http. // nhà phát triển. mysql. com/doc/refman/5. 5/en//chức năng. html Hàm chuỗi MySQLThẩm quyền giải quyết. Hàm chuỗi @ http. // nhà phát triển. mysql. com/doc/refman/5. 5/en/string-functions. html
MySQL GROUP BY Aggregate FunctionsReference. MySQL " > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt87 (Aggregate) Functions" @ https. //dev. mysql. com/doc/refman/5. 5/en/group-by-functions. html We can apply > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt87 aggregate functions to each group of rows
MySQL Date/Time Functions[TODO] MySQL Mathematical Functions
Quy ước đặt tênquy ước đặt tên ưa thích của tôi là như sau
[TODO] Nối chữ thường hay chữ thường với dấu gạch dưới? Các kiểu dữ liệu MySQLLà một lập trình viên, việc hiểu các kiểu dữ liệu là điều cần thiết để hiểu hoạt động của hệ thống cơ sở dữ liệu cơ bản. (Đọc "Hướng dẫn về Biểu diễn Dữ liệu - Số nguyên, Số dấu phẩy động và Bộ ký tự". ) MySQL hỗ trợ nhiều loại dữ liệu, được nhóm thành 3 loại
Số - Số nguyênSố nguyên, theo mặc định, là số nguyên có dấu; Bạn có thể đặt chiều rộng hiển thị, sử dụng cú pháp > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+65, trong đó > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+66 là chiều rộng trường hiển thị tối đa 255, e. g. , > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+67. Bạn cũng có thể chỉ định > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+68 để đệm các số được hiển thị bằng các số 0 ở đầu (chỉ dành cho > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+64) thay vì để trống. Độ rộng trường chỉ ảnh hưởng đến màn hình chứ không ảnh hưởng đến số được lưu trữ MySQL hỗ trợ nhiều loại số nguyên với độ chính xác và phạm vi khác nhau
Chọn đúng loại số nguyên rất quan trọng để tối ưu hóa việc sử dụng bộ nhớ và hiệu quả tính toán. Ví dụ: đối với cột số nguyên có phạm vi từ 1 đến 9999, hãy sử dụng > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+86. đối với 1 đến 9999999, hãy sử dụng > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+87 (đủ cho một doanh nghiệp nhỏ, nhưng luôn dành nhiều chỗ hơn cho việc mở rộng trong tương lai) Số nguyên (và số dấu phẩy động sẽ được thảo luận sau) có thể được khai báo là > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+88, với giá trị bắt đầu mặc định là 1. Khi bạn chèn một > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt89 (được khuyến nghị) (hoặc 0 hoặc một giá trị bị thiếu) vào cột > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+88, giá trị tối đa của cột đó cộng với 1 sẽ được chèn vào. Bạn cũng có thể chèn bất kỳ giá trị hợp lệ nào vào cột > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+88, bỏ qua phần tăng tự động. Hãy lưu ý rằng > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+92 tiếp theo sẽ không thành công nếu > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+92 cuối cùng đã đạt đến giá trị tối đa. Do đó, nên sử dụng > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+94 cho cột > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+88 để tránh xử lý chạy quá mức. Chỉ có thể có một cột > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+88 trong một bảng và cột đó phải được xác định là khóa Ví dụ (Kiểm tra kiểu dữ liệu số nguyên). Đọc "_______18_______97" Ví dụ (Thử nghiệm AUTO_INCREMENT). autoincrement_arena. sql Số - Số dấu phẩy động & dấu phẩy độngMySQL hỗ trợ cả điểm động xấp xỉ (_______18_______98 và > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+99) và điểm cố định chính xác ( shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye00)
Các số dấu phẩy động và dấu phẩy động cũng có thể được khai báo là > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+64 để loại trừ các số âm Ví dụ (Kiểm tra kiểu dữ liệu dấu phẩy động). Read " shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye27" Character Sets and CollationsStrings are made up of a sequence of characters. Before presenting the various string data types, it is important to understand the so-called character set and collating sequence (or collation) Character SetsA character set (also called charset, character encoding, character map, code page) defines a set of character and maps each character to a unique numeric code. For example, in ASCII, character shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye28 is assigned 65, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye29 is 97, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye30 is 48 and space is 32. The most commonly-used character sets are the 8-bit Latin-1 (ISO/IEC 8859-1) for English characters (which is backward compatible with 7-bit ASCII), and multi-byte Unicode (ISO/IEC 10646) for internationalization. Unicode can be encoded in variable-length UTF8 (1-3 bytes) for fixed-length UCS2 (2 bytes) For more details on character sets, read "A Tutorial on Data Representation - Integers, Floating-point Numbers, and Character Sets", section "" MySQL's UCS-2 is in big-endian without BOM (Byte Order Mark), i. e. , UCS-2BE or UTF2BE. MySQL uses no BOM for UTF-8 Collating SequencesA collating sequence (or collation) refers to the orders in which individual characters should be ranked in comparing or sorting the strings. In other words, a collation is a set of rules for ranking characters in a character set A string may contain uppercase and lowercase characters, digits and symbols. As an example, suppose we wish to sort three strings. shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye31, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye32, and shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye33 encoded in ASCII. If we rank the characters according to the underlying ASCII code numbers, the order would be shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye32, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye31 and shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye36 It is because uppercase letters have smaller code numbers than lowercase letters in ASCII code. Ranking strings according to their underlying code numbers is known as binary collation. Binary collation does not agree with the so-called dictionary order, where the same uppercase and lowercase letters have the same rank There are many other collating sequences available. Some case-insensitive ( shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye37) dictionary-order collating sequences have the same rank for the same uppercase and lowercase letter, i. e. , shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye28, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye29 ⇒ shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye40, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye41 ⇒ . ⇒ shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye42, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye43. Some case-sensitive ( shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye44) dictionary-order collating sequences put the uppercase letter before its lowercase counterpart, i. e. , shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye28 ⇒ shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye29 ⇒ shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye47 shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye41 ⇒ . ⇒ shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye42 ⇒ shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye43. Typically, space is ranked before digits shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye30 to shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye52, followed by the alphabets Collating sequence is also language dependent, as different languages use different sets of characters, e. g. , a, A, α, Ä, á, é. It becomes more complicated if more than one languages are used, e. g. , you need to decide how to rank 'a' and 'α', by choosing an appropriate collating sequence MySQL Character Sets and CollationsMySQL 5. 5 supports 39 character sets with 197 collations You could use command shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye53 to display all the supported character sets and their default collation; and shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye54 to display all the collations and the charsets their support mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec) The default character set in MySQL is shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye55 (aka ISO-8859-1). The default collation for shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye55 is shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye57, which is a case-insensitive collation suitable for Swedish/finnish (MySQL was created by some Swedishs). You can list the available collations for shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye55, as follows mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+ shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye59 is a case-insensitive collation for multilingual western european. shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye60 is case-sensitive. shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye61 is the binary collation according to shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye55 encoding I recommend that you use shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye63 charset for applications that require internationalization (i18n) support (because shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye63 supports all the languages in this world). The default collation for shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye63 is shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye66. Other collations for shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye63 includes mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ...... You may want to try out some of these collations by ranking space, numbers, uppercase and lowercase letters, and some typical strings Specifying Character Set and Collation in MySQLYou could specify the character set and collation via keyword shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye68 (or shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye69) and shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye70. They could be set at 4 levels. server, database, table, and column To set the charset and collation for the server, start shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye71 with shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye72 and shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye73 options. The default charset is shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye55 You can set the default charset and collation for an database in shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye75. For example, CREATE DATABASE databaseName DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; You can set the character set and collation for table and column in > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt18. For example, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt0 In the above example, the default character set for all the string columns for the table is shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye63 (for unicode support) with collating sequence of shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye78 (default). However, column shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye79 uses character set shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye55 with collation shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye60; column shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye82 uses character set shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye83 with default collation shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye84 (not from table default but from the charset default); column shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye85 uses the default character set and collation of the table Example (Testing Character Sets and Collations). Read " shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye86" Character Set and Collation Related System VariablesThere are quite a number of system variables related to charset and collation, with names starting with " shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye87" and " shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye88", respectively. You can list them as follows The system variables shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye89, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye90 and shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye91, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye92 maintain the default charset and collation for the server and the current database respectively In MySQL, columns, tables, databases may use different character sets. However, the meta-data (such as column names and attributes) must be stored in the same character set, and include all languages. Hence, meta-data, by default, stored in UTF-8, as maintained in the system variable shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye93 Character Set for Client-Server CommunicationA client connects to the server via a so-called connection object. The client sends the SQL statements over the connection to the server. The server returns the results via the connection to the client. Server maintains a connection for each client, and maintains connection-related character set and collation system variables for each client connection The client sends its statements using shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye94. They are translated to shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye95, and uses shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye96 for comparing string literals. The server returns the results to the client using shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye97. You can use command " shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye98" (e. g. , " shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye99") to change the character set used for client-server communication. " > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye00", in effect, changes system variables shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye94, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye95, and shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye97. For example, When a client (e. g. , mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......1, > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye05, > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye06) connects to the server, it sends the name of the character set that it wants to use to the server (default is shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye55; or specify in > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye08 startup option of the client). The server uses this name to set the shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye94, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye97, and shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye95 system variables for this client connection. In effect, the server performs a " > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye00" String Data TypesMySQL supports fixed-length and variable-length string types
Example. [TODO] String Literals and String IntroducerA string literal has on optional charset introducer and collate clause. The introducer tells the parser that the string that is followed uses a certain character set CONVERT(. USING . ) Function> mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye18 can be used to convert string between different character sets, e. g. , > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt1 String introducer merely interprets the string literal and does not change its value; whereas > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye19() returns a new value of the specified type. For example, UTF8 CharsetFor internationalization, UTF8 charset shall be used UTF8-Encoded Chinese Characters in Windows' CMD ShellIn brief, do not use CMD shell to work on UTF8 charset. Use a tool such as NetBeans, Eclipse, MySQL workbench or PhpMyAdmin which fully supports UTF8 charset If you want to display a text file encoded in a particular character set (e. g. , ASCII, Latin1, UTF8) in CMD correctly, you need to choose a matching "codepage". For example, ANSI/Latin1 requires codepage 1252, Simplified Chinese GBK (GB2312) requires 936, UTF8 requires 65001, UCS2LE requires 1200, UCS2BE requires 1201, and the original DOS extended-ASCII uses codepage 437 CMD is NOT able to detect the character set automatically and switch to the matching codepage. To set the codepage in CMD, issue command " > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye20", e. g. " > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye21" to choose UTF8 codepage. This codepage could display UTF8 text correctly, and display garbage if the text is encoded using other character set, including UCS2, GB2312 You also have to choose a font (such as Consolas, Lucida Console, but NOT raster font) that supports the characters. To choose the font for CMD, click on the CMD icon ⇒ "Properties" ⇒ "Font" There are a number of character encoding schemes for Chinese characters, e. g. , Unicode (UTF8, UCS2GE, UCS2LE), GB2312, GBK and BIG5, which are not compatible. Đọc phần "Hướng dẫn về biểu diễn dữ liệu" để biết thêm chi tiết Unicode is an international standard, which supports all languages, including Chinese, Japanese and Korean (CJK). Unicode has two frequently-used encoding schemes. UTF8 (1-3 bytes variable-length, the 1-byte codes are backward-compatible with ASCII) and UCS2 (2 byte fixed-length). UTF8 is ideal if your text consists of mainly ASCII (English text), with occasional non-ASCII text. I strongly recommend using UTF8 character set for MySQL columns that require internationalization support. GB2312 là tiêu chuẩn quốc gia của Trung Quốc dành cho tiếng Trung giản thể, nó không tương thích với UCS2 hoặc UTF8. For example, for the chinese characters "您好", the UCS2 is " > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye22", the UTF8 is " > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye23", the GB1232 is " > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye24" Unfortunately, in CMD, the Microsoft Pinyin IME (MSPY), the Chinese character input tool, is disabled for codepage 65001 (UTF8) (?. ?. ). MSPY works on GB2312 (codepage 936). If you run your MySQL client in CMD, using codepage 936 (GB2312) so that you can input chinese characters (e. g. , đối với > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+92 và > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye26), nhưng các cột ký tự trong cơ sở dữ liệu của bạn đang sử dụng UTF8, bạn cần xử lý tình huống này hết sức cẩn thận In MySQL client, you can issue the command " > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye27'" to specify the character set used for the client/server communication. (Alternatively, you could start > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye28 client with the > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye29 option. ) Trong tình huống trên, bạn cần phát hành " > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye30", vì đầu vào của bạn được mã hóa bằng GB2312, được gửi đến máy chủ. The server receives the characters in GB2312, converts them into the character set defined for the column, which is UTF8, before storing them. Trong khi truy vấn, máy chủ truy xuất các ký tự từ các cột trong UTF8, chuyển đổi thành GB2312 (theo > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye00) và gửi tới máy khách trong CMD, sử dụng trang mã 936 để hiển thị chính xác văn bản. Try the following commands > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt2 Quan sát rằng > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye00 ảnh hưởng đến shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye94, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye95 và shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye97 Giả sử rằng bạn tương tác với máy chủ thông qua một chương trình Java, được viết bằng Eclipse hoặc các chương trình khác. Bạn nên đặt mã hóa thành UTF8 và bao gồm " shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye99" trong tập lệnh MySQL của mình Tương tự, giả sử rằng bạn đang chạy tập lệnh SQL ở chế độ hàng loạt hoặc sử dụng lệnh nguồn trong CMD và tập lệnh của bạn được mã hóa bằng UTF8, thì bạn nên đưa " shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye99" vào tập lệnh (" > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye30" dẫn đến rác được chèn vào cơ sở dữ liệu). Nhưng để truy vấn tương tác trong CMD bằng mã trang 936, bạn cần " > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye30" để chuyển đổi UTF8 thành GB2312 (cp936) Tôi hy vọng rằng, trong CMD sử dụng trang mã 65001, tôi có thể hiển thị cột UTF8 từ MySQL một cách chính xác với "_______20_______99". Nhưng nó không hoạt động trong máy tính của tôi (?. ?. ) Lưu ý rằng bạn có thể sử dụng hàm > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt64 để yêu cầu mã hex của các ký tự, được lưu trữ trong cơ sở dữ liệu > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt3 Ví dụ (Kiểm tra các ký tự tiếng Trung được mã hóa UTF8). Đọc " > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye42" Lưu ý khi sử dụng các ký tự được mã hóa UTF8 trong Eclipse/Java
Dữ liệu nhị phân và BLOBMySQL hỗ trợ cả chuỗi ký tự (một chuỗi ký tự) và chuỗi nhị phân (một chuỗi byte). Dữ liệu nhị phân (e. g. , hình ảnh và mã) là chuỗi đặc biệt có bộ ký tự là > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye46, nghĩa là tất cả các ký tự được coi là byte thô nhị phân
Ví dụ. Hàm > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye51, đọc từ tệp và trả về một chuỗi, có thể được sử dụng để tải trường > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye52/ > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye53 với tệp nhị phân/văn bản > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt4 Ví dụ. Lưu trữ hình thu nhỏ [Tham khảo Ví dụ về Cơ sở dữ liệu cho thuê] ENUM> mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye54 là một loại chuỗi đặc biệt. Một > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye54 (hoặc liệt kê) là một chuỗi có giá trị được chọn từ danh sách các giá trị được phép (hoặc thành viên). The list of allowed values are defined explicitly in the column definition of the > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt18 command. Ví dụ, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt5 Giá trị phải là một chuỗi ký tự được trích dẫn. Giá trị trùng lặp không được phép. > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt89 chỉ được phép nếu cột được khai báo là cho phép > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt89. Nếu bạn chèn một giá trị không có trong danh sách > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye54, một chuỗi trống ( > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye60) sẽ được chèn vào, báo hiệu giá trị lỗi Mỗi loại > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye54 được liên kết với một chỉ mục, bắt đầu bằng 1 cho giá trị được phép đầu tiên. Chuỗi trống có chỉ số là 0 (để biểu thị giá trị lỗi). Chỉ số của > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt89 là > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt89 ENUM thường được biểu diễn bằng 16-bit, cho phép 65.535 thành viên Ví dụ (Thử nghiệm loại ENUM)
BỘ> mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt81 cũng là một kiểu chuỗi đặc biệt. > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt81 tương tự như > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye54, nhưng bạn có thể chọn giá trị không (chuỗi trống) hoặc nhiều giá trị hơn từ các giá trị được phép; . Đối với > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt81 (_______20_______29, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye41, > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye82), các giá trị hợp lệ là > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye60 (chuỗi trống), shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye29, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye41, > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye82, > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye87, > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye88, > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye89 và > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye90. Dấu phẩy dùng để phân cách nhiều giá trị, không có dấu cách xung quanh dấu phẩy Một bộ có thể có tối đa 64 thành viên. Trong MySQL, một giá trị đã đặt được lưu trữ dưới dạng số nguyên 64 bit, với bit ít quan trọng nhất tương ứng với thành viên đầu tiên. Ví dụ: đối với > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt81 (_______20_______29, shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye41, > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye82), giá trị số được chọn là shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye290011 shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye410102 > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye821004 > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye600000 > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye901117 Tương tự như > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye54, bạn có thể sử dụng giá trị số Ví dụ (Kiểm tra kiểu dữ liệu SET). Đọc " /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......01" [TODO] Giải thích Ngày giờNgày và giờ (cũng như tiền tệ) được quan tâm đặc biệt đối với các ứng dụng cơ sở dữ liệu. Đó là bởi vì hồ sơ kinh doanh thường mang thông tin ngày/giờ (e. g. , > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+57, /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......03, > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+58), cũng như nhu cầu đánh dấu thời gian cho lần cập nhật cuối cùng của hồ sơ để kiểm tra và bảo mật MySQL cung cấp các hàm dựng sẵn này để lấy ngày và/hoặc giờ hiện tại
Ví dụ như, > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+1 MySQL cung cấp các kiểu dữ liệu ngày/giờ này
Giá trị ngày/giờ có thể được nhập thủ công dưới dạng chuỗi ký tự (e. g. , /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......30 cho /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......31). MySQL sẽ đưa ra cảnh báo và chèn tất cả các số không (e. g. , '0000-00-00 00:00:00' for ________38_______31 ), nếu giá trị của ngày/giờ được chèn không hợp lệ hoặc nằm ngoài phạm vi. /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......33 được gọi là ngày "giả" Ví dụ (Kiểm tra kiểu dữ liệu NGÀY/GIỜ)
Ví dụ (DẤU THỜI GIAN Tạo và cập nhật lần cuối). Trong MySQL, bạn chỉ có thể có một cột > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+63 với /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......44. Để duy trì cả dấu thời gian tạo và cập nhật lần cuối, hãy đặt cột tạo thành mặc định 0 và chèn một > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt89 để nhận /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......37 loại đúcĐể phân tích một chuỗi (các chữ số) thành số, hãy sử dụng hàm /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......47 hoặc /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......48. Ví dụ, > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+8 [TODO] thêm Các kiểu dữ liệu không gian
Tuyên bố chuẩn bịTấn công tiêm SQLThẩm quyền giải quyết. Ví dụ về "Tấn công SQL injection" của Steve Friedl Giả sử rằng ứng dụng của bạn nhắc người dùng nhập tên người dùng/mật khẩu của anh ấy và dự định đưa ra câu lệnh SQL sau > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt19 > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+9 Người dùng ác ý có thể nhập " /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......50", kết quả là Điều gì sẽ xảy ra nếu bạn có lệnh /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......51 như sau. Tất cả các bản ghi trong bảng sẽ bị xóa shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye0 Trong một số trường hợp, bạn có thể thực thi nhiều câu lệnh SQL, phân tách chúng bằng dấu phẩy. Người dùng ác ý có thể nhập "______38_______52", trong đó -- (hoặc #) là nhận xét cuối dòng của MySQL. Kết quả này trong Các giải pháp là
Tuyên bố chuẩn bịMột câu lệnh đã chuẩn bị (hoặc câu lệnh được tham số hóa) chứa các phần giữ chỗ cho các tham số đầu vào. Bạn có thể sử dụng lại cùng một câu lệnh đã chuẩn bị nhiều lần, với các đầu vào khác nhau. Prepared statements are often pre-compiled, which are more efficient than the normal statements Các bước trong việc sử dụng báo cáo chuẩn bị là
Tấn công chèn HTMLCó một cuộc tấn công tiêm chích tương tự khác được gọi là HTML Injection (hoặc Cross-site Scripting hoặc XSS), có thể làm lộ quyền riêng tư và khả năng bảo vệ của người dùng web, nhưng không ảnh hưởng nhiều đến tính bảo mật của hệ thống Giả sử bạn yêu cầu người dùng để lại nhận xét của họ. Người dùng ác ý có thể nhập một số /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......64 /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......65 /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......66 là /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......67, /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......68 là /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......68;, > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt26 là > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt26 và /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......72 là /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......72) trước khi phản hồi lại cho người dùng Các đối tượng và chương trình được lưu trữMột đối tượng cơ sở dữ liệu được lưu trữ là một tập hợp các câu lệnh SQL được lưu trữ trong máy chủ để gọi tiếp theo. Các đối tượng được lưu trữ trong MySQL bao gồm
Một đối tượng được lưu trữ được liên kết với cơ sở dữ liệu. Bạn có thể gọi một thường trình không thuộc cơ sở dữ liệu hiện tại bằng cách xác định tên cơ sở dữ liệu, e. g. /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......90 hoặc /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......91. Tất cả các đối tượng được lưu trữ cho cơ sở dữ liệu sẽ bị xóa khi cơ sở dữ liệu bị hủy Bạn có thể sử dụng /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......92, để tạo, thay đổi hoặc xóa các đối tượng được lưu trữ Trước khi thảo luận về các đối tượng được lưu trữ, chúng ta cần xem cú pháp MySQL để lập trình các đối tượng được lưu trữ Tuyên bố phức hợpMột câu lệnh ghép bao gồm nhiều câu lệnh, được coi là một đơn vị. Một câu lệnh ghép được đính kèm trong /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......93. Mỗi câu lệnh được kết thúc bằng > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt06 (được gọi là dấu phân cách câu lệnh). Vì "dấu phân cách câu lệnh" gặp sự cố với dấu phân cách "cuối câu lệnh" của máy khách mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......1 (báo hiệu máy khách gửi câu lệnh đến máy chủ để xử lý), chúng ta cần sử dụng lệnh /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......96 để tạm thời thay đổi "dấu phân cách câu lệnh" . Ví dụ, Biến cục bộTrong một câu lệnh ghép được bao quanh bởi /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......93, chúng ta có thể khai báo các biến cục bộ bằng cách sử dụng câu lệnh > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt84, chỉ định tên, loại và giá trị mặc định tùy chọn của nó. Phạm vi của các biến cục bộ nằm trong /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......93. Chúng ta có thể sử dụng lệnh > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt81 để gán giá trị cho các biến cục bộ Chúng ta cũng có thể sử dụng " mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)02" để gán giá trị cho (các) biến cục bộ từ một truy vấn. " mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)03" sẽ trả về một hàng. Bạn có thể sử dụng " mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)04" để giới hạn đầu ra của > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt19 thành một hàng với tiêu chí lựa chọn và thứ tự phù hợp Kiểm soát lưu lượngMySQL hỗ trợ các cấu trúc điều khiển luồng mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)06, mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)07, mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)08, mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)09, mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)10 và mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)11 cho các chương trình được lưu trữ shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye1 shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye2 shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye3 shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye4 shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye5 shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye5 shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye7 shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye7 Các chức năng điều khiển lưu lượng sau đây khả dụng shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye9_______20_______9 shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye9 shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye9 thủ tục lưu trữcú pháp > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye3 Bạn có thể sử dụng các tham số để truyền dữ liệu vào và nhận dữ liệu từ một thủ tục được lưu trữ bằng cách khai báo hướng của các tham số là /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......78, /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......79 hoặc /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......80. Dữ liệu nhận được thường được đặt trong các biến do người dùng định nghĩa Bạn có thể sử dụng một câu lệnh ghép, bao gồm nhiều câu lệnh, làm phần thân của mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)15 Một câu lệnh ghép được đặt giữa mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)16 và mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)17. Vì mỗi câu lệnh được kết thúc bằng dấu chấm phẩy, điều này gặp sự cố với dấu kết thúc câu lệnh của MySQL, chúng tôi phải sử dụng /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......96 để thay đổi dấu phân cách của MySQL Ví dụ Bạn có thể tùy ý sử dụng " mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)19" và " mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)20" để kiểm soát quyền truy cập của người dùng vào thủ tục được lưu trữ > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye4 Nếu sử dụng mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)21, quy trình sẽ thực thi với các đặc quyền của người dùng mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)19 bất kể người dùng nào gọi nó. Nếu mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)23 được sử dụng, quy trình sẽ thực thi với các đặc quyền của người dùng gọi nó. mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)19 mặc định là người dùng hiện tại Hàm do người dùng định nghĩacú pháp > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye5 Một hàm trả về một giá trị vô hướng, thông qua câu lệnh mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)25. Tất cả các tham số là ______38_______78 tham số Ví dụ > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye6 Để gọi hàm, hãy sử dụng /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......74 /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......75 > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye7 Ví dụ Lượt xemDạng xem là một truy vấn đặt trước được lưu trữ trong cơ sở dữ liệu. Chế độ xem có thể hữu ích để cải thiện bảo mật bằng cách hạn chế dữ liệu có sẵn cho người dùng ít đặc quyền hơn; cú pháp > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye8 Ví dụ. Bảng > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+38 của chúng tôi có 5 cột. Chúng tôi có thể tạo chế độ xem để hạn chế một số người dùng ở hai cột, > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+45 và mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)31, như sau Gây nênTrình kích hoạt là một trình xử lý sự kiện thực thi để phản hồi lại một sự kiện. Một trình kích hoạt được liên kết với một bảng. Ba loại sự kiện bảng có thể kích hoạt trình kích hoạt. /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......51 (bao gồm mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)33), > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+92 (bao gồm mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)35 và mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)33), và > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye26. Bạn có thể đặt trình kích hoạt để thực thi mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)38 hoặc mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)39 sự kiện bảng cú pháp > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye9 Bạn có thể tham khảo các cột trong bảng được liên kết với trình kích hoạt bằng cách sử dụng bí danh mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)40 và mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)41
Ví dụ. Lưu hàng vào bảng dự phòng trước khi hàng bị xóa Để kiểm tra kích hoạt /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......0 Ví dụ (Được tạo và cập nhật lần cuối)Sự kiệnMột sự kiện là một tập hợp các câu lệnh SQL được lưu trữ được thực thi vào ngày và giờ đã lên lịch. Khi một sự kiện được hoàn thành, nó sẽ tự động bị hủy cú pháp /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......1 Lịch trình được xác định trong " mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)51" (e. g. , mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)52, mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)53). Bạn có thể chỉ định thời gian tương đối với tùy chọn " mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)54", (e. g. , mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)55) Bạn cần kích hoạt bộ lập lịch sự kiện, đây là một luồng đặc biệt để duy trì hàng đợi sự kiện và chạy các sự kiện đã lên lịch /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......2 Bạn cũng có thể khởi động máy chủ shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye71 với tùy chọn mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)57 Ví dụ Một sự kiện cũng có thể được lặp lại /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......3 Ví dụ Quản trị máy chủ cơ sở dữ liệu MySQLQuản lý tài khoản người dùngĐể tạo người dùng mới, sử dụng lệnh mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)58 như sau /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......4 mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)59 mặc định là máy chủ cục bộ. Bạn có thể sử dụng tên máy chủ DNS hoặc địa chỉ IP. Ký tự đại diện mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)60 và > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+31 có thể được sử dụng cho mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)59, e. g. , mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)63 (đối với tất cả máy chủ), mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)64, mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)65. Đối với địa chỉ IP, mặt nạ mạng có thể được chỉ định trong mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)66 mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)67, e. g. , mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)68 Người dùng mới được tạo không có đặc quyền. Bạn cần cấp đặc quyền phù hợp cho người dùng bằng lệnh mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)69. Đặc quyền có thể được cấp trên toàn cầu (cho tất cả các bảng của tất cả các cơ sở dữ liệu) hoặc ở cấp cơ sở dữ liệu, cấp bảng, cấp cột. Ví dụ, Các đặc quyền có sẵn là
Để xóa đặc quyền, hãy sử dụng lệnh mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)91, e. g. , Bạn có thể cần đưa ra lệnh " mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)92" để xóa và tải lại bộ đệm tạm thời trong MySQL, để các đặc quyền mới có hiệu lực ngay lập tức Để xóa người dùng, hãy sử dụng lệnh mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)93 như sau /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......5 Để thay đổi mật khẩu của người dùng, hãy sử dụng lệnh mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)94, e. g. , Để đổi tên người dùng, hãy sử dụng lệnh mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)95, e. g. , /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......6 Cân nhắc bảo mật về quản lý người dùng
Nhật kýMySQL duy trì một số nhật ký để giúp bạn duy trì máy chủ cơ sở dữ liệu của mình. Nhật ký lỗi, Nhật ký nhị phân, Nhật ký truy vấn chung, Nhật ký truy vấn chậm. Theo mặc định, tất cả nhật ký được lưu trong thư mục dữ liệu Nhật ký lỗiNhật ký lỗi duy trì tất cả các thông báo lỗi khởi động, tắt máy và vận hành quan trọng. Lỗi đăng nhập được bật theo mặc định. Tệp nhật ký lỗi có tên là " mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+10 mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+11" trong thư mục dữ liệu. Bạn có thể thay đổi tệp nhật ký lỗi thông qua tùy chọn khởi động mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+12 shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye71 Trên Windows, nếu bạn sử dụng tùy chọn mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+14, lỗi sẽ được ghi vào mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+15 (mặc định là bảng điều khiển) thay vì tệp nhật ký lỗi Nhật ký nhị phânNhật ký nhị phân giữ tất cả các câu lệnh thay đổi cơ sở dữ liệu, e. g. , > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt18, > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+92, > mysql -u myuser -p -vvv -e "SELECT user, host FROM user; SHOW databases" mysql Enter password: ******** -------------- SELECT user, host FROM user -------------- +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | ...... +-----------+-----------+ -------------- SHOW databases -------------- +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ...... +--------------------+ Bye26, /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......51. Nó không giữ các câu lệnh truy vấn (chẳng hạn như > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt19, mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+21, mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+22) mà không thay đổi cơ sở dữ liệu. Nhật ký nhị phân có thể được sử dụng để sao chép máy chủ sao lưu nô lệ hoặc để cập nhật cơ sở dữ liệu sao lưu bằng cách thực hiện tất cả các thay đổi được ghi lại. Để bật nhật ký nhị phân, hãy khởi động máy chủ MySQL shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye71 với tùy chọn mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+24. Một số được thêm vào phía sau mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+25. Máy chủ tạo một tệp nhật ký mới với số tiếp theo mỗi khi nó bắt đầu hoặc bất cứ khi nào nhật ký bị xóa. Một tệp chỉ mục cũng được tạo để theo dõi tất cả nhật ký nhị phân Có ba định dạng đăng nhập
Bạn có thể hiển thị nhật ký nhị phân bằng tiện ích mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+29, tiện ích này chuyển đổi các mục nhật ký thành câu lệnh SQL /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; CREATE DATABASE /*!32312 IF NOT EXISTS*/ `eastwind` /*!40100 DEFAULT CHARACTER SET latin1 */; ......9 Để cập nhật máy chủ dự phòng, hãy chuyển đầu ra của mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+29 sang máy khách mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......1 mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)0 Nhật ký truy vấn chungNhật ký truy vấn chung duy trì thông tin về các kết nối của máy khách và tất cả các câu lệnh SQL. Để bật, hãy sử dụng tùy chọn khởi động shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye71 mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+33 (hoặc mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+34 với tên tệp mặc định là mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+10 mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+36). Nhật ký có thể được chuyển hướng đến một tệp (mặc định) hoặc một bảng ( mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+37) hoặc cả hai hoặc bị vô hiệu hóa thông qua một tùy chọn bổ sung mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+38 Nhật ký truy vấn chậmTheo dõi câu lệnh SQL mất hơn mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+39 (mặc định là 10 giây). Bạn có thể kích hoạt thông qua tùy chọn khởi động shell> mysql -u myuser -p -t -vvv < d:\myproject\sqlscripts\testscript.sql Enter password: ******** -------------- SHOW DATABASES -------------- +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec) -------------- SELECT user, host FROM user -------------- +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+ 4 rows in set (0.00 sec) Bye71 mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+41. Tương tự như Nhật ký truy vấn chung, nó có thể được chuyển hướng đến tệp (mặc định) hoặc bảng ( mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+42) hoặc cả hai hoặc bị vô hiệu hóa thông qua tùy chọn bổ sung mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+38 Duy trì tệp nhật kýCác tệp nhật ký phát triển và cần được duy trì định kỳ. Bạn có thể xóa nhật ký lỗi và nhật ký truy vấn, nhưng không thể xóa nhật ký nhị phân vì nhật ký đó được sử dụng để sao lưu và phục hồi Bạn có thể ra lệnh " mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+44" để đóng và mở lại tất cả các tệp nhật ký. Đối với nhật ký lỗi, tệp nhật ký cũ sẽ được đổi tên với hậu tố " mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+45" (có thể xóa) và một tệp mới được tạo Đối với nhật ký nhị phân, một tệp mới có số tiếp theo sẽ được tạo khi nhật ký bị xóa. Đối với nhật ký nhị phân, bạn có thể đặt mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+46 tự động hết hạn các tệp nhật ký nhị phân sau một số ngày nhất định. Đảm bảo rằng bản sao hoặc bản sao lưu được cập nhật trước khi nhật ký nhị phân hết hạn Nhật ký truy vấn chung, nhật ký truy vấn chậm và nhật ký nhị phân có thể ghi các câu lệnh chứa mật khẩu. Để xem xét bảo mật, nên có một vị trí trong thư mục chuyên dụng (e. g. , " mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+47") với quyền truy cập vào máy chủ cơ sở dữ liệu và chỉ quản trị viên [TODO] Tập lệnh và quy trình duy trì tệp nhật ký, sao lưu và phục hồi Ghi chú cho InnoDBTrong thư mục dữ liệu, mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+48 chứa cơ sở dữ liệu InnoDB của bạn và mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+49 và mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+50 là các tệp nhật ký cho InnoDB. Hai tệp nhật ký 5 MB được sử dụng theo cách vòng tròn Sao lưu và phục hồiCó hai loại sao lưu. sao lưu logic và sao lưu vật lý (thô)
Tiện ích "mysqldump"" > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt13" có thể được sử dụng để sao lưu toàn bộ máy chủ, cơ sở dữ liệu được chọn hoặc bảng được chọn của cơ sở dữ liệu. Nó tạo ra một tệp văn bản gồm các câu lệnh SQL mà sau này có thể được thực thi để tạo lại cơ sở dữ liệu/bảng và nội dung của nó mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)1 Bạn có thể khôi phục từ bản sao lưu bằng cách chạy ứng dụng khách " mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......1" ở chế độ hàng loạt để thực thi các câu lệnh SQL mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)2 hoặc sử dụng lệnh nguồn trong ứng dụng khách tương tác mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)3 Ví dụ. Lệnh sau sao lưu bảng mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+61 và > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+35 của cơ sở dữ liệu mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+63 mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)4 Nghiên cứu tệp đầu ra, chứa các câu lệnh > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt18 và > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+92 để tạo lại các bảng đã kết xuất Sao lưu gia tăng bằng Nhật ký nhị phânKích hoạt nhật ký nhị phân, bằng cách chạy máy chủ mysqld với tùy chọn mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+66 Để tạo một bản sao lưu gia tăng từ bản sao lưu đầy đủ hoặc bản sao lưu gia tăng cuối cùng. Đầu tiên xóa nhật ký ( mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+44) để xoay nhật ký nhị phân. Then, use utility mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+29 to convert the log entries to SQL statements and pipe into a mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......1 client mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)5 Chiến lược sao lưu và phục hồi bằng mysqldump và Nhật ký nhị phânInnoDB storage engine supports transaction and automatic crash recovery. Suppose that MySQL server was terminated abnormally (e. g. , mất điện, sự cố hệ thống). Khi khởi động lại, công cụ InnoDB đọc nhật ký của nó cho các giao dịch đã cam kết và chưa cam kết đang chờ xử lý. Nó xóa dữ liệu cho những giao dịch đang chờ xử lý đã cam kết và khôi phục giao dịch chưa được cam kết. Nó ghi lại tất cả các hành động của nó trong nhật ký lỗi Tuy nhiên, nếu đĩa dữ liệu MySQL bị hỏng, chúng tôi cần khôi phục cơ sở dữ liệu từ nhật ký nhị phân và sao lưu đầy đủ mới nhất (hy vọng không bị hỏng), cần có chính sách sao lưu/phục hồi trực tiếp Giả sử rằng chúng tôi chạy sao lưu toàn bộ vào Chủ nhật hàng tuần lúc 3. 00 giờ sáng và sao lưu gia tăng mỗi ngày lúc 3 giờ. 00pm
"mysqldump" với đầu ra bảngYou can use option mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+80 to direct > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt13 to backup the data in table format (instead of > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt18 and > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+92 statements). For example, mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)9 Two file are created in the specified directory for each table dumped. In our example, " mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+84" for the data and " mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+85" for the > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt18 statement. The text file uses 'tab' as column delimiter, mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+87 as the line delimiter, and mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+88 for > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt89 SELECT . INTO OUTFILEUse for backing up the contents of a table or selected columns and rows of a table mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+0 Ví dụ The output file has no column header, uses 'tab' as column delimiter, mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+87 as the line delimiter. > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt89 is represented as mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+88, as follows mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+1 LOAD DATA INFILEWe can use " mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+93" to import data from a text file into a database table. The default column delimiter is 'tab'; default line delimiter is mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+87, and > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt89 is represented as mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+88. " mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+56" can be used to restore data exported via " mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+54" Example. Suppose that we want to load a text file which has a different format from the table We shall use the same table mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+99 created earlier. The text file to be loaded is as follows. It contains a header row, two columns (name and price) need to be loaded, and two extra columns. The column delimiter is > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt97, line delimiter is mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......01 (created in Windows). We also want to raise the price by 10% mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+2 We could use the following mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+93 command mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+3 The " mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......03" and " mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......04" clauses are used to specified the column delimiter and line delimiter, respectively. " mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......05" ignores the header row of the file. The first column of the file are loaded into table columns " > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+45". The third column of the file is assigned to a user-defined variable mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......07. The mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | ascii | US ASCII | ascii_general_ci | 1 | ...... 39 rows in set (0.06 sec) mysql> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | ...... 197 rows in set (0.01 sec)31 column of the table is then set to mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......09. The other columns of the text file is assigned to an user-defined variable mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......10, which is not used and discarded The > mysql -u myuser -p < d:\myproject\sqlscripts\testscript.sql > output.txt19 command confirms that the table is properly loaded mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+4 "mysqlimport" Utilitymysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+55 performs the same function as " mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+56" to load data from a text file into a table. " mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+56" runs inside an interactive client, whereas mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+55 runs from command-line mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+5 For example, mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+6 Load the data from " mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......16" into table > mysql -u myuser -p -t < d:\myproject\sqlscripts\testscript.sql Enter password: ******** +--------------------+ | Database | +--------------------+ | information_schema | | eastwind | | mysql | | test | +--------------------+ +--------+-----------+ | user | host | +--------+-----------+ | root | 127.0.0.1 | | myuser | localhost | | root | localhost | +--------+-----------+35, " mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+84" into table mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+61 of the database mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+63. The table name is derived from the file name by ignoring the file extension SecurityDon't run the MySQL server as superuser rootIf you run MySQL server as root, it has root privilege (e. g. , can access all the files in the system). Run MySQL server as an ordinary, unprivileged user. For example, in Ubuntu, a system, unprivileged user called mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......1 is created to run the MySQL server mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+7 To specify the owner of the mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......1 process, you need to configure mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......23 as follows mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+8 The MySQL data directory shall be owned and only be accessible by server's accountAs seen in the above example, the data directory is only accessible by the user "mysql". Other users have no access [The location of the data directory is configured in the " mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......24" directive of the configuration file " mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......23" as illustrated above. ] To set the ownerships and permissions mysql> SHOW COLLATION LIKE 'latin1%'; +-------------------+---------+----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +-------------------+---------+----+---------+----------+---------+ | latin1_german1_ci | latin1 | 5 | | Yes | 1 | | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | | latin1_danish_ci | latin1 | 15 | | Yes | 1 | | latin1_german2_ci | latin1 | 31 | | Yes | 2 | | latin1_bin | latin1 | 47 | | Yes | 1 | | latin1_general_ci | latin1 | 48 | | Yes | 1 | | latin1_general_cs | latin1 | 49 | | Yes | 1 | | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | +-------------------+---------+----+---------+----------+---------+9 MySQL configuration file shall be properly protectedThe global configuration file mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......23 shall be owned by root, with permissions mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......27 or mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......28. That is, it is writable by root (or sudo), but readable by the world (both server and client program need to read this configuration). You should be keep confidential data such as password as it is world-readable The local configuration files ( mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......29 located at data directory) shall be owned by user "mysql" and read-write only by "mysql" (i. e, mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......30) Remove the mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......31 database MySQL user accounts
Chính sách mật khẩu
Backup RegularlyUse a mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......32 job (configured in mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......33)to back up the database regularly, and preferably mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......34 or mysql> SHOW COLLATION LIKE 'utf8%'; +--------------------+---------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------+---------+-----+---------+----------+---------+ | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | | utf8_bin | utf8 | 83 | | Yes | 1 | | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | ......35 to another server How to find script in MySQL?To run SQL script in MySQL, use the MySQL workbench. First, you need to open MySQL workbench. Now, File -> Open SQL Script to open the SQL script . Note − Press OK button twice to connect with MySQL.
Where are MySQL scripts stored?Theo mặc định, các tập lệnh được lưu trữ trong scripts/ thư mục của thư mục cấu hình MySQL Workbench của bạn .
Where can I find SQL scripts?Exporting and Importing SQL Scripts. . On the Workspace home page, click SQL Workshop and then SQL Scripts. . Select a script. . Click the Find button Tôi có thể tìm tệp cơ sở dữ liệu MySQL ở đâu?Its a MySQL database configuration file. Đây là file cấu hình chính của MySQL server. Điều này được tìm thấy trong thư mục gốc nơi quá trình cài đặt được thực hiện . Trong tệp này, người dùng có thể tìm thấy vị trí của thư mục dữ liệu. |