二) mysql体系结构
mysql逻辑存储结构
Mysql数据库物理结构
mysql5.7.20版本
[root@mysqlmaster01 mysql_data]# ll
total 4240516
drwxr-x---. 2 mysql mysql 4096 Nov 22 15:02 aaa
-rw-r-----. 1 mysql mysql 56 Nov 21 15:15 auto.cnf
-rw-r-----. 1 mysql mysql 177 Nov 21 15:15 bin.000001
-rw-r-----. 1 mysql mysql 398 Nov 22 13:28 bin.000002
-rw-r-----. 1 mysql mysql 3522 Nov 22 15:02 bin.000003
-rw-r-----. 1 mysql mysql 39 Nov 22 13:28 bin.index
-rw-------. 1 root root 1675 Nov 21 15:16 ca-key.pem
-rw-r--r--. 1 root root 1107 Nov 21 15:16 ca.pem
-rw-r--r--. 1 root root 1107 Nov 21 15:16 client-cert.pem
-rw-------. 1 root root 1679 Nov 21 15:16 client-key.pem
-rw-r-----. 1 mysql mysql 22131 Nov 22 14:13 error.log
-rw-r-----. 1 mysql mysql 898 Nov 21 15:15 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Nov 22 13:28 ibdata1
-rw-r-----. 1 mysql mysql 2147483648 Nov 22 13:28 ib_logfile0
-rw-r-----. 1 mysql mysql 2147483648 Nov 21 15:15 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 Nov 22 14:51 ibtmp1
drwxr-x---. 2 mysql mysql 4096 Nov 21 15:15 mysql
-rw-r-----. 1 mysql mysql 5 Nov 22 13:28 mysqlmaster01.test.com.pid
drwxr-x---. 2 mysql mysql 4096 Nov 21 15:15 performance_schema
-rw-------. 1 root root 1679 Nov 21 15:16 private_key.pem
-rw-r--r--. 1 root root 451 Nov 21 15:16 public_key.pem
-rw-r--r--. 1 root root 1107 Nov 21 15:16 server-cert.pem
-rw-------. 1 root root 1679 Nov 21 15:16 server-key.pem
-rw-r-----. 1 mysql mysql 1985 Nov 22 14:40 slow.log
drwxr-x---. 2 mysql mysql 12288 Nov 21 15:15 sys
-rw-r-----. 1 mysql mysql 7340032 Nov 21 15:15 undo001
-rw-r-----. 1 mysql mysql 7340032 Nov 21 15:15 undo002
-rw-r-----. 1 mysql mysql 7340032 Nov 22 13:28 undo003
drwxr-x---. 2 mysql mysql 4096 Nov 22 13:36 wpdb
mysql主要文件
数据库配置文件
表结构定义文件
每个表对应一个表结构文件、表结构文件以.frm结尾,表结构文件是二进制文件
[root@mysqlmaster01 mysql_data]# ll mysql/user.*
-rw-r-----. 1 mysql mysql 10816 Nov 22 15:53 mysql/user.frm
-rw-r-----. 1 mysql mysql 508 Nov 22 15:56 mysql/user.MYD
-rw-r-----. 1 mysql mysql 4096 Nov 22 15:56 mysql/user.MYI
错误文件
慢查询日志
二)如何查看frm格式的文件
[root@mysqlmaster01 software]# wget https://cdn.mysql.com//Downloads/MySQLGUITools/mysql-utilities-1.6.5.tar.gz
--2017-11-22 16:08:45-- https://cdn.mysql.com//Downloads/MySQLGUITools/mysql-utilities-1.6.5.tar.gz
Resolving cdn.mysql.com...
23.198.138.106
Connecting to cdn.mysql.com|23.198.138.106|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 668241 (653K) [application/x-tar-gz]
Saving to: “mysql-utilities-1.6.5.tar.gz”
100%[=====================================================================================================================>] 668,241 3.18M/s in 0.2s
2017-11-22 16:08:58 (3.18 MB/s) - “mysql-utilities-1.6.5.tar.gz” saved [668241/668241]
[root@mysqlmaster01 software]# tar xf mysql-utilities-1.6.5.tar.gz -C /usr/local/
[root@mysqlmaster01 software]# cd /usr/local/mysql-utilities-1.6.5/
[root@mysqlmaster01 mysql-utilities-1.6.5]# python setup.py install
[root@mysqlmaster01 ~]# mysqlfrm --diagnostic /data/mysql_data/aaa/a.frm
# WARNING: Cannot generate character set or collation names without the --server option.
# CAUTION: The diagnostic mode is a best-effort parse of the .frm file. As such, it may not identify all of the components of the table correctly. This is especially true for damaged files. It will also not read the default values for the columns and the resulting statement may not be syntactically correct.
# Reading .frm file for /data/mysql_data/aaa/a.frm:
# The .frm file is a TABLE.
# CREATE TABLE Statement:
CREATE TABLE `aaa`.`a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY `PRIMARY` (`id`)
) ENGINE=InnoDB;
#...done.
3)关于mysql慢查询日志
将运行超过某个时间阈值的SQL语句记录到文件(默认吗:机器码-slow.log),一般我们会在配置文件中定义一个
slow_query_log_file = slow.log
mysql> show variables like ‘slow_query_%‘;
+---------------------+----------+
| Variable_name | Value |
+---------------------+----------+
| slow_query_log | ON |
| slow_query_log_file | slow.log |
+---------------------+----------+
2 rows in set (0.00 sec)
案例:
slow_query_log = 1
slow_query_log_file = slow.log
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1
log_throttle_queries_not_using_indexes = 10
expire_logs_days = 90
long_query_time = 2
min_examined_row_limit = 100
mysql> show variables like ‘slow_query_%‘;
+---------------------+----------+
| Variable_name | Value |
+---------------------+----------+
| slow_query_log | ON | -----------是否开启慢查询日志
| slow_query_log_file | slow.log |---------------慢查询日志的文件
+---------------------+----------+
2 rows in set (0.00 sec)
mysql> show variables like ‘long_query%‘;
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 2.000000 |---------------------------指定慢查询阈值
+-----------------+----------+
1 row in set (0.00 sec)
mysql> show variables like ‘min_examined%‘;
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| min_examined_row_limit | 100 |-------------扫描记录少于该值的SQL不记录到慢查询
+------------------------+-------+
1 row in set (0.00 sec)
mysql> show variables like ‘log_queries%‘;
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | ON | ------将没有使用的索引的SQL记录到慢查询日志
+-------------------------------+-------+
1 row in set (0.00 sec)
mysql> show variables like ‘log_throttle%‘;
+----------------------------------------+-------+
| Variable_name | Value |
+----------------------------------------+-------+
| log_throttle_queries_not_using_indexes | 10 | -----限制每分钟记录没有使用索引sql语句的次数
+----------------------------------------+-------+
1 row in set (0.00 sec)
mysql> show variables like ‘log_slow%‘;
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| log_slow_admin_statements | ON |-------------------记录管理操作,如alter
| log_slow_slave_statements | ON |------------------在从服务器上开启慢查询日志
+---------------------------+-------+
2 rows in set (0.00 sec)
mysql> show variables like ‘log_output%‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |------------慢查询日志格式(file/table/none)
+---------------+-------+
1 row in set (0.00 sec)
mysql> show variables like ‘log_time%‘;
+----------------+--------+
| Variable_name | Value |
+----------------+--------+
| log_timestamps | SYSTEM |---------使用系统时间戳
+----------------+--------+
1 row in set (0.00 sec)
查询慢查询日志
[root@mysqlmaster01 mysql_data]# mysqldumpslow --help
-s ORDER what to sort by (al, at, ar, c, l, r, t), ‘at‘ is default
al: average lock time
ar: average rows sent
at: average query time
c: count
l: lock time
r: rows sent
t: query time
-r reverse the sort order (largest last instead of first) 排序方式
-t NUM just show the top n queries 查询前几条
二)使用表格来存储慢查询
log_output = TABLE 添加到my.cnf文件中
然后重启mysql服务器
然后查看log_output是不是记录到table表(该表是mysql数据里slow_log表)
更改slow_log表的存储引擎
mysql> show variables like ‘slow%‘;
+---------------------+----------+
| Variable_name | Value |
+---------------------+----------+
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | slow.log |
+---------------------+----------+
3 rows in set (0.00 sec)
mysql> set global slow_query_log = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> alter table slow_log engine=myisam;
Query OK, 0 rows affected (5.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> set global slow_query_log = 1;
Query OK, 0 rows affected (0.00 sec)
三)general_log
mysql> show variables like ‘general_log%‘;
+------------------+------------------------------------+
| Variable_name | Value |
+------------------+------------------------------------+
| general_log | OFF |
| general_log_file | /data/mysql_data/mysqlmaster01.log |
+------------------+------------------------------------+
2 rows in set (0.00 sec)
审计:
https://mariadb.com/kb/en/library/mariadb-audit-plugin-log-settings/
http://blog.csdn.net/jacson_bai/article/details/46238501