MySQL 8.0 OCP (1Z0-908) 考点概要
MySQL 8.0 OCP (1Z0-908) 考点精析-安装与配置考点1:设置系统变量
【MySQL】控制MySQL优化器行为方法之optimizer_switch系统变量
【MySQL】MySQL系统变量(system variables)列表(mysqld --verbose --help的结果例)
【MySQL】MySQL系统变量(system variables)列表(SHOW VARIABLES 的结果例)
MySQL 8.0 OCP (1Z0-908) 考点精析-备份与恢复考点1:MySQL Enterprise Backup概要
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点1:sys.statement_analysis视图
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点2:系统变量的确认
MySQL 8.0 性能优化之 EXPLAIN ANALYZE
MySQL 8.0 性能优化之 慢速查询日志(slow query log)
MySQL 8.0 性能优化之 表连接算法(join algorithm)
MySQL 8.0 性能优化之 MySQL Enterprise Monitor之Query Analyzer
MySQL 8.0 OCP (1Z0-908) 考点精析-架构考点1:二进制日志文件(Binary log)
MySQL 8.0 架构 之数据字典(Data Dictionary)
MySQL 8.0 InnoDB Tablespaces之System Tablespace(系统表空间)
MySQL 8.0 InnoDB Tablespaces之File-per-table tablespaces(单独表空间)
MySQL 8.0 InnoDB Tablespaces之General Tablespaces(通用表空间/一般表空间)
MySQL 8.0 InnoDB Tablespaces之Temporary Tablespaces(临时表空间)
MySQL 8.0 InnoDB Tablespaces之Undo Tablespaces(UNDO表空间)
【MySQL】在数据目录之外创建InnoDB 表(Creating Tables Externally)
MySQL 8.0 InnoDB 架构之 日志缓冲区(Log Buffer)和重做日志(Redo Log)
MySQL 8.0 架构 之错误日志文件(Error Log)(1)
MySQL 8.0 架构 之错误日志文件(Error Log)(2)
MySQL中有多种类型的日志文件,这些日志可用于故障排除、性能调整和审计等目的,帮助找出正在发生的活动。
常见的日志文件包括:
日志类型 | 写入日志的信息 |
---|---|
错误日志(Error log) | 启动、运行或停止mysqld时遇到的问题 |
二进制日志(Binary log) | 更改数据的语句(也用于复制) |
中继日志(Relay log) | 从复制源服务器接收到的数据更改 |
通用查询日志(General query log) | 已建立的客户端连接和从客户端接收到的语句 |
慢查询日志(Slow query log) | 执行时间超过long_query_time秒的查询 |
DDL日志(元数据日志)(DDL log) | DDL语句执行的元数据操作 |
通用查询日志(General Query Log)用于记录MySQL服务器(mysqld)正在执行的操作。当客户端连接或断开连接时,服务器会将信息写入此日志,并记录从客户端接收到的SQL语句。
默认情况下,一般查询日志是禁用的。当启用后,MySQL服务器会按照接收顺序将SQL语句写入查询日志,这可能会与执行顺序有所不同。
通用查询日志(General Query Log)一般用在怀疑客户端存在错误或者希望了解客户端发送给服务器的详细内容的场景。
通用查询日志(General Query Log)相关的变量主要包括如下内容:
mysql> SHOW VARIABLES LIKE '%general%';
+------------------+-----------------------------+
| Variable_name | Value |
+------------------+-----------------------------+
| general_log | OFF |
| general_log_file | /var/lib/mysql/mysql-vm.log |
+------------------+-----------------------------+
2 rows in set (0.00 sec)
general_log 变量用于控制是否启用通用查询日志。默认值为禁用。
另外,通用查询日志输出的目标还和log_output系统变量有关,如果log_output值为NONE,则即使启用通用查询日志,也不会写入任何日志信息。
启用和禁用的设定例:
SET GLOBAL general_log = 'ON';
SET GLOBAL general_log = 'OFF';
参数详细:
| Command-Line Format | --general-log[={OFF|ON}] |
|------------------------|--------------------------|
| System Variable | general_log |
| Scope | Global |
| Dynamic | Yes |
| SET_VAR Hint Applies | No |
| Type | Boolean |
| Default Value | OFF |
参考:
general_log_file
https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_general_log
general_log_file变量用于控制通用查询日志的文件名。默认值为host_name.log。
Command-Line Format | –general-log-file=file_name |
---|---|
System Variable | general_log_file |
Scope | Global |
Dynamic | Yes |
SET_VAR Hint Applies | No |
Type | File name |
Default Value | host_name.log |
参考:
general_log_file
https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_general_log_file
log_output用于控制通用查询日志(General Query Log)和慢查询日志(slow query log)输出的格式。
TABLE:将日志记录到表中。通用查询日志(General Query Log)和慢查询日志(slow query log)分别对应mysql.general_log和mysql.slow_log表
FILE:将日志记录到文件中。(默认值)
NONE :禁用日志记录(优先于其他设置)。
参数详细:
Command-Line Format | –log-output=name |
---|---|
System Variable | log_output |
Scope | Global |
Dynamic | Yes |
SET_VAR Hint Applies | No |
Type | Set |
Default Value | FILE |
Valid Values | TABLE, FILE, NONE |
参考:
log_output
https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_log_output
log_output的值为TABLE时,输出目标通用查询日志(General Query Log)的表定义如下。
mysql> SHOW CREATE TABLE mysql.general_log\G
*************************** 1. row ***************************
Table: general_log
Create Table: CREATE TABLE `general_log` (
`event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
`user_host` mediumtext NOT NULL,
`thread_id` bigint unsigned NOT NULL,
`server_id` int unsigned NOT NULL,
`command_type` varchar(64) NOT NULL,
`argument` mediumblob NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8mb3 COMMENT='General log'
1 row in set (0.00 sec)
mysql>
下面演示一下如何设置通用查询日志。
mysql> SHOW VARIABLES LIKE '%general%';
+------------------+-----------------------------+
| Variable_name | Value |
+------------------+-----------------------------+
| general_log | OFF |
| general_log_file | /var/lib/mysql/mysql-vm.log |
+------------------+-----------------------------+
2 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |
+---------------+-------+
1 row in set (0.00 sec)
mysql> SET GLOBAL general_log = 'ON';
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| employees |
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| testdb |
| world |
| world_x |
+--------------------+
9 rows in set (0.00 sec)
mysql> use employees;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------------+
| Tables_in_employees |
+----------------------+
| current_dept_emp |
| departments |
| dept_emp |
| dept_emp_latest_date |
| dept_manager |
| employees |
| employees_t |
| salaries |
| titles |
+----------------------+
9 rows in set (0.01 sec)
mysql> select count(*) from employees;
+----------+
| count(*) |
+----------+
| 300024 |
+----------+
1 row in set (0.14 sec)
mysql>
通过OS命令查看通用查询日志。
ubuntu@mysql-vm:~$ sudo more /var/lib/mysql/mysql-vm.log
/usr/sbin/mysqld, Version: 8.0.35-0ubuntu0.22.04.1 ((Ubuntu)). started with:
Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock
Time Id Command Argument
2023-12-31T14:32:02.135073Z 10 Query show databases
2023-12-31T14:32:19.343316Z 10 Query SELECT DATABASE()
2023-12-31T14:32:35.743159Z 10 Init DB employees
2023-12-31T14:32:35.744393Z 10 Query show databases
2023-12-31T14:32:35.745216Z 10 Query show tables
2023-12-31T14:32:35.747175Z 10 Field List current_dept_emp
2023-12-31T14:32:35.764138Z 10 Field List departments
2023-12-31T14:32:35.766528Z 10 Field List dept_emp
2023-12-31T14:32:35.766667Z 10 Field List dept_emp_latest_date
2023-12-31T14:32:35.766935Z 10 Field List dept_manager
2023-12-31T14:32:35.767356Z 10 Field List employees
2023-12-31T14:32:35.770947Z 10 Field List employees_t
2023-12-31T14:32:35.772111Z 10 Field List salaries
2023-12-31T14:32:35.772519Z 10 Field List titles
2023-12-31T14:32:38.751537Z 10 Query show tables
2023-12-31T14:32:53.823039Z 10 Query select count(*) from employees
ubuntu@mysql-vm:~$
mysql> SET GLOBAL general_log = 'OFF';
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE '%general%';
+------------------+-----------------------------+
| Variable_name | Value |
+------------------+-----------------------------+
| general_log | OFF |
| general_log_file | /var/lib/mysql/mysql-vm.log |
+------------------+-----------------------------+
2 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |
+---------------+-------+
1 row in set (0.00 sec)
mysql>
7.4.3 The General Query Log
https://dev.mysql.com/doc/refman/8.0/en/query-log.html
7.4.1 Selecting General Query Log and Slow Query Log Output Destinations
https://dev.mysql.com/doc/refman/8.0/en/log-destinations.html