編譯安裝全流程
# 1. 解壓與用戶準備
tar zxvf mysql-4.0.14.tar.gz -C /setup
cd /setup/mysql-4.0.14
groupadd mysql
useradd mysql -g mysql -M -s /bin/false
# 2. 核心配置參數詳解
./configure --prefix=/web/mysql \
--without-debug # 移除調試符號,減小體積提升性能
--with-extra-charsets=gb2312 # 靜態編入 gb2312,避免運行時加載
--enable-assembler # 字符函數使用汇編優化
--without-isam # 移除 ISAM(舊引擎,平台相關)
--without-innodb # 移除 InnoDB(不需事務時節省資源)
--with-pthread # 強制使用 POSIX 線程庫
--enable-thread-safe-client # 線程安全客戶端庫
--with-client-ldflags=-all-static \
--with-mysqld-ldflags=-all-static # 純靜態鏈接,部署無依賴
--with-raid # 啟用 RAID 支持(數據文件條帶化)
# 3. 編譯安裝
make && make install
# 4. 初始化與配置
scripts/mysql_install_db
cp support-files/my-medium.cnf /etc/my.cnf # 根據內存選擇 large/medium/small
cp support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
# 5. 目錄權限
cd /web
chmod 750 mysql -R
chgrp mysql mysql -R
chown mysql mysql/var -R
# 6. 二進制精簡與服務註冊
cd /web/mysql/libexec
cp mysqld mysqld.old
strip mysqld # 剝離符號表,進一步減小體積
chkconfig --add mysqld
chkconfig --level 345 mysqld on
service mysqld start
# 7. 驗證
netstat -atln | grep 3306
ln -s /web/mysql/bin/mysql /sbin/mysql
ln -s /web/mysql/bin/mysqladmin /sbin/mysqladmin
mysqladmin -uroot password "yourpass"
mysql -uroot -p -e "USE mysql; DELETE FROM user WHERE Password=''; FLUSH PRIVILEGES;"
Perl DBI/DBD::mysql 安裝
# 1. DBI
tar zxvf DBI-1.38.tar.gz
cd DBI-1.38
perl Makefile.PL && make && make test && make install
# 兼容性鏈接(部分腳本寫死 /usr/local/bin/perl)
ln -s /usr/bin/perl /usr/local/bin/perl
# 2. DBD::mysql
tar zxvf DBD-mysql-2.9002.tar.gz
cd DBD-mysql-2.9002
# 關鍵:鏈接 mysql_config 讓 Makefile.PL 自動探測庫/頭文件
ln -s /web/mysql/bin/mysql_config /sbin/mysql_config
perl Makefile.PL && make && make install
# make test 常失敗但不影響使用
# 3. 基準測試(後台運行)
perl /web/mysql/sql-bench/run-all-tests --user=root --password="yourpass" &
常用管理命令速查
| 工具 | 命令/參數 | 作用 |
| mysqld | --log | 啟用通用查詢日志 |
--log-update | 啟用更新日志(舊版 binlog 前身) |
--log-long-format | 長格式日志(含未使用索引查詢) |
| mysql | GRANT ALL ON db.* TO user IDENTIFIED BY "pass" | 創建用戶並授權 |
REVOKE ALL ON db FROM user | 回收權限 |
mysql -uroot -p --one-database db < file.sql | 指定庫導入 SQL 文件 |
| mysqladmin | flush-logs | 切換日志文件(舊文件歸檔) |
| mysqldump | --add-drop-table | 導出含 DROP TABLE IF EXISTS |
--quick | 流式導出,不緩存結果集 |
--opt | 綜合優化組合(推薦默認開啟) |
| myisamchk | --extend-check | 擴展檢查(最全) |
--medium-check | 折中速度檢查 |
--recover | 修復表 |
--safe-recover | 安全修復(較慢) |
--force | 強制清理臨時文件 |
--quick | 僅按索引修復 |
管理工具推薦
- phpMyAdmin:Web 界面,需 PHP 環境
- MySQLCC:Linux 圖形客戶端,Windows 也支持
現代視角補充
- 4.0 系列已停止維護,生產環境務必升級至 5.7/8.0 或 MariaDB 10.x
- 靜態編譯 (
-all-static) 現代發行版 glibc 兼容性較差,推薦動態鏈接 + 容器化部署
--without-innodb 現已罕見,InnoDB 是默認且核心引擎
- Perl DBI/DBD 仍活躍,但新項目多用 Python (PyMySQL/SQLAlchemy) 或 Go (go-sql-driver/mysql)