Home Database Mysql Tutorial mysql学习记录(二十五)--mysql日志_MySQL

mysql学习记录(二十五)--mysql日志_MySQL

May 30, 2016 pm 05:10 PM
log

一、理论:
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;
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is event ID 6013 in win10? What is event ID 6013 in win10? Jan 09, 2024 am 10:09 AM

The logs of win10 can help users understand the system usage in detail. Many users must have encountered log 6013 when looking for their own management logs. So what does this code mean? Let’s introduce it below. What is win10 log 6013: 1. This is a normal log. The information in this log does not mean that your computer has been restarted, but it indicates how long the system has been running since the last startup. This log will appear once every day at 12 o'clock sharp. How to check how long the system has been running? You can enter systeminfo in cmd. There is one line in it.

Logger buffer size what is log used for Logger buffer size what is log used for Mar 13, 2023 pm 04:27 PM

The function is to provide engineers with feedback on usage information and records to facilitate problem analysis (used during development); because users themselves do not often generate upload logs, they are useless to users. The logging buffer is a small, temporary area used for short-term storage of change vectors for redo logs to be written to disk. A log buffer write to disk is a batch of change vectors from multiple transactions. Even so, the change vector in the log buffer is written to disk in near real-time, and when the session issues a COMMIT statement, the log buffer write operation is performed in real time.

Troubleshooting Event 7034 Error Log Issues in Win10 Troubleshooting Event 7034 Error Log Issues in Win10 Jan 11, 2024 pm 02:06 PM

The logs of win10 can help users understand the system usage in detail. Many users must have seen a lot of error logs when looking for their own management logs. So how to solve them? Let’s take a look below. . How to solve win10 log event 7034: 1. Click "Start" to open "Control Panel" 2. Find "Administrative Tools" 3. Click "Services" 4. Find HDZBCommServiceForV2.0, right-click "Stop Service" and change it to "Manual Start "

How to use logging in ThinkPHP6 How to use logging in ThinkPHP6 Jun 20, 2023 am 08:37 AM

With the rapid development of the Internet and Web applications, log management is becoming more and more important. When developing web applications, how to find and locate problems is a very critical issue. A logging system is a very effective tool that can help us achieve these tasks. ThinkPHP6 provides a powerful logging system that can help application developers better manage and track events that occur in applications. This article will introduce how to use the logging system in ThinkPHP6 and how to utilize the logging system

Detailed explanation of log viewing command in Linux system! Detailed explanation of log viewing command in Linux system! Mar 06, 2024 pm 03:55 PM

In Linux systems, you can use the following command to view the contents of the log file: tail command: The tail command is used to display the content at the end of the log file. It is a common command to view the latest log information. tail [option] [file name] Commonly used options include: -n: Specify the number of lines to be displayed, the default is 10 lines. -f: Monitor the file content in real time and automatically display the new content when the file is updated. Example: tail-n20logfile.txt#Display the last 20 lines of the logfile.txt file tail-flogfile.txt#Monitor the updated content of the logfile.txt file in real time head command: The head command is used to display the beginning of the log file

How to view your medication log history in the Health app on iPhone How to view your medication log history in the Health app on iPhone Nov 29, 2023 pm 08:46 PM

iPhone lets you add medications to the Health app to track and manage the medications, vitamins and supplements you take every day. You can then log medications you've taken or skipped when you receive a notification on your device. After you log your medications, you can see how often you took or skipped them to help you track your health. In this post, we will guide you to view the log history of selected medications in the Health app on iPhone. A short guide on how to view your medication log history in the Health App: Go to the Health App>Browse>Medications>Medications>Select a Medication>Options&a

Understand the meaning of event ID455 in win10 logs Understand the meaning of event ID455 in win10 logs Jan 12, 2024 pm 09:45 PM

The logs of win10 have a lot of rich content. Many users must have seen the event ID455 display error when looking for their own management logs. So what does it mean? Let’s take a look below. What is event ID455 in the win10 log: 1. ID455 is the error <error> that occurred in <file> when the information store opened the log file.

Three commands to view logs in Linux Three commands to view logs in Linux Jan 04, 2023 pm 02:00 PM

The three commands for viewing logs in Linux are: 1. tail command, which can view changes in file content and log files in real time; 2. multitail command, which can monitor multiple log files at the same time; 3. less command, which can Changes to the log can be viewed quickly without cluttering the screen.

See all articles