當需在多台 Linux 伺服器部署相同版本的 MySQL 時,手動編譯極為繁瑣。本文提供一個完整的 install_mysql.sh 腳本,實現從下載原始碼、編譯安裝、初始化資料目錄、生成配置檔、啟動服務到驗證安裝的全流程自動化。
/home/longxibendi/mysql/usr/local/mysql/home/longxibendi/mysql/3309/ (含 data、binlog、relaylog)/home/longxibendi/mysql/3309/my.cnf3309longxibendi#!/bin/bash
# 建立目錄並下載原始碼
mkdir -p /home/longxibendi/mysql/usr/local/mysql
cd /home/longxibendi/mysql
wget https://downloads.mysql.com/archives/mysql-5.1/mysql-5.1.45.tar.gz
tar zxvf mysql-5.1.45.tar.gz
cd mysql-5.1.45
# 配置編譯參數(關鍵:靜態連結、字符集、儲存引擎、SSL、分區等)
./configure \
--prefix=/home/longxibendi/mysql/usr/local/mysql \
--without-debug --without-bench \
--enable-thread-safe-client --enable-assembler --enable-profiling \
--with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static \
--with-charset=latin1 --with-extra-charset=utf8,gbk \
--with-innodb --with-mysqld-user=longxibendi \
--without-embedded-server --with-server-suffix=community \
--with-unix-socket-path=/home/longxibendi/mysql/usr/local/mysql/sock/mysql.sock \
--with-extra-charsets=complex --with-big-tables --with-readline --with-ssl \
--with-plugins=partition,heap,innobase,myisam,myisammrg,csv
# 並行編譯安裝 (自動偵測 CPU 核心數)
make -j `cat /proc/cpuinfo | grep 'model name' | wc -l` && make install
# 建立資料/日誌目錄並設定權限
chmod +w /home/longxibendi/mysql/usr/local/mysql
mkdir -p /home/longxibendi/mysql/3309/{data,binlog,relaylog}
# 初始化系統資料庫
/home/longxibendi/mysql/usr/local/mysql/bin/mysql_install_db \
--basedir=/home/longxibendi/mysql/usr/local/mysql \
--datadir=/home/longxibendi/mysql/3309/data --user=longxibendi
# 生成 my.cnf (含複寫、InnoDB、緩衝區、二進位日誌、Relay Log 等完整參數)
cat > /home/longxibendi/mysql/3309/my.cnf <<'EOF'
[client]
character-set-server = utf8
port = 3309
socket = /home/longxibendi/mysql/usr/local/mysql/sock/mysql.sock
[mysqld]
character-set-server = utf8
replicate-ignore-db = mysql,test,information_schema
user = mysql
port = 3309
socket = /home/longxibendi/mysql/usr/local/mysql/sock/mysql.sock
basedir = /home/longxibendi/mysql
datadir = /home/longxibendi/mysql/3309/data
log-error = /home/longxibendi/mysql/3309/mysql_error.log
pid-file = /home/longxibendi/mysql/3309/mysql.pid
open_files_limit = 10240
back_log = 600
max_connections = 5000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 16M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 300
query_cache_size = 20M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 20M
max_heap_table_size = 20M
long_query_time = 3
log-slave-updates
log-bin = /home/longxibendi/mysql/3309/binlog/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 20M
relay-log-index = /home/longxibendi/mysql/3309/relaylog/relaylog
relay-log-info-file = /home/longxibendi/mysql/3309/relaylog/relaylog
relay-log = /home/longxibendi/mysql/3309/relaylog/relaylog
expire_logs_days = 30
key_buffer_size = 10M
read_buffer_size = 1M
read_rnd_buffer_size = 6M
bulk_insert_buffer_size = 4M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 20M
myisam_repair_threads = 1
myisam_recover
interactive_timeout = 120
wait_timeout = 120
skip-name-resolve
server-id = 1
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 20M
innodb_data_file_path = ibdata1:56M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 20M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
[mysqldump]
quick
max_allowed_packet = 32M
EOF
# 修正已知 Bug (errmsg.sys 缺失)
mkdir -p /home/longxibendi/mysql/share/mysql/english/
cp /home/longxibendi/mysql/usr/local/mysql/share/mysql/english/errmsg.sys \
/home/longxibendi/mysql/share/mysql/english/
# 啟動服務並驗證
/bin/sh /home/longxibendi/mysql/usr/local/mysql/bin/mysqld_safe \
--defaults-file=/home/longxibendi/mysql/3309/my.cnf 2>&1 > /dev/null &
/home/longxibendi/mysql/usr/local/mysql/bin/mysql -u root -p \
-S /home/longxibendi/mysql/usr/local/mysql/sock/mysql.sock -P3309 -h127.0.0.1 -e "show databases;"
chmod +x install_mysql.sh./install_mysql.shSHOW DATABASES;,若輸出 information_schema、mysql、test 即代表成功。--with-plugins:靜態編譯常用儲存引擎,避免運行時動態載入。--with-mysqld-ldflags=-all-static:生成靜態連結的 mysqld,提升部署可攜性。binlog_format = MIXED:混合複寫模式,兼顧效率與一致性。skip-name-resolve:跳過 DNS 解析,提升連線速度。此腳本可作為企業標準化部署基礎,根據實際硬體資源調整緩衝區大小、連線數等參數即可投入生產環境。