本文記錄 Linux 下 MySQL 5.1 通用二進制包(免編譯)標準化安裝流程,含用戶創建、目錄佈署、權限初始化、配置文件選擇、服務註冊、防火牆放行,適合生產環境標準化部署。
# 文件名格式:mysql-VERSION-OS.tar.gz
cd /usr/local
gunzip < /path/to/mysql-VERSION-OS.tar.gz | tar xvf -
# 或 GNU tar 一步到位
tar zxvf /path/to/mysql-VERSION-OS.tar.gz
# 建立軟鏈接便於版本切換
ln -s mysql-VERSION-OS mysql
groupadd mysql
useradd -r -g mysql -s /bin/false mysql # -r 系統用戶,無登錄殼
cd /usr/local/mysql
chown -R mysql .
chgrp -R mysql .
scripts/mysql_install_db --user=mysql # 初始化系統表、權限表
chown -R root .
chown -R mysql data # 數據目錄歸 mysql 用戶
# 注意:命令中的 "." 不可省略
# 根據內存選擇模板
cp support-files/my-medium.cnf /etc/my.cnf
# 可選模板:my-small.cnf / my-medium.cnf / my-large.cnf / my-huge.cnf
# 前台驗證啟動
bin/mysqld_safe --user=mysql &
# 正常應見:Starting mysqld daemon with databases from /usr/local/mysql/data
# 設置 root 密碼
/usr/local/mysql/bin/mysqladmin -uroot password 'StrongPass123!'
# 複製啟動腳本
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod 700 /etc/init.d/mysqld
# SysVinit 註冊
chkconfig --add mysqld
chkconfig --level 345 mysqld on
# systemd(現代發佈版)可改用官方 RPM/DEB 包自帶 unit 文件
# 啟動/停止/重啟
service mysqld start
service mysqld stop
service mysqld restart
# 驗證端口
netstat -atln | grep 3306
# 防火牆放行 3306/tcp(iptables/firewalld/ufw/云安全組)
shell> groupadd mysql
shell> useradd -g mysql mysql
shell> cd /usr/local
shell> gunzip < /PATH/TO/MYSQL-VERSION-OS.tar.gz | tar xvf -
shell> ln -s FULL-PATH-TO-MYSQL-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
| 現象 | 原因 | 對策 |
|---|---|---|
mysqld ended 啟動即退出 | 權限錯誤(data 目錄非 mysql 用戶)、配置文件路徑錯、端口被佔用 | 查看 error.log(my.cnf 中 log-error 指定) |
連接報 Can't connect to local MySQL server through socket | Socket 文件路徑不匹配、服務未啟動 | 對齊 my.cnf socket 參數、確認進程存在 |
| 中文亂碼 | 配置文件未設置 character-set-server=utf8mb4 | 修改 /etc/my.cnf 重啟 |
二進制包免編譯安裝,核心在於「專用用戶、數據目錄權限、配置文件匹配內存、服務註冊」。按流程操作可實現標準化、可複現的部署。