本文以 CentOS 7.2 為示範環境,說明 MariaDB 的兩種安裝方式與設定檔的運作邏輯。開始之前,請先關閉 SELinux,並在 iptables 中放行 3306 埠(本次示範直接清空規則)。
iptables -F vim /etc/selinux/config # SELINUX=enforcing ## 註解掉 # SELINUXTYPE=targeted ## 註解掉 SELINUX=disabled ## 新增此行
先設定 yum 源(此處使用阿里雲鏡像站),再安裝伺服器端與客戶端:
wget https://mirrors.aliyun.com/repo/Centos-7.repo -O /etc/yum.repos.d/CentOS-Base.repo yum install MariaDB-server MariaDB-client
接著編輯伺服器端設定檔,以下為常見的優化項目,可依需求選用:
vim /etc/my.cnf.d/server.cnf [mysqld] innodb_file_per_table=ON # 使用獨立表空間模式(建議) skip_name_resolve=ON # 關閉域名反解(可選)
啟動並設定開機自啟,最後執行安全初始化:
systemctl start mariadb.service systemctl enable mariadb.service mysql_secure_installation # 設定 root 密碼、移除匿名帳號、禁止 root 遠端登入、刪除測試庫 mysql -uroot -hlocalhost -p'PASSWORD'
命名格式為 mariadb-版本-作業系統.tar.gz,官方下載點為 https://downloads.mariadb.org/,請務必確認與你的機器相符。以下以 5.5 版為例,通常會安裝到 /usr/local/mysql(許多平台的預設位置),並建立軟連結以便日後切換版本。
確保使用正確的 my.cnf:MariaDB 會依序尋找 /etc/my.cnf、/etc/mysql/my.cnf、~/.my.cnf。若系統中殘留舊的設定檔,可能導致非預期的行為。建議的做法是在家目錄建立自己的 ~/.my.cnf,並以 --defaults-file=~/.my.cnf 指定;此選項必須是命令列的第一個參數。
以 root 安裝到 /usr/local/mysql:
groupadd mysql useradd -g mysql mysql cd /usr/local tar -zxvpf /path-to/mariadb-VERSION-OS.tar.gz ln -s mariadb-VERSION-OS mysql cd mysql ./scripts/mysql_install_db --user=mysql chown -R root . chown -R mysql data
以 ln -s 建立軟連結,日後要測試、升級或降級多個版本都會方便很多。若是以 MariaDB 取代既有 MySQL,通常不必先關閉 MySQL,mysql_install_db 會從既有的 my.cnf 中找到資料目錄。
啟動方式:
./bin/mysqld_safe --user=mysql & # 或 ./bin/mysqld_safe --defaults-file=~/.my.cnf --user=mysql &
把 bin 目錄加入 PATH,方便直接呼叫 mysql、mysqldump 等工具(也可寫入 .bashrc 或 .bash_profile 永久生效):
export PATH=$PATH:/usr/local/mysql/bin/
安裝到任意目錄:把 /usr/local 換成自訂路徑即可。
cd 你的目標目錄 gunzip < /path-to/mariadb-VERSION-OS.tar.gz | tar xf - ln -s mariadb-VERSION-OS mysql cd mysql ./scripts/mysql_install_db --defaults-file=~/.my.cnf ./bin/mysqld_safe --defaults-file=~/.my.cnf &
若有 GNU tar,也可直接 tar xfz 檔案。
設定開機自啟:
cp support-files/mysql.server /etc/init.d/mysql.server
安裝到這一步後,請務必為所有不可信的來源設定密碼以降低風險。
MariaDB 使用 ini 風格設定檔,會依固定順序讀取多個檔案:
my_print_defaults Default options are read from the following files in the given order: /etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf
其中 /etc/my.cnf 由 mariadb-libs 產生,/etc/my.cnf.d/server.cnf 由 mariadb-server 產生,client.cnf 則由 mariadb 產生。同一參數在不同檔案中出現時,後讀取的值才是最終生效值。
相關選項:
--defaults-file=file_name # 改為只讀取指定檔案 --defaults-extra-file=path # 在預設檔案之外再額外載入一個
參數寫法有兩種等價形式:innodb_file_per_table 與 innodb-file-per-table。設定段落則依程式而異,常見有 [mysqld]、[mysqld_safe]、[server]、[mysql]、[mysqldump]、[client] 等。
伺服器端:mysqld_safe、mysqld_multi;客戶端:mysql、mysqldump、mysqladmin;工具程式:myisampack 等。
基本語法:
mysql [options] db_name
常用選項:
-h, --host=主機 # 伺服器位址
-u, --user=使用者 # 帳號
-p, --password[=密碼] # 密碼
-P, --port=埠號 # 連接埠
--protocol={TCP|SOCKET|PIPE|MEMORY}
-S, --socket=路徑
-D, --database=名稱
-C, --compress # 壓縮傳輸
-e, --execute=語句 # 非互動模式執行
-E, --vertical # 結果縱向顯示
本機連線預設走 SOCKET(Windows 為 PIPE / MEMORY),使用非回環位址時則走 TCP。
常用客戶端命令:
help (\?) 顯示說明 clear (\c) 清除已輸入的內容 connect (\r) 重新連線 delimiter (\d) 設定語句分隔符 ego (\G) 執行並縱向顯示結果 exit (\q) 離開 go (\g) 送出命令 source (\.) 執行 SQL 腳本檔 status (\s) 檢視伺服器狀態 system (\!) 執行 shell 命令 tee (\T) 將輸出寫入檔案 use (\u) 切換資料庫 charset (\C) 切換字元集 warnings (\W) / nowarning (\w) 開關警告顯示
批次執行腳本:
mysql [options] [DATABASE] < /PATH/TO/SOME_SQL_SCRIPT
檢視系統變數與狀態:
SHOW [GLOBAL | SESSION] VARIABLES [like_or_where]; SHOW [GLOBAL | SESSION] STATUS [like_or_where]; SELECT @@[global.|session.]system_var_name;
變數分為全域(作為所有工作階段預設)與工作階段層級(子階段建立時從全域繼承)。修改方式有兩種:執行期以 SET 調整(global 只影響新建工作階段,session 立即生效),或改設定檔後重啟生效。
SET [GLOBAL | SESSION] system_var_name = expr; SET [@@global. | @@session. | @@]system_var_name = expr;
建議開機前調整的參數:
innodb_file_per_table=ON # 啟用獨立表空間 skip_name_resolve=ON # 關閉域名反解 sql_safe_updates=ON # 開啟安全更新模式(不可直接在設定檔定義)