萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 資料庫 >> MySQL 教學

MySQL 客戶端密碼輸入安全指南:四種方式風險分析與最佳實踐

四種密碼輸入方式風險等級

方式指令範例風險等級風險說明
1. 命令列明文mysql -u user -ppassword🔴 極高ps -ef 可見、Shell 歷史紀錄、進程表洩露
2. 互動式提示mysql -u user -p🟡 中終端不顯示,但僅適用互動模式,腳本無法使用
3. 選項檔~/.my.cnf [client] password=...🟢 低檔案權限 600,僅擁有者可讀,自動化腳本首選
4. 環境變數MYSQL_PWD=password🔴 極高ps -e 可見環境變數,官方文檔明確不建議

詳細分析與防護

方式一:命令列明文 - 為什麼危險?

$ mysql -u root -pSecret123 dbname &
$ ps -f
UID  PID  PPID  C STIME TTY  TIME CMD
root 1234  1000  0 10:00 pts/0 0:00 mysql -u root -pSecret123 dbname
# 密碼直接曝光!

雖然 MySQL 客戶端啟動後會嘗試用 0 覆寫 argv,但存在競態條件視窗,且背景執行時更明顯。

方式二:互動式提示 - 適用場景限制

$ mysql -u root -p
Enter password: *********
# 終端不回顯,安全性較好
# 但:Shell 腳本無法自動化、首行輸入可能被誤讀為密碼

方式三:選項檔 ~/.my.cnf - 生產環境標準做法

# ~/.my.cnf
[client]
user = myuser
password = "Str0ng!P@ssw0rd"
host = 127.0.0.1
port = 3306

# 嚴格權限!
chmod 600 ~/.my.cnf
ls -l ~/.my.cnf
# -rw------- 1 user user 68 Oct 15 10:00 .my.cnf

MySQL 讀取優先順序:

  1. /etc/my.cnf (全域)
  2. /etc/mysql/my.cnf
  3. ~/.my.cnf (用戶)
  4. --defaults-extra-file= 指定檔案

後讀取的覆蓋先讀取的同名選項。

方式四:環境變數 MYSQL_PWD - 官方棄用

"This method of specifying your MySQL password is insecure and should not be used." — MySQL 官方文檔

進階:mysqldump / mysqlpump 自動化備份腳本範例

#!/bin/bash
# backup.sh - 安全備份腳本

CONFIG_FILE="/etc/mysql/backup.cnf"  # 僅 root 可讀,600 權限
OUT_DIR="/backup/mysql"
DATE=$(date +%F_%H-%M)

mkdir -p "$OUT_DIR"

# 使用 --defaults-extra-file 指定專用配置,不依賴 ~/.my.cnf
mysqldump --defaults-extra-file="$CONFIG_FILE" \
    --single-transaction --routines --triggers --events \
    --all-databases | gzip > "$OUT_DIR/full_$DATE.sql.gz"

# 保留 7 天
find "$OUT_DIR" -name 'full_*.sql.gz' -mtime +7 -delete

Docker / 容器環境最佳實踐

# docker-compose.yml
services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password  # Docker Secrets
    secrets:
      - db_root_password

secrets:
  db_root_password:
    file: ./secrets/db_root_password.txt  # 構建時注入,不進鏡像

歷史紀錄檔清理

MySQL 客戶端將執行指令記錄在 ~/.mysql_history,包含 SET PASSWORD 等敏感 SQL。

# 禁用歷史記錄
export MYSQL_HISTFILE=/dev/null
# 或
ln -sf /dev/null ~/.mysql_history

總結建議

  1. 生產自動化:專用 .cnf 檔 + chmod 600 + --defaults-extra-file
  2. 互動管理:mysql -u user -p 手動輸入
  3. 嚴禁:命令列明文、環境變數、寫入 Shell 歷史
  4. 容器化:Docker Secrets / Kubernetes Secrets
copyright © 萬盛學電腦網 all rights reserved