System Preparation and Configuration
Building MySQL 5.6 from source requires careful system tuning to ensure optimal database performance. This guide covers the essential configuration steps needed before installation.
Storage Configuration
For production environments, RAID 10 provides the best balance between read performance, write performance, and data redundancy. Physical servers with SSD storage are strongly recommended for database workloads.
Recommended RAID controller settings:
- Write Policy: WriteBack (WB)
- Read Policy: ReadAdaptive or normal — avoid ReadAhead on database servers
- IO Policy: Direct
File and Process Limits
Database servers require high file descriptor limits to handle numerous concurrent connections. Add the following entries to the system security configuration:
# vim /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
NUMA Optimization
Non-Uniform Memory Access (NUMA) can cause memory allocation issues on database servers. Disable it through one of these methods:
- BIOS settings (recommended for physical servers)
- Kernel parameter: Add
numa=offto the kernel line in/etc/grub.conf
Kernel Parameters
Tune the kernel for database workloads by modifying the system control configuration:
# vim /etc/sysctl.conf
fs.file-max = 65536
vm.min_free_kbytes = 1048576
vm.swappiness = 10
vm.dirty_ratio = 10
vm.vfs_cache_pressure=150
vm.dirty_ratio = 10
vm.overcommit_memory = 1
vm.drop_caches = 1
kernel.panic = 60
net.core.somaxconn = 8192
net.ipv4.tcp_max_syn_backlog = 8192
vm.zone_reclaim_mode = 0
IO Scheduler Configuration
The deadline or noop scheduler algorithms perform best for database workloads. Apply the settings temporarily and persistently:
# Apply immediately (replace sda with your device name)
echo {SCHEDULER-NAME} > /sys/block/{DEVICE-NAME}/queue/scheduler
# Make permanent via /etc/rc.local
echo {SCHEDULER-NAME} > /sys/block/{DEVICE-NAME}/queue/scheduler
Transparent Huge Pages
Disable transparent huge pages (THP) to prevent potential memory allocation issues:
# Disable THP immediately
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
# Disable THP persistently via /etc/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
MySQL Installation
Installing Dependencies
Install all required compilation tools and libraries:
# yum install -y nc rsync screen lrzsz expect cmake make gcc gcc-c++ \
perl bison bc thread libaio libaio-devel zlib zlib-devel nc \
python python-devel cmake ncurses-devel sysstat \
perl-ExtUtils-CBuilder perl-CPAN perl-Log-Dispatch \
perl-Config-Tiny perl-Parallel-ForkManager perl-Config-IniFiles
Creating System User
Create a dedicated system user for MySQL operations:
# groupadd mysql
# useradd -g mysql mysql
Directory Structure
Plan and create the directory structure for MySQL data, logs, and runtime files:
# mkdir -p /data/my3306
# mkdir -p /u02/my3306/data
# mkdir -p /u02/my3306/log/iblog
# mkdir -p /u02/my3306/log/binlog
# mkdir -p /u02/my3306/tmp
# mkdir -p /u02/my3306/run
# chown -R mysql:mysql /u02/my3306
Compiling from Source
Extract the source archive and compile with configuration options tailored to your requirements:
# tar zxvf mysql-5.6.35.tar.gz
# cd mysql-5.6.35/
# cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql5.6 \
-DDEFAULT_CHARSET=utf8mb4 \
-DEXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1
# make
# make install
Configuration File
Create the MySQL configuration file with production-appropriate settings:
# vim /u02/my3306/my.cnf
[client]
port=3306
default-character-set=utf8mb4
socket=/u02/my3306/mysql.sock
[mysql]
pid_file=/u02/my3306/run/mysqld.pid
default-character-set=utf8mb4
no-auto-rehash
[mysqld]
default-time-zone = '+8:00'
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
autocommit=1
general_log=off
explicit_defaults_for_timestamp=true
# GTID Configuration
gtid-mode = ON
enforce_gtid_consistency = 1
log_slave_updates = 1
# System Settings
basedir=/usr/local/mysql5.6
datadir=/u02/my3306/data
max_allowed_packet=1g
max_connections=3000
max_user_connections=2800
open_files_limit=65535
pid_file=/u02/my3306/run/mysqld.pid
port=3306
server_id=20633306
skip_name_resolve=ON
socket=/u02/my3306/run/mysql.sock
tmpdir=/u02/my3306/tmp
query_cache_type = 0
query_cache_size = 0
read-buffer-size = 32M
# Binary Log Settings
log_bin=/u02/my3306/log/binlog/binlog
binlog_cache_size=32768
binlog_format=row
expire_logs_days=7
log_slave_updates=ON
max_binlog_cache_size=2147483648
max_binlog_size=524288000
sync_binlog=100
# Logging Configuration
log_error=/u02/my3306/log/error.log
slow_query_log_file=/u02/my3306/log/slow.log
log_queries_not_using_indexes=0
slow_query_log=1
log_slave_updates=ON
log_slow_admin_statements=1
long_query_time=1
log-queries-not-using-indexes = 0
# Relay Log Settings
relay_log=/u02/my3306/log/relaylog
relay_log_index=/u02/my3306/log/relay.index
relay_log_info_file=/u02/my3306/log/relay-log.info
# Replication Settings
slave_load_tmpdir=/u02/my3306/tmp
slave_skip_errors=OFF
# InnoDB Settings
innodb_data_home_dir=/u02/my3306/log/iblog
innodb_log_group_home_dir=/u02/my3306/log/iblog
innodb_adaptive_flushing=ON
innodb_adaptive_hash_index=ON
innodb_autoinc_lock_mode=1
innodb_buffer_pool_instances=8
innodb_change_buffering=inserts
innodb_checksums=ON
innodb_buffer_pool_size= 4G
innodb_data_file_path=ibdata1:1024M:autoextend
innodb_doublewrite=ON
innodb_file_format=Barracuda
innodb_file_per_table=ON
innodb_flush_log_at_trx_commit=1
innodb_flush_method=O_DIRECT
innodb_io_capacity=1000
innodb_lock_wait_timeout=10
innodb_log_buffer_size=67108864
innodb_log_file_size=1048576000
innodb_log_files_in_group=4
innodb_max_dirty_pages_pct=75
innodb_open_files=60000
innodb_purge_threads=1
innodb_read_io_threads=4
innodb_stats_on_metadata=OFF
innodb_support_xa=ON
innodb_use_native_aio=OFF
innodb_write_io_threads=10
[mysqld_safe]
datadir=/u02/my3306/data
# chown -R mysql.mysql /u02
Database Initialization
Initialize the MySQL data directory with the specified configuration:
# cd /usr/local/mysql5.6
# ./scripts/mysql_install_db --defaults-file=/u02/my3306/my.cnf \
--datadir=/u02/my3306/data --user=mysql
Starting the Database
Launch the MySQL server process:
su - mysql -c "/usr/local/mysql5.6/bin/mysqld_safe --defaults-file=/u02/my3306/my.cnf &"
Post-Installation Security
Secure the installation by removing anonymous accounts, setting the root password, and removing the test database:
# /usr/local/mysql5.6/bin/mysql -uroot -p -S /u02/my3306/run/mysql.sock
# Execute these commands in MySQL client:
delete from mysql.user where user='';
set password=password('xxxxxxx');
drop database test;
Accessing the Database
/usr/local/mysql5.6/bin/mysql -uroot -p -S /u02/my3306/run/mysql.sock
Shutting Down the Daatbase
/usr/local/mysql5.6/bin/mysqladmin -uroot -p -S /u02/my3306/run/mysql.sock shutdown