mysql-5.6.51.tar.gz)yum -y install gcc gcc-c++ make cmake ncurses-devel bison \ libaio-devel perl-Data-Dumper openssl-devel \ wget tar gzip
關鍵:libaio-devel 為 InnoDB 異步 I/O 必需,缺失會導致啟動報錯 libaio.so.1 not found。
groupadd -r mysql
useradd -r -g mysql -s /sbin/nologin -M mysql
# 目錄規劃 (掛載獨立存儲卷)
mkdir -p /data/mysql/{data,binlog,relaylog,tmp,log}
chown -R mysql:mysql /data/mysql
cd /usr/local/src wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.6.51.tar.gz # 建議驗證 GPG 簽名 tar zxf mysql-5.6.51.tar.gz cd mysql-5.6.51
cmake . \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.51 \ -DMYSQL_DATADIR=/data/mysql/data \ -DMYSQL_TCP_PORT=3306 \ -DDEFAULT_CHARSET=utf8mb4 \ -DDEFAULT_COLLATION=utf8mb4_unicode_ci \ -DSYSCONFDIR=/etc/mysql \ -DMYSQL_UNIX_ADDR=/data/mysql/tmp/mysql.sock \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DWITH_SSL=system \ -DWITH_ZLIB=system \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_DEBUG=0 make -j$(nproc) make install
# 軟鏈接便於版本切換 ln -s /usr/local/mysql-5.6.51 /usr/local/mysql # 配置文件 mkdir -p /etc/mysql cat > /etc/mysql/my.cnf <<'EOF' [mysqld] basedir=/usr/local/mysql datadir=/data/mysql/data socket=/data/mysql/tmp/mysql.sock pid-file=/data/mysql/tmp/mysqld.pid port=3306 server_id=1001 gtid_mode=ON enforce_gtid_consistency=ON log_bin=/data/mysql/binlog/mysql-bin binlog_format=ROW expire_logs_days=14 max_binlog_size=500M sync_binlog=1 innodb_buffer_pool_size=4G innodb_log_file_size=512M innodb_log_files_in_group=2 innodb_file_per_table=1 innodb_flush_log_at_trx_commit=1 innodb_flush_method=O_DIRECT innodb_io_capacity=2000 innodb_read_io_threads=8 innodb_write_io_threads=8 max_connections=3000 open_files_limit=65535 table_open_cache=4000 thread_cache_size=500 skip-name-resolve character_set_server=utf8mb4 collation_server=utf8mb4_unicode_ci [mysql] default-character-set=utf8mb4 prompt=\\u@\\h:\\p:\\d> [client] port=3306 socket=/data/mysql/tmp/mysql.sock default-character-set=utf8mb4 EOF # 權限 chown -R mysql:mysql /usr/local/mysql-5.6.51 chown -R mysql:mysql /data/mysql # 初始化系統表 (5.6 專用) /usr/local/mysql/scripts/mysql_install_db \ --user=mysql \ --basedir=/usr/local/mysql \ --datadir=/data/mysql/data \ --defaults-file=/etc/mysql/my.cnf # 生成 SSL 憑證 (5.6 支持) /usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/data/mysql/data
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on # systemd 兼容 (CentOS 7) cat > /etc/systemd/system/mysqld.service <<'EOF' [Unit] Description=MySQL Server After=network.target [Service] Type=forking User=mysql Group=mysql ExecStart=/etc/init.d/mysqld start ExecStop=/etc/init.d/mysqld stop TimeoutSec=300 Restart=on-failure [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl start mysqld systemctl enable mysqld
| 報錯現象 | 原因 | 解決 |
|---|---|---|
cmake: command not found | 未安裝 CMake | yum -y install cmake3 並 ln -s /usr/bin/cmake3 /usr/bin/cmake |
Curses library not found | 缺 ncurses-devel | yum -y install ncurses-devel |
libaio.so.1 not found | 缺 libaio-devel | yum -y install libaio-devel |
Can't create/write to file '/data/mysql/tmp/mysqld.pid' | 目錄不存在/權限錯誤 | 腳本已預建目錄並 chown mysql |
ERROR 2002 (HY000): Can't connect to local MySQL server through socket | Socket 路徑不一致 | 客戶端指定 -S /data/mysql/tmp/mysql.sock 或配置 [client] socket |
InnoDB: mmap(137363456 bytes) failed; errno 12 | 內存不足 | 調小 innodb_buffer_pool_size 或增加 Swap/物理內存 |
9. 連接測試與基準驗證
總結
源碼編譯安裝 MySQL 5.6 雖步驟較多,但**完全可控、無多餘組件、參數全透明**。關鍵成功因子:依賴包預裝、目錄規劃分離、CMake 參數顯式化、配置文件基線化、systemd 服務管理、安全基線腳本化。建議將上述流程封裝為 Ansible Playbook 或 Shell 函數庫,實現「一鍵交付、版本可追溯、審計可合規」。