Home Database Mysql Tutorial mysql初识之日志文件篇_MySQL

mysql初识之日志文件篇_MySQL

Jun 01, 2016 pm 01:31 PM
mysql Record details

bitsCN.com

mysql初识之日志文件篇

 

日志文件

 

1. err日志

 

    error log 记录mysql在运行的过程中所有较为严重的警告和错误信息,以及mysql server每次启动和关闭的详细信息。系统在默认情况下关闭error log 功能。error log 默认存放在data目录下,默认文件名为主机名.err。error log 通过两种方式开启:

 

1). 启动mysqld时,附加上--log-error参数

 

2) . 在my.cnf中配置log-error系统环境变量

 

当进入mysql后执行 flush logs命令后,mysql会将当前的错误日志文件附加-old文件保存,并且创建一个新的空错误日志文件,仅仅在指定--log-error的情况下。

 

注:error log 不全是保存是警告和错误信息,还保存每次启动和关闭的信息。

 

ps启动时信息:130420 16:19:29 mysqld_safe mysqld from pid file /data0/mysql_data_7706/FZTEST-24178.pid ended130420 16:19:30 mysqld_safe Starting mysqld daemon with databases from /data0/mysql_data_7706error: Found option without preceding group in config file: /data0/mysql_data_7706/my.cnf at line: 1Fatal error in defaults handling. Program aborted130420 16:19:30 [Note] Plugin 'FEDERATED' is disabled.130420 16:19:30 [Note] Plugin 'ndbcluster' is disabled.130420 16:19:30  InnoDB: Initializing buffer pool, size = 8.0M130420 16:19:30  InnoDB: Completed initialization of buffer pool(以下省略) 查看error 日志是否启动:mysql> show variables like 'log_error';+---------------+---------------------+| Variable_name | Value                        |+---------------+---------------------+| log_error          | /var/log/mysqld.log |+---------------+---------------------+1 row in set (0.00 sec)关闭mysql:  mysqladmin -uroot -p shutdown 
Copy after login

2. 二进制日志(binlog)

binlog日志主要记录mysql的更新内容,记录每一条更新语句的执行的时间、消耗的资源,以及相关的事务信息。可以用于实时备份,与master/slave复制。

如何打开:

my.cnf中[mysqld] 节点上添加

查看binlog是否打开

mysql> show variables  like 'log_%';+---------------------------------+-----------------+| Variable_name                   | Value           |+---------------------------------+-----------------+| log_bin                             | ON              |查看binlog内容: /usr/local/mysql3306/bin/mysqlbinlog mysql-bin.00002log-bin-index文件作用:记录目录所有binlog文件[root@FZTEST-24178 mysql_data_7706]# cat mysql_7706-relay-bin.index./mysql_7706-relay-bin.000032binlog格式说明:[root@localhost ~]# mysqlbinlog  /home/mysql/binlog/binlog.000003  | more/*!40019 SET @@session.max_insert_delayed_threads=0*/;/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;DELIMITER /*!*/;# at 4#120330 16:51:46 server id 1  end_log_pos 98    Start: binlog v 4, server v 5.0.45-log created 120330 16:51:46# Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.# at 196#120330 17:54:15 server id 1  end_log_pos 294   Query   thread_id=3     exec_time=2     error_code=0SET TIMESTAMP=1333101255/*!*/;insert into tt7 select * from tt7/*!*/;# at 294#120330 17:54:46 server id 1  end_log_pos 388   Query   thread_id=3     exec_time=28    error_code=0SET TIMESTAMP=1333101286/*!*/;alter table tt7 engine=innodb/*!*/;
Copy after login

 

 

解析binlog格式

位置

位于文件中的位置,“at 196”说明“事件”的起点,是以第196字节开始;“end_log_pos 294”说明以第294字节结束

 

时间戳

事件发生的时间戳:“120330 17:54:46”

 

事件执行时间

事件执行花费的时间:"exec_time=28"

 

错误码

错误码为:“error_code=0”

 

服务器的标识

服务器的标识id:“server id 1”

 

其他参数说明:

1). binlog_do_db:表示记录指定数据库的二进制日志

2).binlog_ignore_db:表示忽略指定的数据库的二进制日志

3).  max_binlog_cache_size:表示使用binlog时最大的内存值

4). binlog_cache_size

此参数表示binlog使用的内存大小,可以通过状态变量binlog_cache_use和binlog_cache_disk_use来帮助测试。

       binlog_cache_use:使用二进制日志缓存的事务数量

       binlog_cache_disk_use:使用二进制日志缓存但超过binlog_cache_size值并使用临时文件来保存事务中的语句的事务数量

5).max_binlog_size

Binlog最大值,最大和默认值是1GB,该设置并不能严格控制Binlog的大小,尤其是Binlog比较靠近最大值而又遇到一个比较大事务时,为了保证事务的完整性,不可能做切换日志的动作,只能将该事务的所有SQL都记录进当前日志,直到事务结束

6).sync_binlog

这个参数直接影响mysql的性能和完整性

sync_binlog=0:

当事务提交后,Mysql仅仅是将binlog_cache中的数据写入Binlog文件,但不执行fsync之类的磁盘        同步指令通知文件系统将缓存刷新到磁盘,而让Filesystem自行决定什么时候来做同步,这个是性能最好的。

sync_binlog=n,在进行n次事务提交以后,Mysql将执行一次fsync之类的磁盘同步指令,同志文件系统将Binlog文件缓存刷新到磁盘。

Mysql中默认的设置是sync_binlog=0,即不作任何强制性的磁盘刷新指令,这时性能是最好的,但风险也是最大的。一旦系统绷Crash,在文件系统缓存中的所有Binlog信息都会丢失。

 

查询日志

 

查询日志即记录所有的查询语句的日志,一般建议不开启,有些query语句比较大,开启后对性能的也有较大的影响。一般用于跟踪某特殊 的性能问题才会短暂打开功能,默认的查询日志文件名为主机名.log。

 

慢查询日志

 

慢查询日志记录的是查询需要较长时间的query,通过在[mysqld] 下添加log-slow-queries=/tmp/slow_log 打开些功能,默认文件名是hostname-slow.log默认的目录是数据目录。

分析慢查询的工具有:msyqlslowdump、mysqlsla

 

innodb在线日志redo日志

 

innodb是一个事务安全的存储引擎,其事务安全性主要是通过在线redo日志和记录在表空间中的undo信息来保证redo日志中记录了innodb所做的所有物理变更和事务信息,通过redo日志和undo信息,innodb保证了在任何情况下的事务安全性。innodb的redo日志同样默认存放 在数据目录下,可通过innodb_log_group_home_dir来更改设置日志的存放公交车,通过innodb_log_files_in_group 设置日志的数量。

 

bitsCN.com
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

See all articles