搜索
首页数据库mysql教程mysql学习记录(二十五)--mysql日志_MySQL

一、理论:
1.错误日志:
a.记录了mysqld启动和停止时以及出错时的相关信息,当数据库出现故障导致无法启动时可以先查看此信息。
b.可用--log-error来指定mysqld保存错误日志文件的位置
2.二进制日志:
a.statement:记录的都是语句。优点:日志记录清晰易读、日志量少、对I/O影响较小,缺点:在某些情况下slave的日志复制会出错
b.row:将每一行的变更记录到日志中,而不是记录sql语句。优点:记录每一行的数据变化细节,不会出现某些情况下无法复制的情况,缺点:日志量大,对I/O影响较大
c.mixed:目前mysql的默认日志格式。尽可能对上两种模式的优点加以利用而避开它们的缺点
d.可以在global和session级别对binlog_format进行日志格式的操作,确保从库的复制能够正常进行
3.日志的读取:
a.mysqlbinlog工具
4.日志的删除:
a.reset master.可以删除所有的binlog日志
b.purge master logs to 'mysql-bin.*',将删除*编号前的所有日志
c.purge master logs before 'yyyy-mm-dd hh24:mi:ss',将删除日期为指定日期之前的所有日志
d.--expire_logs_days=#:设置日志的过期天数
5.其他选项:
a.--binlog-do-db=db_name:仅更新db_name数据库记录到二进制日志中而不更新其他数据库
b.--binlog-ignore-db=db_name:忽略db_name数据库记录到二进制日志中
c.--innodb-safe-binlog:与--sync-binlog=N(每写N次日志同步磁盘)一起配合使用,使得事务在日志中的记录更加安全
d.sql_log_bin=0:具有super权限的客户端可以通过设置此值使得禁止将自己的操作写入二进制记录。但有可能会导致主从数据不一致
6.日志的读取:查询日志记录的格式是纯文本,所以可以直接进行读取
7.慢查询日志:
a.记录了所有时间超过long_query_time的设置值并且扫描记录数不小于in_examined_row_limit的所有sql语句的日志
b.默认情况下,管理语句和不使用索引进行查询的语句不会记录到慢查询日志
c.--slow_query_log指定慢查询的状态,--show_query_log_file指定慢查询输出的路径,--log-out指定输出慢查询的方式(输出到表则只能精确到秒,输出到文件则能精确到微秒)
8.日志的读取:
a.查询long_query_time的值:show variables like 'long%';
b.set long_query_time:设置long_query_time的值
c.more localhost-slow.log:查看慢查询日志的值
9.其他查看日志的相关工具:
a.mysqlsla:查看日志

b.sqlprofi,mysql-expain-slow-log,mysqllogfilter:分析日志

二、实践:

 

mysql> use test;
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> create table emp(
    -> id int(11),
    -> info varchar(20)
    -> ) engine = innnodb charset = utf8;
Query OK, 0 rows affected, 2 warnings (0.05 sec)

mysql> insert into emp values(1,'z1');
Query OK, 1 row affected (0.00 sec)

mysql> insert into emp values(1,'z2');
Query OK, 1 row affected (0.00 sec)

mysql> exit

