สามารถให้สิทธิ์ SELECT บนทุกตารางในทุกฐานข้อมูล (Global privilege) ได้โดยใช้คำสั่ง GRANT SELECT ON *.*
Syntax
GRANT SELECT ON *.* TO 'yourUserName'@'yourHostName';
*.*- หมายถึง ทุกฐานข้อมูล (*) ทุกตาราง (*)'username'@'hostname'- ผู้ใช้และโฮสต์ที่อนุญาต (เช่น 'user'@'localhost' หรือ 'user'@'%')
ตัวอย่างการใช้งาน
1. ดูรายชื่อผู้ใช้ที่มีอยู่
mysql> select user, host from mysql.user;
+------------------+-----------+ | user | host | +------------------+-----------+ | Bob | % | | Charlie | % | | Robert | % | | User2 | % | | mysql.infoschema | % | | mysql.session | % | | mysql.sys | % | | root | % | | @UserName@ | localhost | | Adam | localhost | | Adam Smith | localhost | | Chris | localhost | | David | localhost | | Emma | localhost | | Jace | localhost | | James | localhost | | John | localhost | | John Doe | localhost | | Michael | localhost | | Mike | localhost | | Robert | localhost | | User1 | localhost | | am | localhost | | hbstudent | localhost | | mysql.infoschema | localhost | | mysql.session | localhost | +------------------+-----------+ 26 rows in set (0.00 sec)
2. ให้สิทธิ์ SELECT ทั้งหมดแก่ผู้ใช้ hbstudent@localhost
mysql> GRANT SELECT ON *.* TO 'hbstudent'@'localhost'; Query OK, 0 rows affected (0.00 sec)
คำสั่ง GRANT ที่เกี่ยวข้อง
| คำสั่ง | ความหมาย |
|---|---|
| GRANT SELECT ON *.* TO 'user'@'host'; | อ่านได้ทั้งหมด (Global read-only) |
| GRANT ALL PRIVILEGES ON *.* TO 'user'@'host'; | สิทธิ์เต็มทั้งหมด (Superuser) |
| GRANT SELECT ON db_name.* TO 'user'@'host'; | อ่านได้เฉพาะฐานข้อมูล db_name |
| GRANT SELECT ON db_name.table_name TO 'user'@'host'; | อ่านได้เฉพาะตาราง table_name |
ตรวจสอบสิทธิ์ที่ให้ไป
SHOW GRANTS FOR 'hbstudent'@'localhost';
⚠️ คำเตือน: การให้ GRANT SELECT ON *.* จะทำให้ผู้ใช้สามารถอ่านข้อมูลทุกฐานข้อมูล รวมถึง mysql, information_schema, performance_schema ได้ ควรให้สิทธิ์เฉพาะฐานข้อมูลที่จำเป็น (GRANT SELECT ON db_name.*) เพื่อความปลอดภัย