MySQL cho phép một số cách khác nhau để xác thực người dùng. Chúng được gọi là "plugin xác thực" và mỗi người dùng có thể chỉ định plugin của riêng mình. Để xem plugin nào được chỉ định cho người dùng, bạn có thể sử dụng giá trị
# Prior to MySQL 8
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| mysql_native_password |
+-----------------------+
8 trong bảng # Prior to MySQL 8
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| mysql_native_password |
+-----------------------+
9. Trên các hệ thống Ubuntu [và có thể cả các hệ thống Linux khác], cài đặt mặc định cho cài đặt MySQL mới cung cấp cho người dùng MySQL # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0 một plugin khác với người dùng tiêu chuẩn, điều này giải thích sự khác biệt mà bạn nhận thấyCụ thể, người dùng MySQL
# MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0 được chỉ định xác thực # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0 theo mặc định, trong khi người dùng tiêu chuẩn được chỉ định # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
1 [tiền MySQL 8] hoặc # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
2 [MySQL 8+]. Đó là, bạn sẽ thấy một cái gì đó như# Prior to MySQL 8
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| mysql_native_password |
+-----------------------+
hoặc
# MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
# MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0 là xác thực ổ cắm, hoạt động bằng cách khớp người dùng Unix đang kết nối với người dùng MySQL có cùng tên, nếu có. Điều này chỉ hoạt động khi kết nối cục bộ, đó là một lý do tốt cho người dùng MySQL # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0 đặc quyền; Cả
# MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
1 và # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
2 đều là hình thức xác thực mật khẩu. Để hiểu sự khác biệt mà bạn thấy giữa việc truy cập MySQL với tư cách là người dùng # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0 và với tư cách là người dùng tiêu chuẩn, chúng tôi sẽ so sánh quy trình đăng nhập giữa xác thực mật khẩu và xác thực socketxác thực mật khẩu
Giả sử bạn cố gắng truy cập máy chủ MySQL cục bộ bằng lệnh thường được khuyến nghị. Người dùng hệ điều hành Unix/Linux của bạn là
joeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
1 và bạn muốn kết nối với máy chủ MySQL với tư cách là người dùng MySQL joeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
2. Bạn gõjoeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
Kết nối thành công. Điều đã xảy ra ở đây là chương trình
joeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
3 [sử dụng joeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
4 để tìm chương trình này trên hệ thống của bạn] được gọi với các đối số dòng lệnh joeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
5. Từ những điều này, MySQL hiểu rằng bạn đang cố gắng kết nối với tư cách là người dùng MySQL joeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
2. Nó kiểm tra giá trị # Prior to MySQL 8
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| mysql_native_password |
+-----------------------+
8 của # Prior to MySQL 8
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| mysql_native_password |
+-----------------------+
9 cho joeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
2 và tìm thấy, giả sử, # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
2. Do đó, nó biết bạn cần mật khẩu để kết nối. Từ đối số dòng lệnh # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
81, nó biết rằng bạn muốn nó yêu cầu bạn nhập mật khẩu của mình và nó đã làm như vậy. Bạn nhập mật khẩu của mình và MySQL sẽ băm mật khẩu đó và so sánh hàm băm đó với mật khẩu được lưu trữ làm mật khẩu cho joeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
2. Nó phù hợp, vì vậy bạn được phép kết nối với tư cách là người dùng MySQL joeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
2Xác thực ổ cắm
Bây giờ, bạn muốn truy cập máy chủ MySQL cục bộ với tư cách là người dùng MySQL
# MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0. Nếu bạn ngây thơ thử cùng một lệnh mà bạn đã sử dụng cho người dùng chuẩn joeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
2, bạn sẽ gặp sự cố mà bạn đã lưu ý# MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
8Điều này xảy ra ngay cả khi bạn đã xác định mật khẩu cho người dùng MySQL
# MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0 và nhập mật khẩu đó khi được nhắc. Vậy chuyện gì đã xảy ra? . Tuy nhiên, bây giờ, khi nó kiểm tra giá trị # Prior to MySQL 8
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| mysql_native_password |
+-----------------------+
8 của # Prior to MySQL 8
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| mysql_native_password |
+-----------------------+
9 cho # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0, nó tìm thấy # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0 thay vì một trong các giá trị xác thực mật khẩuVì vậy, thay vì yêu cầu mật khẩu, nó sẽ kiểm tra một tệp ổ cắm được chỉ định mà hệ thống Unix/Linux của bạn đã viết rõ ràng cho mục đích này và tệp này bao gồm tên của người dùng hệ điều hành mà bạn đang đóng vai trò là. Thay vì khớp mật khẩu,
# MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0 khớp tên người dùng. Do đó, lưu ý rằng tên người dùng hệ điều hành bạn đang sử dụng, joeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
1, không bằng tên người dùng MySQL mà bạn đang cố gắng kết nối là, # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0, nó sẽ cấm kết nối với thông báo lỗi vô dụng nghiêm trọng # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
87Vì vậy, bực bội, bạn thực hiện động thái tiêu chuẩn của Linux là thêm 'sudo' để làm cho nó làm những gì bạn muốn
# MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
8Những công việc này. Tại sao điều này làm việc? . Sau khi được đưa ra, phần còn lại của lệnh được thực thi với tư cách là người dùng đó. Do đó, tệp ổ cắm được tạo khi kết nối với máy chủ MySQL được xác định bằng Unix
# MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0, không phải joeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
1 và xác thực ổ cắm của MySQL khớp với điều này với người dùng MySQL có cùng tên. Xác thực được thông qua tại thời điểm này, nhưng bạn đã yêu cầu cụ thể để được hỏi mật khẩu của mình với tùy chọn # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
81, vì vậy MySQL thực hiện nghĩa vụ bằng cách yêu cầu mật khẩu người dùng MySQL # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0. Sau khi nhập nó, bạn đã kết nối thành công với máy chủ MySQL cục bộMột câu hỏi rõ ràng khi bạn hiểu điều này là, nếu xác thực ổ cắm dựa trên tên người dùng phù hợp [tên người dùng Unix với tên người dùng MySQL], thì mật khẩu có cần thiết không? . Bạn có thể rời khỏi
# MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
81 và làm điều này# Prior to MySQL 8
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| mysql_native_password |
+-----------------------+
5và bỏ qua việc nhập mật khẩu MySQL
# MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0 của bạn. Điều này chỉ hoạt động đối với người dùng MySQL # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0 và chỉ khi người dùng đó được đặt để sử dụng # MySQL 8 and above
mysql> USE mysql;
mysql> SELECT plugin FROM mysql.user WHERE user='root';
+-------------+
| plugin |
+-------------+
| auth_socket |
+-------------+
mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user';
+-----------------------+
| plugin |
+-----------------------+
| caching_sha2_password |
+-----------------------+
0 làm plugin xác thực của họTóm lược
Theo mặc định tiêu chuẩn cho MySQL trên các phiên bản Ubuntu gần đây [ít nhất 16. 20-04. 04, có thể là các lệnh khác], đây là các lệnh để kết nối dễ dàng nhất với trình bao
joeuser@localhost:~$ mysql -u joesql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
3Người dùng MySQL
0.# MySQL 8 and above mysql> USE mysql; mysql> SELECT plugin FROM mysql.user WHERE user='root'; +-------------+ | plugin | +-------------+ | auth_socket | +-------------+ mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user'; +-----------------------+ | plugin | +-----------------------+ | caching_sha2_password | +-----------------------+
81# Prior to MySQL 8 mysql> USE mysql; mysql> SELECT plugin FROM mysql.user WHERE user='root'; +-------------+ | plugin | +-------------+ | auth_socket | +-------------+ mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user'; +-----------------------+ | plugin | +-----------------------+ | mysql_native_password | +-----------------------+
[nhập mật khẩu sudo của bạn]
Người dùng MySQL khác.
82# Prior to MySQL 8 mysql> USE mysql; mysql> SELECT plugin FROM mysql.user WHERE user='root'; +-------------+ | plugin | +-------------+ | auth_socket | +-------------+ mysql> SELECT plugin FROM mysql.user WHERE user='basic_db_user'; +-----------------------+ | plugin | +-----------------------+ | mysql_native_password | +-----------------------+
[nhập mật khẩu MySQL]
Tất nhiên, bạn đã hỏi về Debian Linux, không phải Ubuntu. Ubuntu có nguồn gốc trực tiếp từ Debian và tôi cho rằng nó tương tự như những gì tôi đã nói ở đây. Nhận xét về cách giải thích ở trên khác nhau giữa các bản phân phối và phiên bản Linux khác nhau đều được hoan nghênh