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

UNIX/Linux 系統下 MySQL 完整安裝與初始化教學

     編輯:MySQL 資料庫相關知識

本文詳細記錄在 Linux/UNIX 環境下,從原始碼編譯安裝 MySQL 3.22.25 的完整流程,包含目錄規劃、權限設定、資料庫初始化、啟動與安全性設定。

安裝前準備

# cd /usr/src
# tar -zvxf mysql-3.22.25-pc-linux-gnu-i686.tar.gz
# cd mysql-3.22.25-pc-linux-gnu-i686

編譯安裝(指定安裝路徑)

# ./configure --prefix=/usr/local/mysql
# make
# make install
  • --prefix:指定安裝目標目錄,建議獨立目錄方便日後管理。

初始化系統資料庫

# scripts/mysql_install_db

此步驟會在 /usr/local/mysql/var/ 建立兩個資料庫:

  • mysql:權限管理資料庫(核心)
  • test:測試用資料庫

注意: 若曾安裝過 MySQL,舊資料庫已存在則無需重複執行。

啟動 MySQL 服務

# cd /usr/local/mysql/bin
# ./safe_mysqld &

若需開機自啟,將上述指令加入 /etc/rc.d/rc.local。

根用戶密碼設定(關鍵安全步驟)

預設 root 無密碼,極度危險。請立即設定密碼:

# ./mysqladmin -u root password '新密碼'

驗證密碼生效

$ ./mysql -u root          # 應被拒絕
ERROR 1045: Access denied for user: 'root@localhost' (Using password: NO)

# ./mysql -u root -p       # 應提示輸入密碼
Password: ********
Welcome to the MySQL monitor...

重要: 即使是系統 root 用戶,未加 -p 參數同樣會被拒絕,確保密碼驗證機制有效。

服務管理常用指令

mysql.server start 或 /bin/sh -c 'cd /usr/local/mysql ; ./bin/safe_mysqld &'mysql.server stop 或 mysqladmin shutdownmysqladmin -u root -p shutdown
操作指令
啟動服務
停止服務
帶密碼停止

自動啟動設定

將啟動指令加入 /etc/rc.local:

/bin/sh -c 'cd /usr/local/mysql ; ./bin/safe_mysqld &'

安全性建議

  1. 安裝完成後立即修改 root 密碼。
  2. 不要使用系統 root 用戶啟動 MySQL 服務,建立專用 mysql 用戶。
  3. 定期備份 mysql 權限資料庫。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved