MySQL server hardware, OS and configuration tuning tips
MySQL server hardware, OS and configuration tuning tips
Hardware and OS
-
Use a 64-bit OS because it supports adding more memory.
-
Do not use software RAID.
-
Avoid RAID 5 and use RAID 10 instead.
-
A RAID controller with battery-backed cache is highly desirable.
-
Use disks that are not too large for better performance.
-
Use disks with fast reads and writes.
-
Put data and the OS on separate partitions.
-
Mount file systems with noatime and nodirtime.
-
Use the DEADLINE I/O scheduler instead of CFQ or ANTICIPATORY. Add elevator=deadline to grub.conf on the entry for the kernel you want to load.
title CentOS (2.6.32-220.7.1.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-220.7.1.el6.x86_64 ro root=LABEL=/ quiet elevator=deadline initrd /initramfs-2.6.32-220.7.1.el6.x86_64.img[ -
Install only the packages you use and remove unnecessary packages.
-
Check the number of files the mysql user can open with ulimit. If the database has many tables, raise the limit appropriately.
-
Keep MySQL temporary space and replication logs on a different partition from the data.
-
Prevent swap use by adding “vm.swappiness=0” to /etc/sysctl.conf.
-
Use the XFS file system to improve performance and prevent double buffering, which occurs with ext3, when used with MySQL.
-
Add frequently used hostnames and IP addresses to /etc/hosts to avoid DNS lookups.
-
Do not force-kill MySQL processes, as this can corrupt the database.
Configuration
- Use innodb_flush_method=O_DIRECT to prevent double buffering when writing data.
- Avoid using O_DIRECT with the EXT3 file system, as it serializes writes.
- Use skip-name-resolve.
- Set innodb_buffer_pool_size to no more than 70–80% of memory. This lets the system load InnoDB files into memory.
- Do not make innodb_log_file_size too large; around 256MB is reasonable. A larger size is faster but slows recovery after a system crash.
- Do not set max_connections too high. If too many connections arrive, memory use may exceed the available memory and crash the system.
- Keep innodb_log_buffer_size within 2–8MB unless you use large BLOBs.
- Enable innodb_file_per_table to prevent the main tablespace file from growing too large and store each table in its own file.
- Use the query cache when data does not change often.
- Increase temp_table_size and max_heap_table_size to avoid writing to disk.
- Do not set sort_buffer_size too high, because its memory use scales with the number of connections.
Backup
- Back up from the slave.
- Do not rely on LVM snapshots for backups, because data inconsistencies may occur.
- For online backups, I recommend xtrabackup.