MySQL日志 错误日志 二进制日志 查询日志 慢查询日志

一、错误日志

错误日志是MySQL中最重要的日志之一,它记录了当mysqld启动和停止时,以及服务器在运行过程中发生任何严重错误时的相关信息。当数据库出现任何故障导致无法正常使用时,建议首先查看此日志。
该日志是默认开启的,默认存放目录/var/log/,默认的日志文件名为mysqld.log 。(yum安装)

MySQL里查看位置  :

mysql> show variables like '%log_error%';
+---------------------+---------------------+
| Variable_name       | Value               |
+---------------------+---------------------+
| binlog_error_action | ABORT_SERVER        |
| log_error           | /var/log/mysqld.log |
| log_error_verbosity | 3                   |
+---------------------+---------------------+
3 rows in set (0.01 sec)

实例:

在一台服务器修改MySQL的配置文件uuid,打开另一个窗口动态查看错误日志

[root@zhuku ~]# vim /var/lib/mysql/auto.cnf 
重启服务
[root@zhuku ~]# systemctl restart mysqld
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
报错了
 
 
修改uuid  加一位,然后打开另一个终端  动态查看错误日志
 
[root@zhuku ~]# tail -f /var/log/mysqld.log    -----这是另一个窗口显示
 
2023-10-03T08:02:12.032739Z 0 [ERROR] Garbage characters found at the end of the server_uuid value in auto.cnf file. It should be of length '36' (UUID_LENGTH). Clear it and restart the server. 
2023-10-03T08:02:12.032782Z 0 [ERROR] Initialization of the server's UUID failed because it could not be read from the auto.cnf file. If this is a new server, the initialization failed because it was not possible to generate a new UUID.
2023-10-03T08:02:12.032789Z 0 [ERROR] Aborting

二、二进制日志

二进制日志(binlog)记录所有的DDL语句和DNL语句,但不包括数据查询语句。

作用:1.灾难时的数据恢复

2.MySQL的主从复制,在MySQL8版本中,默认二进制日志时开启的,涉及的参数如下:

mysql> show variables like '%log_bin%';
+---------------------------------+--------------------------------+
| Variable_name                   | Value                          |
+---------------------------------+--------------------------------+
| log_bin                         | ON                             |
| log_bin_basename                | /var/log/mysql/mysql-bin       |
| log_bin_index                   | /var/log/mysql/mysql-bin.index |
| log_bin_trust_function_creators | OFF                            |
| log_bin_use_v1_row_events       | OFF                            |
| sql_log_bin                     | ON                             |
+---------------------------------+--------------------------------+
6 rows in set (0.00 sec)
 
 
###  log_bin_basename是最终生成的二进制文件存放地址
###  log_bin_index 是日志索引文件

日志格式

日志格式含义
statement  基于sQL语句的日志记录,记录的是SQL语句,对数据进行修改的SQL都会记录在日志文件中。
row基于行的日志记录,记录的是每一行的数据变更(默认)
mixed  混合了STATEMENT和ROW两种格式,默认采用STATEMENT,在某些特殊情况下会自动切换为ROW进行记录。
mysql> show variables like '%binlog_format%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.00 sec)

查看二进制日志

由于日志是以二进制方式存储的,不能直接读取,需要通过二进制日志查询工具mysqlbinlog来查看,具体语法: 
mysqlbinlog   [ 参数选项]    logfilename

参数选项:
   -d              指定数据库名称,只列出指定的数据库相关操作。
   -o              忽略掉日志中的前n行命令。
   -v               将行事件(数据变更)重构为SQL语句
   -w              将行事件(数据变更)重构为SQL语句,并输出注释信息

实验:

由于默认是ROW,在MySQL中修改表的数据,查看二进制文件是如何显示的

mysql> select * from zhucong;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | aaa  |   21 |
|    2 | bbb  |   21 |
|    3 | ccc  |   21 |
+------+------+------+
3 rows in set (0.00 sec)
 
mysql> update zhucong set id=101;
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3  Changed: 3  Warnings: 0

查看二进制日志

[root@zhuku mysql]# mysqlbinlog -v mysql-bin.000001 
 
 
### UPDATE `db2`.`zhucong`
### WHERE                     -------where是更新条件
###   @1=100                    -----@是字段
###   @2='aaa'
###   @3=21
### SET
###   @1=101
###   @2='aaa'
###   @3=21
### UPDATE `db2`.`zhucong`
### WHERE
###   @1=100
###   @2='bbb'
###   @3=21
### SET
###   @1=101
###   @2='bbb'
###   @3=21
### UPDATE `db2`.`zhucong`
### WHERE
###   @1=100
###   @2='ccc'
###   @3=21
### SET
###   @1=101
###   @2='ccc'
###   @3=21
# at 457
#231003 17:31:47 server id 1  end_log_pos 488 CRC32 0xec1bf22b 	Xid = 21
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

语法是:mysqlbinlog   参数   二进制日志文件名字

修改日志格式

默认是ROW

[root@zhuku ~]# vim /etc/my.cnf     #修改配置文件
  
进去添加:
binlog-format=statement
 
重启服务:
# systemctl restart mysqld
 
查看是否修改成功
mysql> show variables like '%binlog_format%';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)
 

