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

สร้าง MySQL User โดยไม่ระบุ Hostname (อัตโนมัติเป็น %)

ละ Hostname ได้

ถ้าระบุแค่ Username MySQL จะถือว่าเป็น user@'%' (เข้าจาก Host ใดก็ได้)

Syntax

USE mysql;
CREATE USER user_name IDENTIFIED BY 'password';

ตัวอย่าง: สร้าง user 'remote'

mysql> CREATE USER remote IDENTIFIED BY 'password123';
Query OK, 0 rows affected

mysql> SHOW GRANTS FOR remote@'%';
+------------------------------------+
| Grants for remote@%                |
+------------------------------------+
| GRANT USAGE ON *.* TO 'remote'@'%' |
+------------------------------------+

สร้างโดยไม่ระบุ Password (MySQL 8.0+)

mysql> CREATE USER hello;
Query OK, 0 rows affected

mysql> SHOW GRANTS FOR hello@'%';
+-----------------------------------+
| Grants for hello@%                |
+-----------------------------------+
| GRANT USAGE ON *.* TO 'hello'@'%' |
+-----------------------------------+

ข้อควรระวัง

  • ความปลอดภัย: % = ทุก Host ควรใช้เฉพาะ Development/Internal
  • Production ควรระบุ Host/IP เฉพาะ
  • MySQL 5.7 ต้องมี Password เสมอ MySQL 8.0 อนุญาตไม่มี Password