MySQL Database คืออะไร ตั้งค่า Optimize และ Backup อย่างไร
MySQL Database fundamentals: what it is, how to optimize it, backup strategies, and Hosting configuration.
สารบัญ
MySQL คืออะไร
MySQL คือระบบจัดการฐานข้อมูล (DBMS) ที่นิยมมากที่สุดในโลกสำหรับ Web Application ใช้ SQL (Structured Query Language) ในการจัดการข้อมูล Open Source ฟรี WordPress, Drupal, Joomla, Magento และ Application อีกหลายล้านตัวใช้ MySQL
MySQL is the world's most popular database management system (DBMS) for web applications, using SQL (Structured Query Language) to manage data. Open source and free — WordPress, Drupal, Joomla, Magento, and millions of other applications use MySQL.
- MySQL: RDBMS ยอดนิยมของโลก
- SQL: ภาษาสำหรับ Query ข้อมูล
- Open Source: ฟรี Oracle ดูแล
- WordPress ใช้ MySQL เก็บ Post, Comment, User
- LAMP Stack: Linux, Apache, MySQL, PHP
MySQL vs MariaDB ต่างกันอย่างไร
MariaDB คือ Fork ของ MySQL ที่สร้างโดย Developers เดิมของ MySQL หลังจาก Oracle ซื้อ MySQL Compatible เกือบ 100% กับ MySQL แต่บาง Feature ต่างกัน Hosting หลายรายใช้ MariaDB แทน MySQL เพราะเร็วกว่าในบาง Workload
MariaDB is a MySQL fork created by the original MySQL developers after Oracle acquired MySQL. Nearly 100% compatible with MySQL but with some different features. Many hosts use MariaDB instead of MySQL for its performance advantages in certain workloads.
- MariaDB 10.x: Compatible กับ MySQL 8.x
- Performance: MariaDB เร็วกว่าบาง Read-heavy Workload
- Storage Engine: MariaDB มี InnoDB เหมือนกัน
- WordPress: รองรับทั้ง MySQL และ MariaDB
- แทนกันได้แทบสมบูรณ์ ทั่วไปไม่ต้องเปลี่ยน Code
สร้าง Database และ User
ใน cPanel ไปที่ MySQL Databases → สร้าง Database → สร้าง User → Assign User ให้ Database และกำหนด Permission ควรสร้าง Database User ที่มีสิทธิ์แค่ที่จำเป็น ไม่ควรใช้ root User สำหรับ Application
In cPanel, go to MySQL Databases → Create Database → Create User → Assign User to Database and set permissions. Create users with minimal required permissions — never use the root user for applications.
- สร้าง Database: cPanel → MySQL Databases
- สร้าง User: Username + Password แยกต่างหาก
- Assign Permission: SELECT, INSERT, UPDATE, DELETE
- ไม่ควรให้ ALL PRIVILEGES ถ้าไม่จำเป็น
- 1 App = 1 Database + 1 User (หลักการ Least Privilege)
Index คืออะไรและทำไมสำคัญ
Index ใน MySQL คือโครงสร้างข้อมูลที่ช่วยให้ Query เร็วขึ้น เหมือน Index หนังสือที่ช่วยหาหน้าเร็วกว่าอ่านทั้งเล่ม Column ที่ใช้ใน WHERE, JOIN, ORDER BY ควร Index ไว้ ไม่มี Index = Full Table Scan = ช้า
Index in MySQL is a data structure that speeds up queries — like a book index helping you find pages faster than reading the whole book. Columns used in WHERE, JOIN, and ORDER BY clauses should be indexed. No index = Full Table Scan = slow.
- Primary Key: Index อัตโนมัติ
- Index WHERE Column: WHERE user_id → Index user_id
- Composite Index: WHERE (a, b) → Index (a, b)
- Full Table Scan: ไม่มี Index = ช้ามากบน Table ใหญ่
- EXPLAIN: ดูว่า Query ใช้ Index ไหม
Optimize MySQL Configuration
ตั้งค่า innodb_buffer_pool_size ให้ 50-70% ของ RAM สำหรับ InnoDB Table (ส่วนใหญ่ใน WordPress) เปิด query_cache_size สำหรับ Read-heavy Site ตั้ง max_connections ให้เหมาะสมกับ Traffic ไม่สูงเกินจำเป็น
Set innodb_buffer_pool_size to 50-70% of RAM for InnoDB tables (most WordPress use). Enable query_cache_size for read-heavy sites. Set max_connections appropriately for your traffic — not higher than necessary.
- innodb_buffer_pool_size: 50-70% ของ RAM
- max_connections: 100-500 ขึ้นกับ Traffic
- slow_query_log: เปิดเพื่อหา Query ช้า
- tmp_table_size: 64M+ ลด Disk Temp Table
- Long-running Query: ตรวจด้วย SHOW PROCESSLIST
Backup MySQL Database
Backup Database เป็นสิ่งสำคัญที่สุด วิธีที่ง่ายที่สุดคือ mysqldump ซึ่ง Export เป็น SQL File สามารถ Schedule ผ่าน Cron Job ได้ ใน cPanel ใช้ MySQL Backup ใน Backups Section หรือ phpMyAdmin Export Function
Database backup is critical. The simplest method is mysqldump, which exports an SQL file schedulable via Cron Job. In cPanel, use MySQL Backup in the Backups section or phpMyAdmin's Export function.
- mysqldump -u user -p database > backup.sql
- Schedule ผ่าน Cron: รัน mysqldump ทุกคืน
- cPanel Backup: ทำได้ใน Backups Section
- phpMyAdmin Export: ง่าย เหมาะสำหรับมือใหม่
- เก็บ Backup ออกนอก Server: S3, Backblaze, Google Drive
ความปลอดภัย MySQL
ปิด Remote Root Login, ใช้ User ที่มีสิทธิ์ขั้นต่ำ, เปลี่ยน Password ที่แข็งแกร่ง, ปิด Port MySQL (3306) จากภายนอกถ้าไม่ต้องการ Remote Access, Run mysql_secure_installation หลัง Install ใหม่
Disable remote root login, use minimal-privilege users, set strong passwords, block MySQL port (3306) from external access if remote access isn't needed, run mysql_secure_installation after new installations.
- ปิด Remote Root: เข้า MySQL Root แค่ localhost
- User Privilege: ให้สิทธิ์แค่ที่จำเป็น
- Strong Password: 16+ Characters Random
- ปิด Port 3306: ถ้าไม่ต้องการ Remote DB Connection
- mysql_secure_installation: Run หลัง Install ใหม่
Query Performance ตรวจสอบและปรับปรุง
เปิด slow_query_log เพื่อ Log Query ที่ใช้เวลา > N วินาที ใช้ EXPLAIN ดู Query Plan ว่าใช้ Index ไหม ถ้า EXPLAIN แสดง type=ALL = Full Table Scan = ต้องเพิ่ม Index ตรวจสอบ SHOW PROCESSLIST เพื่อดู Query ที่กำลังรัน
Enable slow_query_log to log queries taking longer than N seconds. Use EXPLAIN to see query plans and index usage. If EXPLAIN shows type=ALL, it's a Full Table Scan requiring index addition. Check SHOW PROCESSLIST for currently running queries.
- slow_query_log=1, long_query_time=2
- EXPLAIN SELECT ... : ดู Query Plan
- type=ALL → Full Table Scan → เพิ่ม Index
- SHOW PROCESSLIST: Query ที่ Running อยู่
- MySQL Workbench: GUI สำหรับ Analyze
phpMyAdmin คืออะไร
phpMyAdmin คือ Web Interface สำหรับจัดการ MySQL Database ผ่านเบราว์เซอร์ ทุก cPanel/DirectAdmin มี phpMyAdmin ให้ใช้งาน ทำได้ทั้ง Browse Table, Run Query, Import/Export Database, สร้าง Index และ User
phpMyAdmin is a web-based MySQL management interface. Every cPanel/DirectAdmin installation includes phpMyAdmin, allowing table browsing, query execution, database import/export, index creation, and user management.
- เข้าได้ผ่าน cPanel → phpMyAdmin
- Browse Table: ดูข้อมูลใน Table
- Run SQL Query: กรอก SQL ตรงๆ
- Export: Dump Database เป็น SQL หรือ CSV
- Import: Upload SQL ไฟล์เพื่อ Restore