เว็บนี้มีลิงก์ affiliate — หากสมัครผ่านลิงก์ เราได้รับค่าคอมมิชชัน · Affiliate links. รายละเอียด

MySQL Database คืออะไร ตั้งค่า Optimize และ Backup อย่างไร

MySQL Database fundamentals: what it is, how to optimize it, backup strategies, and Hosting configuration.

MySQL Database คืออะไร ตั้งค่า Optimize และ Backup อย่างไร
แนะนำAsiaGB.com — Web Hosting & VPS คุณภาพสูง พร้อมทีม Support ภาษาไทย 24 ชั่วโมง SSD NVMe uptime 99.9%

AsiaGB.com — Premium hosting & VPS with Thai support, NVMe SSD, 99.9% uptime.

เยี่ยมชม AsiaGB →

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 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.

สร้าง 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.

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.

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.

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.

ความปลอดภัย 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.

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.

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.

คำถามที่พบบ่อย (FAQ)

WordPress ต้องการ MySQL Version ไหน?
WordPress 6.x ต้องการ MySQL 5.7+ หรือ MariaDB 10.3+ แนะนำ MySQL 8.0 หรือ MariaDB 10.6+ เพราะเร็วกว่า ปลอดภัยกว่า และ Performance ดีกว่า ตรวจสอบ Version ที่ใช้ได้ใน cPanel → MySQL Version หรือ phpMyAdmin
Database สูญหายทำอย่างไร?
ถ้า Hosting มี Daily Backup ขอ Restore ผ่าน Support Ticket ถ้า Export ไว้เอง Import ผ่าน phpMyAdmin หรือ mysql command ถ้าไม่มี Backup เลย Recovery ยาก ควรป้องกันด้วย Automatic Backup และเก็บ Copy ไว้นอก Server
Database ใหญ่เกินไปทำให้เว็บช้าได้ไหม?
ได้ Database ใหญ่โดยไม่มี Index ที่ดีจะ Query ช้ามาก ในกรณี WordPress: ตาราง wp_options และ wp_postmeta มักใหญ่เกิน ใช้ Plugin เช่น WP-Optimize ลบข้อมูลเก่า แล้วรัน OPTIMIZE TABLE เพื่อ Defragment
phpMyAdmin ปลอดภัยไหม?
phpMyAdmin เองปลอดภัย แต่ต้องตั้งค่าให้ถูก ป้องกัน: ใช้ URL ที่ไม่ตรงไปตรงมา (Rename), ตั้ง IP Whitelist, ใช้ Strong Password, อัพเดท Version ล่าสุด และพิจารณาปิดถ้าไม่ต้องการ บน Hosting ส่วนใหญ่ phpMyAdmin ถูก Protect ด้วย cPanel Authentication แล้ว