首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

Linux 環境 MySQL 5.1 二進制免編譯安裝全流程:用戶、權限、配置、啟動、開機自啟

     編輯:MySQL 資料庫相關知識

本文記錄 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!'

六、註冊系統服務(SysVinit / systemd 兼容)


# 複製啟動腳本
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 socketSocket 文件路徑不匹配、服務未啟動對齊 my.cnf socket 參數、確認進程存在
中文亂碼配置文件未設置 character-set-server=utf8mb4修改 /etc/my.cnf 重啟

二進制包免編譯安裝,核心在於「專用用戶、數據目錄權限、配置文件匹配內存、服務註冊」。按流程操作可實現標準化、可複現的部署。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved