引擎
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
常用引擎
引擎 | 说明 |
---|---|
InnoDB | 支持事务,默认引擎 |
MyISAM | 高速查询引擎,不支持事务 |
Archive | 数据压缩存储引擎,便于数据归档 |
Memory | 内存存储引擎 |
InnoDB MyISAM
对比项 | InnoDB | MyISAM |
---|---|---|
数据存储形式 | .frm 源数据文件(表定义) .idb 存储数据和索引 | .frm 源数据文件, .myb 存储数据。 .myi 存放索引 |
锁 | 表锁、行锁、页锁 | 表锁 |
事务 | 支持 | 不支持 |
CURD | 同时读写 | 表锁 只能同时读,不能同时写 |
查看数据库表引擎
show table status like '%表名%'
日志
# 日志查看, 显示日志目录
mysql>
mysql> show variables like '%log_%';
+--------------------------------------------+------------------------------------------+
| Variable_name | Value |
+--------------------------------------------+------------------------------------------+
| binlog_cache_size | 32768 |
| binlog_checksum | CRC32 |
| binlog_direct_non_transactional_updates | OFF |
| binlog_error_action | ABORT_SERVER |
| binlog_format | ROW |
| binlog_group_commit_sync_delay | 0 |
| binlog_group_commit_sync_no_delay_count | 0 |
| binlog_gtid_simple_recovery | ON |
| binlog_max_flush_queue_time | 0 |
| binlog_order_commits | ON |
| binlog_row_image | FULL |
| binlog_rows_query_log_events | OFF |
| binlog_stmt_cache_size | 32768 |
| binlog_transaction_dependency_history_size | 25000 |
| binlog_transaction_dependency_tracking | COMMIT_ORDER |
| expire_logs_days | 0 |
| general_log_file | /var/lib/mysql/localhost.log |
| innodb_flush_log_at_timeout | 1 |
| innodb_flush_log_at_trx_commit | 1 |
| innodb_log_buffer_size | 16777216 |
| innodb_log_checksums | ON
未完待续...
主要日志
- 错误日志
- 查询日志
- 慢查询日志
- 二进制日志
- 事务日志
- 中继日志:reply log
- 错误日志
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.00 sec)
- log_warnings用于标识警告信息是否一并记录到错误日志中。
- 0:表示不记录警告信息。
- 1:表示警告信息一并记录到错误日志中。
- 大于1:表示"失败的连接"的信息和创建新连接时"拒绝访问"类的错误信息也会被记录到错误日志中。
log_warnings 可以在my.cnf
配置文件中设置
- 查询日志
mysql>
mysql> show variables where variable_name like "%general_log%" or variable_name="log_output";
+------------------+------------------------------+
| Variable_name | Value |
+------------------+------------------------------+
| general_log | OFF |
| general_log_file | /var/lib/mysql/localhost.log |
| log_output | FILE |
+------------------+------------------------------+
3 rows in set (0.00 sec)
- general_log
顾名不思义
,不是只记录select日志, 查询日志记录了数据库执行的命令,不管这些语句是否正确,都会被记录。通用日志更确切- 默认不开启,比较消耗IO
- log_output 日志存储4中方式:FILE 、TABLE、FILETABLE、NONE
FILE: 以文件形式存储,文件名与主机名相同,而且使用了相对路径,默认位置为
datadir
变量所对应的目录位置,一般为/var/lib/mysql目录下。
TABLE:: 表示存放于mysql库的general_log
表中
FILETABLE: 表示存储两份,在表和文件中都存储,一般不这么干
NONE: 表示不存储,即使general_log=NO
, 慢查询日志的输出也是由此参数进行控制
- 查询日志—测试
# 开启
mysql>set global general_log = NO;
mysql>set global log_output = 'file,table';
Query OK, 0 rows affected (0.00 sec)
mysql> show variables where variable_name like "%general_log%" or variable_name="log_output";
+------------------+------------------------------+
| Variable_name | Value |
+------------------+------------------------------+
| general_log | ON |
| general_log_file | /var/lib/mysql/localhost.log |
| log_output | FILE,TABLE |
+------------------+------------------------------+
3 rows in set (0.00 sec)
# 日志表
mysql> select * from general_log\G;
ERROR 1146 (42S02): Table 'ikang_db.general_log' doesn't exist
ERROR:
No query specified
mysql> select * from mysql.general_log\G;
*************************** 1. row ***************************
event_time: 2020-06-18 21:54:38.535727
user_host: root[root] @ localhost []
thread_id: 5
server_id: 0
command_type: Query
argument: show variables where variable_name like "%general_log%" or variable_name="log_output"
*************************** 2. row ***************************
event_time: 2020-06-18 21:55:57.455557
user_host: root[root] @ localhost []
thread_id: 5
server_id: 0
command_type: Query
argument: show tables
*************************** 3. row ***************************
event_time: 2020-06-18 21:56:14.031946
user_host: root[root] @ localhost []
thread_id: 5
server_id: 0
command_type: Query
argument: select * from ad_role
*************************** 4. row ***************************
event_time: 2020-06-18 22:03:33.808484
user_host: root[root] @ localhost []
thread_id: 5
server_id: 0
command_type: Query
argument: select * from genral_log
*************************** 5. row ***************************
event_time: 2020-06-18 22:03:49.461788
user_host: root[root] @ localhost []
thread_id: 5
server_id: 0
command_type: Query
argument: select * from mysql.genral_log
*************************** 6. row ***************************
event_time: 2020-06-18 22:04:20.566497
user_host: root[root] @ localhost []
thread_id: 5
server_id: 0
command_type: Query
argument: select * from general_log
*************************** 7. row ***************************
event_time: 2020-06-18 22:04:31.491566
user_host: root[root] @ localhost []
thread_id: 5
server_id: 0
command_type: Query
argument: select * from mysql.general_log
7 rows in set (0.00 sec)
ERROR:
No query specified
mysql>
在/var/lib/mysql
目录中生产与机器名相同日志文件,记录了操作
- 慢查询日志
mysql> show variables where variable_name like '%long_q%' or variable_name like 'slow%';
+---------------------+-----------------------------------+
| Variable_name | Value |
+---------------------+-----------------------------------+
| long_query_time | 10.000000 |
| slow_launch_time | 2 |
| slow_query_log | OFF |
| slow_query_log_file | /var/lib/mysql/localhost-slow.log |
+---------------------+-----------------------------------+
4 rows in set (0.01 sec)
- slow_query_log: 5.6以后使用该设置慢查询,默认是关闭的,可以开启。开启后要设置时间
long_query_time:慢查询时间,任何CURD操作只要超过了设置时间,就会记录。- slow_query_log_file: 日志文件,可以是:FILE、TABLE、FILETABLE、NONE 与查询日志设置一样
- slow_launch_time: 表示thread create的一个阀值,如果thread create的时间超过了这个值,也会开启了慢查询日志,但是没有看到日志生成。
- 慢查询日志—测试
# 开启
mysql> set global slow_query_log=on;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables where variable_name like '%long_q%' or variable_name like 'slow%';
+---------------------+-----------------------------------+
| Variable_name | Value |
+---------------------+-----------------------------------+
| long_query_time | 10.000000 |
| slow_launch_time | 2 |
| slow_query_log | OFF |
| slow_query_log_file | /var/lib/mysql/localhost-slow.log |
+---------------------+-----------------------------------+
4 rows in set (0.01 sec)
# 验证
mysql> select sleep(11), role_name from ad_role;
+-----------+-----------+
| sleep(11) | role_name |
+-----------+-----------+
| 0 | vip1 |
| 0 | vip2 |
| 0 | vip3 |
| 0 | III |
| 0 | vippp |
| 0 | vippp |
| 0 | vippp |
+-----------+-----------+
7 rows in set (1 min 17.00 sec)
修改时间后,一定要退出当前会话,重新登录, 重启失效,修改配置文件长久有效
[mysqld]
slow_query_log=ON
long_query_time=3
查看日志文件
- mysqldumpslow 慢查询日志命令
- mysqldumpslow命令我们可以更加方便的从不同的维度对慢日志进行排序、查找、统计
- mysqldumpslow只能作用于慢查询日志文件,而不会帮我们统计slow_log表
# 根据时间降序
mysqldumpslow -s -t /var/lib/mysql/DB213-slow.log
# 根据记录数降序
mysqldumpslow -s -r /var/lib/mysql/localhost-slow.log
# 根据执行次数降序
mysqldumpslow -s -C /var/lib/mysql/localhost-slow.log
# 更多命令
mysqldumpslow --help
- 二进制日志
- 这个是数据库中最重要的日志,会记录所有DML,不会记录select
- 数据备份和数据恢复
- binlog 有三种模式
- statement
- row
- mixed
binglog在下一篇博客详细说明。
-
事务日志
-
中继日志:reply log