实验2

创建一个新表,添加数据

mysql> use db2;
mysql> create table student(id int,name varchar(10),age int);
mysql> insert into student(id,name,age) values(1,'tom',18),(2,'jack',19),(3,'ml',20);
 
mysql> select * from student;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | tom  |   18 |
|    2 | jack |   19 |
|    3 | ml   |   20 |
+------+------+------+
3 rows in set (0.00 sec)

观察二进制日志是否有记录

[root@zhuku mysql]# cd /var/lib/mysql
[root@zhuku mysql]# ls
 
[root@zhuku mysql]# mysqlbinlog mysql-bin.000002  
切记: 修改完数据的日志格式以后,将产生新的二进制日志文件

日志删除

三、查询日志

查询日志中记录了客户端的所有操作语句,而二进制日志不包含查询数据的SQL语句。默认情况下,查询日志是未开启

mysql> show variables like '%general%';
+------------------+---------------------------+
| Variable_name    | Value                     |
+------------------+---------------------------+
| general_log      | OFF                       |
| general_log_file | /var/lib/mysql/congku.log |
+------------------+---------------------------+
2 rows in set (0.01 sec)

想要打开查询日志,可以设置配置文件

vim /etc/my.cnf
 
加入下面俩行
general_log=1
#该选项用来开启查询日志,可选值: 0或者1 ; 0代表关闭,1 代表开启
 
general_log_file=/var/lib/mysql/mysql_chaxun.log
 #设置日志的文件名,如果没有指定,默认的文件名为host_ name.log
 
重启MySQL服务;
systemctl restart mysqld

由于查询日志的存放路径是/var/lib/mysql 因此我们可以通过动态查看

实践

窗口一输入:
[root@zhuku ~]# mysql -pzyq123
 
mysql> show databases;
mysql> use db1;
mysql> show tables;
mysql> select * from hh;
 
 
窗口二动态查询:
tail -f /var/lib/mysql/mysql_chaxun.log 
 
 
2023-10-04T03:22:25.293695Z	    5 Connect	root@localhost on  using Socket
2023-10-04T03:22:25.294124Z	    5 Query	select @@version_comment limit 1
2023-10-04T03:22:29.661774Z	    5 Query	show databases
2023-10-04T03:23:23.658351Z	    5 Query	SELECT DATABASE()
2023-10-04T03:23:23.658684Z	    5 Init DB	db1
2023-10-04T03:23:23.659608Z	    5 Query	show databases
2023-10-04T03:23:23.660486Z	    5 Query	show tables
2023-10-04T03:23:23.660882Z	    5 Field List	hh 
2023-10-04T03:23:23.682859Z	    5 Field List	idol 
2023-10-04T03:23:27.395442Z	    5 Query	show tables
2023-10-04T03:23:32.343584Z	    5 Query	select * from hh

四、慢查询日志

慢查询日志记录了所有执行时间超过参数long_ query_ _time设置值并且扫描记录数不小于min_ examined_ _row_ _limit的所有的SQL语句的日志,默认未开启。long_ query_ _time 默认为10秒,最小为0,精度可以到微秒。

#慢查询日志
slow_ query_ _log=1
#执行时间参数
long_ query_ _time=2

默认情况下,不会记录管理语句,也不会记录不使用索引进行查找的查询。可以使用log_ _slow_ _admin_ statements和更改此行为log_ _queries_ not_ _using_ indexes, 如下所述

#记录执行较慢的管理语句
log_ slow_ admin_ statements =1 
#记录执行较慢的未使用索引的语句
log_ _queries_ not_ using_ indexes = 1

The initialization of UART4 can vary depending on the microcontroller or development board being used. However, here is an example of how to initialize UART4 on a STM32F4 Discovery board using CubeMX and HAL libraries: 1. Open CubeMX and create a new project for your microcontroller or development board. 2. In the "Pinout & Configuration" tab, select "UART4" from the peripherals list. 3. Enable the "Asynchronous" mode and set the baud rate to the desired value. 4. Set the word length, stop bits, and parity according to your application's requirements. 5. Configure the GPIO pins for the UART4 peripheral by selecting the appropriate pins in the "Pinout" tab. 6. Generate the code by clicking on the "Generate" button. 7. Open the generated code and locate the "MX_UART4_Init" function. 8. In this function, you will find the initialization code for UART4. It will look something like this: ``` huart4.Instance = UART4; huart4.Init.BaudRate = 115200; huart4.Init.WordLength = UART_WORDLENGTH_8B; huart4.Init.StopBits = UART_STOPBITS_1; huart4.Init.Parity = UART_PARITY_NONE; huart4.Init.Mode = UART_MODE_TX_RX; huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart4.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart4) != HAL_OK) { Error_Handler(); } ``` 9. This code initializes UART4 with a baud rate of 115200, 8 data bits, 1 stop bit, no parity, and both transmit and receive modes enabled. 10. You can now use the HAL functions to send and receive data over UART4. For example, to transmit a string over UART4, you can use the following code: ``` char* str = "Hello, World!"; HAL_UART_Transmit(&huart4, (uint8_t*)str, strlen(str), HAL_MAX_DELAY); ``` This will send the string "Hello, World!" over UART4.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值