abc@ubuntu:~/Downloads/mysql$ mysql -uroot -p123 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 98
Server version: 5.5.44-log Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> show global variables like '%log%';
+-----------------------------------------+---------------------------------------+
| Variable_name                           | Value                                 |
+-----------------------------------------+---------------------------------------+
| back_log                                | 50                                    |
| binlog_cache_size                       | 32768                                 |
| binlog_direct_non_transactional_updates | OFF                                   |
| binlog_format                           | STATEMENT                             |
| binlog_stmt_cache_size                  | 32768                                 |
| expire_logs_days                        | 14                                    |
| general_log                             | OFF                                   |
| general_log_file                        | /usr/local/mysql/data/ubuntu.log      |
| innodb_flush_log_at_trx_commit          | 2                                     |
| innodb_locks_unsafe_for_binlog          | OFF                                   |
| innodb_log_buffer_size                  | 8388608                               |
| innodb_log_file_size                    | 67108864                              |
| innodb_log_files_in_group               | 2                                     |
| innodb_log_group_home_dir               | ./                                    |
| innodb_mirrored_log_groups              | 1                                     |
| log                                     | OFF                                   |
| log_bin                                 | ON                                    |
| log_bin_trust_function_creators         | OFF                                   |
| log_error                               | /usr/local/mysql/data/mysql-error.log |
| log_output                              | FILE                                  |
| log_queries_not_using_indexes           | ON                                    |
| log_slave_updates                       | OFF                                   |
| log_slow_queries                        | ON                                    |
| log_warnings                            | 1                                     |
| max_binlog_cache_size                   | 18446744073709547520                  |
| max_binlog_size                         | 1073741824                            |
| max_binlog_stmt_cache_size              | 18446744073709547520                  |
| max_relay_log_size                      | 0                                     |
| relay_log                               |                                       |
| relay_log_index                         |                                       |
| relay_log_info_file                     | relay-log.info                        |
| relay_log_purge                         | ON                                    |
| relay_log_recovery                      | OFF                                   |
| relay_log_space_limit                   | 0                                     |
| slow_query_log                          | ON                                    |
| slow_query_log_file                     | /usr/local/mysql/data/mysql-slow.log  |
| sql_log_bin                             | ON                                    |
| sql_log_off                             | OFF                                   |
| sync_binlog                             | 1                                     |
| sync_relay_log                          | 0                                     |
| sync_relay_log_info                     | 0                                     |
+-----------------------------------------+---------------------------------------+
41 rows in set (0.02 sec)

    -> Ctrl-C -- exit!
Aborted
abc@ubuntu:~/Downloads/mysql$ mysqld --verbose --help | grep -A 1 'Default options'
151106 15:37:14 [Warning] option 'table_definition_cache': unsigned value 100 adjusted to 400
151106 15:37:14 [Note] mysqld (mysqld 5.5.44-log) starting as process 76330 ...
151106 15:37:14 [Warning] Can't create test file /usr/local/mysql/data/ubuntu.lower-test
151106 15:37:14 [Warning] Can't create test file /usr/local/mysql/data/ubuntu.lower-test
151106 15:37:14 [Warning] One can only use the --user switch if running as root

mysqld: File '/usr/local/mysql/data/mysql-bin.index' not found (Errcode: 13)
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf 
151106 15:37:14 [ERROR] Aborting

abc@ubuntu:~/Downloads/mysql$ mysqld --verbose --help | grep -A 1 'Default options'
151106 15:37:32 [Warning] option 'table_definition_cache': unsigned value 100 adjusted to 400
151106 15:37:32 [Note] mysqld (mysqld 5.5.44-log) starting as process 76335 ...
151106 15:37:32 [Warning] Can't create test file /usr/local/mysql/data/ubuntu.lower-test
151106 15:37:32 [Warning] Can't create test file /usr/local/mysql/data/ubuntu.lower-test
151106 15:37:32 [Warning] One can only use the --user switch if running as root

mysqld: File '/usr/local/mysql/data/mysql-bin.index' not found (Errcode: 13)
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf 
151106 15:37:32 [ERROR] Aborting

//以下文件就是mysql的本机上的配置文件
abc@ubuntu:~/Downloads/mysql$ vi /usr/local/mysql/etc/my.cnf 

mysql> select count(*) from payment p  left join customer c  on p.payment_id = c.customer_id;
+----------+
| count(*) |
+----------+
|    16049 |
+----------+
1 row in set (0.06 sec)

mysql> show variables like 'long%';
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 2.000000 |
+-----------------+----------+
1 row in set (0.00 sec)

mysql> set long_query_time = 0.05;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'long%';
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 0.050000 |
+-----------------+----------+
1 row in set (0.00 sec)

mysql> select count(*) from payment p  left join customer c  on p.payment_id = c.customer_id order by c.customer_id;
+----------+
| count(*) |
+----------+
|    16049 |
+----------+
1 row in set (0.07 sec)

mysql> Ctrl-C -- exit!
Aborted

