1. 程式人生 > >DAY5-MySQL日誌管理

DAY5-MySQL日誌管理

無法 時間 bsp glob position HA 錯誤 mixed now()

日誌文件


錯誤日誌 --log-error error-log=/data/mysql/mysql3306/error.log
常規日誌 --general_log=/data/mysql/mysql3306/general_log
慢查詢日誌 --slow_query-log=/data/mysql/mysql3306/slow_log 分析工具:mysqldumpslow/pt-query-digest
二進制日誌 --log-bin=/data/mysql/mysql3306/mysql-bin (保留7天日誌 expire-logs-days = 7) 分析工具: mysqlbinlog/binlog2sql
審計日誌 (官方收費插件) --audit_log --audit_log_file audit.log

二進制日誌

33DDL DML DCL DQL

什麽時候進行切換:
1、flush logs; (如果卡著不動,有可能有大事務沒結束,一個事務只能寫到一個binlog裏面)
2、超過日誌設置 max_binlog_size
3、數據庫重啟

二進制日誌記錄的格式:
binlog_format=row
statement: 記錄執行的SQL語句 (過去式,主從版本不統一,遷移,升級的過度狀態)
insert into t1 (id,c1) values (1,uuid()); 主從不一致。
row: 記錄更改前後的數據 (生產用)
mixed: (不要用)

查看binlog:
show binary logs;
show master status; 5.7 gtid: select @@server_uuid; 查到自己的server還是從別的地方拿到執行的事務。
show binlog events in ‘mysql-bin.000001‘;
MySQL的二進制是以‘事件(event)為單位存儲到日誌中,一個insert,update。。。有多個事件組成。
event:begin,table_map,x_event,commit;
position:字節偏移量(位置)show master status顯示的position就是日誌的大小。

查看二進制日誌:
無法用文本查看
日誌以緊湊二進制格式存儲,以事件組合
使用mysqlbinlog查看
mysqlbinlog -v --base64-output=decode-rows mysql-bin.000001 > t.log

刪除二進制日誌:
基於時間刪除
set global expire_logs_days=7;
purge binary logs before now()-interval 3 day3;
基於文件名刪除
purge binary logs to ‘mysql-bin.000010‘;


審計日誌:
audit——log_policy選項設置
默認all

日誌文件生成一個服務器活動審計記錄:
內容包括,在系統生發生的錯誤記錄,
客戶機連接和斷開的時間,
連接期間執行的操作,
訪問的數據庫和表。

MySQL常用命令

help show; 幫助信息
pager more; 分頁查看
pager less; 上下查看
pager cat; 返回

information_schema:
中央信息庫,磁盤找不到,只能select。

查看schema下面的表的存儲引擎:
select table_name,engine from information_schema.tables where tables_schema=‘test‘
查看表的字符集:
select character_set_name.collation_name from information_schema.colltions where is_default=‘Yes‘;
查看每個庫下面有多少長表:
select table_schema , count(*) from information_schema.tables group by table_schema;

庫改名:
1、create database newdata;
rename olddata.t1 to newdata.t1;
2、select concat("rename table olddata.",table_name," to new.",table_name," ;" from information_schema.tables where table_schema=‘olddata‘ into outfile ‘/tmp/rename.sql‘;
source /tmp/rename.sql;
3、終端 mysql -e " " > rename.sql

統計庫大小:
單位M : select sum(data_length+index_length)/1024/1024 from information_schema.tables where table_schema=‘test‘;
統計每張表的大小:
單位M :select table_name,sum(data_length+index_length)/1024/1024 from information_schema.tables where table_schema=‘test‘ group by table_name;

show核心語句
show open tables; 查看當然打開的表
show table status like ‘‘; 不建議高並發使用
show columns from tb;
show full columns from tb;
show databases like "t%";
show columns from db where ‘Default‘ is null;
show character set ;
show collation;

show global variables ; 全局global
show variables; 當前session

show @@ xxx; 全局

show @ xxx; session

練習腳本:
1、error如果有變更,將變更打印出來:
備份error,diff備份文件和線上文件


DAY5-MySQL日誌管理