当前位置:网站首页>Four logs of MySQL server layer
Four logs of MySQL server layer
2022-07-06 03:13:00 【Rookie ~ ~】
List of articles
One 、MySQL Server Introduction to layer log
When one MySQL Server Client Initiate a connection request , stay MySQL What the backend has to do :
MySQL The journal is in MySQL server Generated on , No matter which storage engine you change , These logs are needed , Include :
Error log : Record mysqld During the operation of the service cordump、error、exception etc.
Query log : Record MySQL Server All that you've received SQL. Due to the of online projects SQL That's too much , Open query log IO Too much leads to MySQL inefficiency , We It usually doesn't open , Only start when debugging . For example, by looking at sql Discover hotspot data and cache it :

Binary log : Record data changes (insert、update、delete、alter …), It's very important , Can be used for Data recovery , Master slave copy . Master-slave replication technology depends on log_bin, All changes to the main library are recorded in log_bin in , From the library from binlog Read all operations of the main library , Do it again .

Slow query log : Records some instances where the execution time exceeds the specified value SQL sentence , Available for developers to analyze SQL, So as to optimize
MySQL There are many global variables defined in to record settings or states , Check is show variables( Global variables ) perhaps show status( state ). View log related variables :
Two 、 Profile parameters
open my.ini, Add the above parameters to the back , Restart after saving mysql Just the service .
adopt set Method can only affect the current session, If you want to The configuration is permanently valid , It needs to be set on the configuration file , And then restart MySQL service , It can take effect forever
linux Next restart mysqld Commands for services :sudo service mysqld restart
Let's look at the configuration file /etc/mysql/my.cnf
- give
log-errorThe path is open log-error, If not custom log-error The path of , Default indata_dir - In the open
log-bin=mysql-binAt the same time, addserver-id=1( At present MySQL Server The identity of the ), Otherwise, in the linux root Nextsudo service mysqld restartThe service cannot be restarted - Set expiration time
expire_log_days, Because one day the disk will be full of this log , Causes the server to become inoperable , After the set time is exceeded, the log file will be deleted
3、 ... and 、 Error log

Four 、 Binary log
Binary log (BINLOG) It records all the DDL( Data definition language ) and DML( Data manipulation language ) sentence , But it does not include data query statements . Statement to “ event ” Form preservation of , It describes the data change process . This log plays an extremely important role in data recovery in case of disaster .
Two important scenes : Master slave copy 、 Data recovery .
see binlog:show binary logs
Binary content cannot be viewed directly , Because it doesn't record plaintext , Because it's a little unsafe to record plaintext , After all, it is the details of database table changes , Unlike slow queries, logs are recorded in clear text .

binlog Default in MySQL Of data_dir Next 
1. demonstration binlog Record changes
Let's refresh , Make a new one binlog
Use school Database based user Table :
perform insert and update operation 
Look again binlog
We found that the log filesize It's different from just now , Be sure to record the data change operation we just did
If we go straight cat Log view , You will find that it is not in clear text , Unable to view directly 
adopt mysqlbinlog Tools (mysql Native tools ) Mind parsing a large number of binlog Log files :
mysqlbinlog --no-defaults --database=school --base64-output=decode-rows -v --start-datetime='2022-03-01 00:00:00' --stop-datetime='2022-03-31 00:00:00' mysql-bin.000001 | more
- database: Specify to view changes to a library
- base64-output:binlog Decoding method
- start-datetime & stop-datetime: Specify to view changes in a certain period of time , If you don't write, view all changes
- mysql-bin.000001: View the binary log file
Let's take a look at what just changed binlog:


- @1、@2、@3、@4: Represents the name of the database table 4 A field
- server id: It means that we are my.cnf Set in the id, Used to identify the current MySQL The identity of the
- at 565、at679: It refers to the current event in binlog The location of the record , Use when recovering data .
2. demonstration binlog Data recovery

establish mytest database , Create inside user Table and add data 
If someone deletes the library now 
At this time mytest All tables and data in the library are gone , However, these operations are recorded in the binary log binlog Inside
In theory , It can be downloaded from binlog Recover the lost data . Because the recovery process is also the modification of data , Therefore, the logs generated during the recovery process should also be recorded in binlog in , So in order to prevent us from not knowing where to end the data recovery process , Or we can't tell which binary logs we need to recover and which are newly generated in the recovery , This requires us Appoint binlog Recovery interval
We now know , We build a library 、 Build table 、 The operations of inserting data are recorded in mysql-bin.00003 In file 
Let's refresh now , Make a new one binlog, This allows our next data recovery operations to be recorded in mysql-bin.00004 In file , Instead of adding to mysql-bin.00003

So let's look at mysql-bin.00003, Find the interval that needs to be recovered 


from mysql-bin.000003 Take out all the operations in the interval , Through the pipe MySQL shell On the implementation 
View the current library , You can see mytest Has recovered 
Then check the table and data 
In this way, all the data will be restored .
We can not only pass binlog The location of the record , Get the interval that needs to be recovered , It can also be done through binlog Record the time to get the interval to be recovered , Parameter is :start-datetime、stop-datetime

Pay attention to data recovery in actual use : Because of configuration bin-log It is also configured mysql-bin, Also configured expire_logs_days( The expiration time of the log ), How to recover the data before the expiration time ?
mysql The recovery of data depends on Data backup and binlog Data recovery . The expired log data will be backed up , No expired data can be passed directly through binlog recovery . Generally, data backup is to backup data to sql In the script file , For example, save in ~/data.sql in , And then in mysql with source ~/data.sql perform , Or if it's in linux Of shell Pipes can be used inside :cat ~/data.mysql|mysql -u root -p, We can restore the data we backed up before to mysql In the library table of .
5、 ... and 、 Slow query log
边栏推荐
- 【若依(ruoyi)】ztree 自定义图标(iconSkin 属性)
- 适合程序员学习的国外网站推荐
- These are not very good
- Tidb ecological tools (backup, migration, import / export) collation
- How to choose PLC and MCU?
- The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
- 八道超经典指针面试题(三千字详解)
- 真机无法访问虚拟机的靶场,真机无法ping通虚拟机
- Problems encountered in 2022 work IV
- Analyze 菜单分析
猜你喜欢

Performance test method of bank core business system

I sorted out a classic interview question for my job hopping friends

Misc (eternal night), the preliminary competition of the innovation practice competition of the National College Students' information security competition
![[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University](/img/0f/520242492524522c887b6576463566.jpg)
[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University

Overview of OCR character recognition methods

My C language learning record (blue bridge) -- under the pointer

MPLS experiment
![[unity3d] GUI control](/img/ef/9de2aa75c67cf825983867a913db74.png)
[unity3d] GUI control

【若依(ruoyi)】启用迷你导航栏

XSS challenges bypass the protection strategy for XSS injection
随机推荐
The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
Distributed service framework dobbo
Idea push rejected solution
Tidb ecological tools (backup, migration, import / export) collation
适合程序员学习的国外网站推荐
【若依(ruoyi)】设置主题样式
电机控制反Park变换和反Clarke变换公式推导
多态day02
银行核心业务系统性能测试方法
不赚钱的科大讯飞,投资价值该怎么看?
XSS challenges绕过防护策略进行 XSS 注入
Codeforces 5 questions par jour (1700 chacune) - jour 6
MySQL Server层四个日志
如何做好功能测试
4. File modification
张丽俊:穿透不确定性要靠四个“不变”
[network security interview question] - how to penetrate the test file directory through
手写数据库客户端
Taobao focus map layout practice
Codeworks 5 questions per day (1700 average) - day 6