abc@ubuntu:/usr/local/mysql/data$ sudo tail ./mysql-slow.log 
[sudo] password for abc: 
# Time: 151106 23:40:39
# User@Host: root[root] @ localhost []
# Query_time: 0.047216  Lock_time: 0.000099 Rows_sent: 1  Rows_examined: 16650
SET timestamp=1446882039;
select count(*) from payment p  left join customer c  on p.payment_id = c.customer_id;
# Time: 151106 23:41:02
# User@Host: root[root] @ localhost []
# Query_time: 0.075027  Lock_time: 0.000083 Rows_sent: 1  Rows_examined: 32699
SET timestamp=1446882062;
select count(*) from payment p  left join customer c  on p.payment_id = c.customer_id order by c.customer_id;
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
MySQL的许可与其他数据库系统相比如何?MySQL的许可与其他数据库系统相比如何?Apr 25, 2025 am 12:26 AM

MySQL使用的是GPL许可证。1)GPL许可证允许自由使用、修改和分发MySQL,但修改后的分发需遵循GPL。2)商业许可证可避免公开修改,适合需要保密的商业应用。

您什么时候选择InnoDB而不是Myisam,反之亦然?您什么时候选择InnoDB而不是Myisam,反之亦然?Apr 25, 2025 am 12:22 AM

选择InnoDB而不是MyISAM的情况包括:1)需要事务支持,2)高并发环境,3)需要高数据一致性;反之,选择MyISAM的情况包括:1)主要是读操作,2)不需要事务支持。InnoDB适合需要高数据一致性和事务处理的应用,如电商平台,而MyISAM适合读密集型且无需事务的应用,如博客系统。

在MySQL中解释外键的目的。在MySQL中解释外键的目的。Apr 25, 2025 am 12:17 AM

在MySQL中,外键的作用是建立表与表之间的关系,确保数据的一致性和完整性。外键通过引用完整性检查和级联操作维护数据的有效性,使用时需注意性能优化和避免常见错误。

MySQL中有哪些不同类型的索引?MySQL中有哪些不同类型的索引?Apr 25, 2025 am 12:12 AM

MySQL中有四种主要的索引类型:B-Tree索引、哈希索引、全文索引和空间索引。1.B-Tree索引适用于范围查询、排序和分组,适合在employees表的name列上创建。2.哈希索引适用于等值查询,适合在MEMORY存储引擎的hash_table表的id列上创建。3.全文索引用于文本搜索,适合在articles表的content列上创建。4.空间索引用于地理空间查询,适合在locations表的geom列上创建。

您如何在MySQL中创建索引?您如何在MySQL中创建索引?Apr 25, 2025 am 12:06 AM

toCreateAnIndexinMysql,usethecReateIndexStatement.1)forasingLecolumn,使用“ createIndexIdx_lastNameEnemployees(lastName); 2)foracompositeIndex,使用“ createIndexIndexIndexIndexIndexDx_nameOmplayees(lastName,firstName,firstName);” 3)forauniqe instex,creationexexexexex,

MySQL与Sqlite有何不同?MySQL与Sqlite有何不同?Apr 24, 2025 am 12:12 AM

MySQL和SQLite的主要区别在于设计理念和使用场景:1.MySQL适用于大型应用和企业级解决方案,支持高性能和高并发;2.SQLite适合移动应用和桌面软件,轻量级且易于嵌入。

MySQL中的索引是什么?它们如何提高性能?MySQL中的索引是什么?它们如何提高性能?Apr 24, 2025 am 12:09 AM

MySQL中的索引是数据库表中一列或多列的有序结构,用于加速数据检索。1)索引通过减少扫描数据量提升查询速度。2)B-Tree索引利用平衡树结构,适合范围查询和排序。3)创建索引使用CREATEINDEX语句,如CREATEINDEXidx_customer_idONorders(customer_id)。4)复合索引可优化多列查询,如CREATEINDEXidx_customer_orderONorders(customer_id,order_date)。5)使用EXPLAIN分析查询计划,避

说明如何使用MySQL中的交易来确保数据一致性。说明如何使用MySQL中的交易来确保数据一致性。Apr 24, 2025 am 12:09 AM

在MySQL中使用事务可以确保数据一致性。1)通过STARTTRANSACTION开始事务,执行SQL操作后用COMMIT提交或ROLLBACK回滚。2)使用SAVEPOINT可以设置保存点,允许部分回滚。3)性能优化建议包括缩短事务时间、避免大规模查询和合理使用隔离级别。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能