Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

ดูรายชื่อ User Accounts ใน MySQL

Query ตาราง mysql.user (System Table) เพื่อดู User และ Host

Query พื้นฐาน

mysql> SELECT User FROM mysql.user;
+------------------+
| User             |
+------------------+
| John             |
| Mac              |
| Manish           |
| mysql.infoschema |
| mysql.session    |
| mysql.sys        |
| root             |
| am               |
+------------------+

ดู User พร้อม Host

mysql> SELECT User, Host FROM mysql.user;
+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| John             | %         |
| Mac              | %         |
| Manish           | %         |
| mysql.infoschema | %         |
| mysql.session    | %         |
| mysql.sys        | %         |
| root             | %         |
| am               | localhost |
+------------------+-----------+

คอลัมน์สำคัญใน mysql.user

Columnความหมาย
UserUsername
HostHost ที่อนุญาตให้เชื่อม (% = ทุก Host, localhost = Local Only)
authentication_stringPassword Hash (Encrypted)
pluginAuthentication Plugin (caching_sha2_password, mysql_native_password, etc.)
account_lockedY/N (MySQL 5.7.6+)
password_expiredY/N
password_last_changedTimestamp

Query ดูสิทธิ์ (Grants)

SHOW GRANTS FOR 'user'@'host';
Security Best Practice:
  • ไม่ใช้ User 'root' จาก Remote Host (%): ใช้ 'root'@'localhost' เท่านั้น
  • สร้าง User เฉพาะ Application: CREATE USER 'app'@'%' IDENTIFIED BY 'strong_pass';
  • Grant Least Privilege: GRANT SELECT, INSERT ON db.* TO 'app'@'%';
  • ใช้ caching_sha2_password (Default MySQL 8.0+) แทน mysql_native_password