How to use:
jbolus@sandbox:/tmp$ chmod +x mysql-conf-optimizer.sh
jbolus@sandbox:/tmp$ ./mysql-conf-optimizer.sh
MySQL Optimization Suggestions:
---------------------------------
innodb_buffer_pool_size: 24GB
innodb_buffer_pool_instances: 20
innodb_io_capacity: 4000
# Additional Suggestions:
max_connections: 500 (Based on CPU cores)
innodb_flush_method = O_DIRECT (To avoid double buffering with the OS)
innodb_log_file_size = 7GB (25% of total memory for optimal recovery speed)
This script is a MySQL optimization tool that analyzes system hardware and generates recommended configuration settings. Here's a breakdown of what it does:
-
System Information Collection:
CPU_CORES=$(nproc) MEM_TOTAL=$(awk '/MemTotal/ {print $2}' /proc/meminfo) NUMA_NODES=$(lscpu | awk '/NUMA node\(s\)/ {print $3}')
- Gets the number of CPU cores
- Retrieves total memory from
/proc/meminfo
- Checks for NUMA node configuration
-
Memory Calculations:
MEM_TOTAL_GB=$((MEM_TOTAL / 1048576)) INNODB_BUFFER_POOL_SIZE=$((MEM_TOTAL * 80 / 100 / 1048576))
- Converts memory from KB to GB
- Calculates InnoDB buffer pool size (set to 80% of total memory)
-
Buffer Pool Instance Optimization:
if [ "$INNODB_BUFFER_POOL_SIZE" -le "$CPU_CORES" ]; then INNODB_BUFFER_POOL_INSTANCES=$INNODB_BUFFER_POOL_SIZE else INNODB_BUFFER_POOL_INSTANCES=$CPU_CORES fi
- Sets buffer pool instances based on either buffer pool size or CPU cores, whichever is smaller
-
I/O Capacity Calculation:
INNODB_IO_CAPACITY=$((CPU_CORES * 200))
- Sets I/O capacity based on CPU cores (200 per core)
-
Output Generation:
- Creates a file named
mysql_optimization_suggestions.txt
- Outputs all recommendations to both the file and screen using
tee
- Includes settings for:
- InnoDB buffer pool size
- Buffer pool instances
- I/O capacity
- NUMA configuration (if applicable)
- Max connections (25 per CPU core)
- InnoDB flush method
- Log file size (25% of total memory)
- Creates a file named
This script is particularly useful for:
- Initial MySQL server setup
- Performance optimization
- Hardware-specific configuration
- Generating documented configuration recommendations
The suggestions are based on common best practices for MySQL performance optimization, taking into account the specific hardware configuration of the system where the script is run.