日志分类:
错误日志
查询日志
二进制日志
慢查询日志
添加完毕后,重启MySQL服务器,便可查询日志设置:
mysql> SHOW VARIABLES LIKE 'log_%';
+----------------------------------------+---------------------------------------+
| Variable_name | Value |
+----------------------------------------+---------------------------------------+
| log_bin | OFF |
| log_bin_basename | |
| log_bin_index | |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| log_builtin_as_identified_by_password | OFF |
| log_error | E:\wamp64\bin\mysql\mysql5.7.14\data\ |
| log_error_verbosity | 3 |
| log_output | FILE |
| log_queries_not_using_indexes | OFF |
| log_slave_updates | OFF |
| log_slow_admin_statements | OFF |
| log_slow_slave_statements | OFF |
| log_statements_unsafe_for_binlog | ON |
| log_syslog | ON |
| log_syslog_tag | |
| log_throttle_queries_not_using_indexes | 0 |
| log_timestamps | UTC |
| log_warnings | 2 |
+----------------------------------------+---------------------------------------+
19 rows in set, 6 warnings (0.05 sec)
如果想修改日志文件的目录和名称,可以对my.ini中的log_bin参数修改如下:
[mysqld]
log_bin=“E:/MYSQL/log/binlog”
关闭和重新启动后,新的二进制日志文件将出现在E:/MYSQL/log/binlog文件夹下面,名称为binlog.000001和binlog.index。
mysql> SHOW BINARY LOGS;
ERROR 1381 (HY000): You are not using binary logging
使用mysqlbinlog查看:
C:\Users\lenovo>mysqlbinlog D:\MySQL\log\binlog.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
mysqlbinlog: File 'D:\MySQL\log\binlog.000001' not found (Errcode: 2 - No such file or directory)
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*/;
删除二进制日志
a. RESET MASTER;
b. PURGE MASTER LOGS
使用二进制日志还原数据库
mybinlog恢复数据语法如下:
mysqlbinlog [option] filename |mysql -uuser -p pass
SET sql_log_bin={0|1}
0代表暂停,1代表恢复记录。
16章 性能优化
使用索引查询:
mysql> USE index_test;
Database changed
mysql> EXPLAIN SELECT * FROM fruits WHERE f_name LIKE '_%x';
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | fruits | NULL | ALL | NULL | NULL | NULL | NULL | 16 | 11.11 | Using where |
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.15 sec)
mysql> EXPLAIN SELECT * FROM fruits WHERE f_name LIKE 'x%';
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | fruits | NULL | ALL | NULL | NULL | NULL | NULL | 16 | 11.11 | Using where |
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
mysql> CREATE INDEX index_id_price ON fruits(f_id,f_price);
Query OK, 0 rows affected (0.89 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> EXPLAIN SELECT * FROM fruits WHERE f_id=5.2;
+----+-------------+--------+------------+------+------------------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+------+------------------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | fruits | NULL | ALL | PRIMARY,index_id_price | NULL | NULL | NULL | 16 | 10.00 | Using where |
+----+-------------+--------+------------+------+------------------------+------+---------+------+------+----------+-------------+
1 row in set, 5 warnings (0.08 sec)
从第一条查询语句可以看出,f_id='12’的记录有一条。
mysql> EXPLAIN SELECT * FROM fruits WHERE f_name='apple' OR s_id=101\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: fruits
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 16
filtered: 19.00
Extra: Using where
1 row in set, 1 warning (0.00 sec)
ERROR:
No query specified
优化插入记录速度:
ALTER TABLE table_name DISABLE KEYS;
重新开启索引的语句如下:
ALTER TABLE table_name ENABLE KEYS;
SET UNIQUE_CHECKS=0;
开启语句:
SET UNIQUE_CHECKS=1;