mysql的日常維護

2021-08-16 15:51:47 字數 2559 閱讀 4608

1.許可權管理

mysql許可權管理基於主機名、使用者名稱和資料庫表,可以根據不同的主機名、使用者名稱和資料庫表分配不同的許可權。

mysql的許可權資訊儲存在資料庫的user、db、host、tables_priv、cloumns_priv和proc_priv表中,通常使用grant和revoke進行許可權分分配。

分配賬號:

(1)主機192.168.61.147需要遠端訪問mysql伺服器的account.users表,許可權為select和update,可以使用如下命令:

grant select,update on account.users to [email protected] identified by 'pass123456';

flush privileges;

建立成功後,檢視資料庫表的變化

select * from user where user = 'user1'\g

select * from db where user = 'user1'\g

mysql許可權驗證按照 user -> db -> tables_priv -> columns_priv檢查的順序,如果user表中的對應許可權為y,則不再檢查後面表中的許可權。

(2)檢視或修改賬號許可權

如需檢視當前使用者的許可權,可使用show grants for命令,如:

show grants for [email protected] \g

mysql使用者登入成功後許可權載入到記憶體中,此時如果在另乙個會話中更改了該賬戶的許可權並不會影響之前會話中使用者的許可權,如需要使用最新的許可權,需要重新登入。

(3)**賬戶許可權

操作**的賬戶需要revoke許可權。

revoke insert on *.* from test3@'%';

revoke all on *.* from test3@'%';

賬戶所有許可權**後使用者仍可以連線mysql伺服器,如需徹底刪除使用者,使用drop命令。

drop user test3@'%';

2.日誌管理

可通過日誌追蹤mysql伺服器的執行狀態,及時發現服務執行過程中的各種問題。

(1)二進位制日誌

記錄了所有的ddl dml但不包括查詢語句。

啟用二進位制日誌:通過在配置檔案中新增「--log-bin=[file-name]」選項指定二進位制檔案存放位置。

檢視二進位制日誌:通過mysql提供的工具mysqlbinlog。如:

mysqlbinlog mysql-bin.00005|cat -n 

刪除指定序號之前的二進位制日誌:

purge binary logs to 'mysql-bin.010';

刪除指定時間之前的二進位制日誌

purge binary logs before '2-18-03-01 22:13:32';

配置檔案中指定二進位制檔案的保留天數:expire_log_days=# .

sql命令列設定二進位制檔案的保留天數:set global expire_log_days = 7;

(2)操作錯誤日誌

記錄了mysql啟動、執行及停止過程中的相關異常資訊,在mysql故障定位方面有重要的作用。

配置檔案中設定:

錯誤日誌存放位置:--log-error=[file-name],預設在mysql的datadir目錄下。

(3)訪問日誌

記錄了所有客戶端發起的連線、查詢和更新語句,由於記錄所有操作,建議在資料庫繁忙時關閉此設定。

配置檔案中設定:

配置檔案設定:--log=[file-name] 指定訪問日誌的位置,預設為主機名作為檔名,存放在資料目錄中

可在登入mysql後通過設定變數

啟用此日誌:set global general_log=on;

查詢日誌位置:show variables like '%general_log%';

關閉日誌:set global general_log=off;

(4)慢查詢日誌

記錄了執行時間超過引數long_query_time所設定的sql語句日誌,對sql審核和開發者發現效能問題具有重要意義。

配置檔案中設定:slow_query_log 用來指定是否開啟慢查詢。如果沒有指定檔名,預設hostname-slow.log,並放在資料目錄中。

log-slow-queries指定日誌檔案的路徑

3.備份與恢復 

匯出整個資料庫   mysqldump -u root test>test.sql 

匯出乙個表

mysql -u root test tbl_2 > test.tbl_2.sql 

只匯出表結構     mysql -u root -d --add-drop-table test>test.sql       //-d 沒有資料, --add-drop-table 在create前,先drop table 

恢復資料庫

mysql -u root test   mysql -u root test 登入後,source /root/test.sql 

4.mysql複製

主從複製好處:健壯、負載均衡、資料備份、

複製搭建過程:待補充

mysql日常維護

檢視mysql執行程序 mysql show processlist 檢視innodb引擎運 況 mysql show engine innodb status g 檢視mysql引數情況 mysql show global variables like innodb 檢視mysql執行狀態 mys...

mysql日常維護工具 備份

檢視mysql當前的字符集設定情況 root cong11 mysql uroot p123456 mysql show variables like character set 名詞解釋 character set client 客戶端請求資料的字符集 character set connecti...

HBASE日常維護

一,基本命令 建表 create testtable coulmn1 coulmn2 也可以建表時加coulmn的屬性如 create testtable 其中的屬性有versions 設定歷史版本數,ttl 過期時間,compression 壓縮方式,當配置lzo的情況 刪除表 drop test...