Home Database Mysql Tutorial 分享一些简介操作MySQL日志的方法

分享一些简介操作MySQL日志的方法

Jun 01, 2016 pm 12:59 PM
mysql log

这篇文章主要介绍了操作MySQL日志的一些方法,对日志的删除操作做了重点讲解,需要的朋友可以参考下。

我们可以在mysql的安装目录下看到mysql的二进制日志文件,如mysql-bin.000***等,很多人都不及时的处理,导致整个硬盘被塞满也是有可能的。这些是数据库的操作日志。它记录了我们平时使用的sql语句命令,即使这个sql语句没有被执行,也会被记录,它包括执行时间之类的都详细在案,其实 它的目的有两个:一是方便我们查错和恢复数据库;二是为了主从服务器之间同步数据用的,主从服务器上所有操作都记录在日志里,从服务器可以根据这些日志来进行相同的操作,确保主从两个同步。
查看mysql日志的命令:

#mysql -u user -p   //user代表你的mysql用户名

Enter password: (输入密码)        //输入密码后就进入mysql的控制台

mysql> show master logs;       //显示mysql日志文件
+------------------+-----------+
| Log_name       | File_size |
+------------------+-----------+
| mysql-bin.000010 | 63328652 |
+------------------+-----------+
1 row in set (0.06 sec)
Copy after login

也可以在mysql的命令模式下,输入以下命令:

mysql> show binary logs;        //查看二进制日志文件列表和大小
+------------------+-----------+
| Log_name     | File_size |
+------------------+-----------+
| mysql-bin.000010 | 63355376 |
+------------------+-----------+
1 row in set (0.00 sec)
Copy after login

MASTER和BINARY是同义词。

删除mysql 二进制日志:

第一种方法是针对单独的mysql服务器,不适合主从同步的。切记!当然,如果你确保自己的主从同步已经完成了,那么也可以使用这个命令,但是我还是不怎么建议的。

第一种方法:mysql>reset master; //清空所有mysql所有的二进制日志文件

主从同步,建议用以下两种方法:

首先,我们要知道从mysql服务器是否在读取日志,或者在读取哪个日志。

在从属服务器上输入一下命令:

mysql>show slave hosts     //查看所有连接到master的slave的信息

mysql>show slave status;        //检查从服务器在读取哪个日志
Copy after login

下面第二种和第三种方法中,当从服务器正在读取你正在删除的日志时候,就会失效,并且会有报错。如果从属服务器没有读取日志,而你又删了它启动后要读取的日志,那么当它启动后就不会复制。所以,下面两种方法可以安全删除日志文件。

主从服务器清理日志操作步骤如下:

  • 在每个从属服务器上,使用SHOW SLAVE STATUS来检查它正在读取哪个日志。

  • 使用SHOW MASTER LOGS获得主服务器上的一系列日志。

  • 在所有的从属服务器中判定最早的日志,这个是目标日志,如果所有的从属服务器是更新的,就是清单上的最后一个日志。

  • 清理所有的日志,但是不包括目标日志,因为从服务器还要跟它同步。

注意:最好做好你要删除的所有日志的备份,有备份,就不怕出错

第二种删除二进制日志的方法:

PURGE {MASTER | BINARY} LOGS TO 'log_name'
Copy after login

例子:

mysql>purge master logs to 'mysql-bin.000800';  //清楚mysql-bin.000800之前的二进制日志
Copy after login

第三种删除二进制日志的方法:

PURGE {MASTER | BINARY} LOGS BEFORE 'date'
Copy after login

例子:

mysql>purge master logs before '2012-12-22 12:12:12'; //删除2012-12-22 12:12:12这时间之前的二进制文件
Copy after login

第四种方法:就是修改my.cnf文件,在中[mysqld]中添加:

expire_logs_day=5       //设置日志的过期天数,过了指定的天数,会自动删除
Copy after login

第五种方法:禁止mysql的日志记录功能(单服务器)

修改my.cnf,注释掉下面两行:

#log-bin=mysql-bin
#binlog_format=mixed
Copy after login

然后重启mysql

【相关教程推荐】

1. mysql数据库图文教程 

2. MySQL 5.1参考手册下载 

3. bootstrap教程 

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)

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

How to view sql database error How to view sql database error Apr 10, 2025 pm 12:09 PM

The methods for viewing SQL database errors are: 1. View error messages directly; 2. Use SHOW ERRORS and SHOW WARNINGS commands; 3. Access the error log; 4. Use error codes to find the cause of the error; 5. Check the database connection and query syntax; 6. Use debugging tools.

See all articles