自动清理MySQL binlog日志与手动删除的设置
以下的文章主要讲述的是对自动清理MySQL binlog日志与手动删除的实际解决方案的设置, 我们大家都知道MySQL数据库从复制(replication)采用了RBR 模式之后,binlog 的格式为ROW,其主要作用是解决很多原先出现的主键重复问题。 在一个繁忙的master db server
以下的文章主要讲述的是对自动清理MySQL binlog日志与手动删除的实际解决方案的设置, 我们大家都知道MySQL数据库从复制(replication)采用了RBR 模式之后,binlog 的格式为"ROW",其主要作用是解决很多原先出现的主键重复问题。
在一个繁忙的master db server上,MySQL binlog日志文件增长速度很快,如果不定时清除,硬盘空间很快就会被充满。
设置自动清理MySQL binlog日志,配置my.cnf:
expire_logs_days = 10
在运行时修改:
<ol class="dp-xml"> <li class="alt"><span><span>show binary logs; </span></span></li> <li><span>show variables like '%log%'; </span></li> <li class="alt"> <span>set global </span><span class="attribute">expire_logs_days</span><span> = </span><span class="attribute-value">10</span><span>; </span> </li> </ol>
清除之前可以采用相应的备份策略。
手动删除10天前的MySQL binlog日志:
<ol class="dp-xml"> <li class="alt"><span><span>PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY); </span></span></li> <li><span>show master logs; </span></li> </ol>
MASTER和BINARY是同义词。
一般情况下,推荐使用MIXED binlog的复制。http://dev.MySQL.com/doc/refman/5.1/en/open-bugs-general.html中的说明:Replication uses query-level logging: The master writes the executed queries to the binary logThis is a very fast, compact, and efficient logging method that works perfectly in most cases
附:关于MySQL复制的几种模式
从 MySQL 5.1.12 开始,可以用以下三种模式来实现:
基于SQL语句的复制(statement-based replication, SBR),
基于行的复制(row-based replication, RBR),
混合模式复制(mixed-based replication, MBR)。
相应地,binlog的格式也有三种:STATEMENT,ROW,MIXED。 MBR 模式中,SBR 模式是默认的。
在运行时可以动态改动 binlog的格式,除了以下几种情况:
存储流程或者触发器中间
启用了NDB
当前会话试用 RBR 模式,并且已打开了临时表
如果binlog采用了 MIXED 模式,那么在以下几种情况下会自动将MySQL binlog的模式由 SBR 模式改成 RBR 模式。
当DML语句更新一个NDB表时
当函数中包含 UUID() 时
2个及以上包含 AUTO_INCREMENT 字段的表被更新时
行任何 INSERT DELAYED 语句时
用 UDF 时
视图中必须要求运用 RBR 时,例如建立视图是运用了 UUID() 函数
设定主从复制模式:
<ol class="dp-xml"> <li class="alt"> <span><span class="attribute">log-bin</span><span>=</span></span>MySQL<span><span>-bin </span></span> </li> <li> <span>#</span><span class="attribute">binlog_format</span><span>=</span><span class="attribute-value">"STATEMENT"</span><span> </span> </li> <li class="alt"> <span>#</span><span class="attribute">binlog_format</span><span>=</span><span class="attribute-value">"ROW"</span><span> </span> </li> <li> <span class="attribute">binlog_format</span><span>=</span><span class="attribute-value">"MIXED"</span><span> </span> </li> </ol>
也可以在运行时动态修改binlog的格式。例如
<ol class="dp-xml"> <li class="alt">MySQL<span><span class="tag">></span><span> SET SESSION </span><span class="attribute">binlog_format</span><span> = </span><span class="attribute-value">'STATEMENT'</span><span>; </span></span> </li> <li>MySQL<span class="tag">></span><span> SET SESSION </span><span class="attribute">binlog_format</span><span> = </span><span class="attribute-value">'ROW'</span><span>; </span> </li> <li class="alt">MySQL<span class="tag">></span><span> SET SESSION </span><span class="attribute">binlog_format</span><span> = </span><span class="attribute-value">'MIXED'</span><span>; </span> </li> <li>MySQL<span class="tag">></span><span> SET GLOBAL </span><span class="attribute">binlog_format</span><span> = </span><span class="attribute-value">'STATEMENT'</span><span>; </span> </li> <li class="alt">MySQL<span class="tag">></span><span> SET GLOBAL </span><span class="attribute">binlog_format</span><span> = </span><span class="attribute-value">'ROW'</span><span>; </span> </li> <li>MySQL<span class="tag">></span><span> SET GLOBAL </span><span class="attribute">binlog_format</span><span> = </span><span class="attribute-value">'MIXED'</span><span>; </span> </li> </ol>
两种模式各自的优缺点:
SBR 的优点:
历史悠久,技能成熟
binlog文件较小
binlog中包含了所有数据库修改信息,可以据此来审核数据库的安全等情况
MySQL binlog可以用于实时的还原,而不仅仅用于复制
主从版本可以不一样,从服务器版本可以比主服务器版本高
SBR 的缺点:
不是所有的UPDATE语句都能被复制,尤其是包含不确定操作的时候。
调用具有不确定因素的 UDF 时复制也可能出疑问
运用以下函数的语句也不能被复制:
LOAD_FILE()
UUID()
USER()
FOUND_ROWS()
SYSDATE() (除非启动时启用了 –sysdate-is-now 选项)
INSERT … SELECT 会产生比 RBR 更多的行级锁
复制须要执行 全表扫描(WHERE 语句中没有运用到索引)的 UPDATE 时,须要比 RBR 请求更多的行级锁
对于有 AUTO_INCREMENT 字段的 InnoDB表而言,INSERT 语句会阻塞其他 INSERT 语句
对于一些复杂的语句,在从服务器上的耗资源情况会更严重,而 RBR 模式下,只会对那个发生变化的记录产生影响
存储函数(不是存储流程 )在被调用的同时也会执行一次 NOW() 函数,这个可以说是坏事也可能是好事
确定了的 UDF 也须要在从服务器上执行
数据表必须几乎和主服务器保持一致才行,否则可能会导致复制出错
执行复杂语句如果出错的话,会消耗更多资源
RBR 的优点:
任何情况都可以被复制,这对复制来说是最安全可靠的
和其他大多数数据库系统的复制技能一样
多数情况下,从服务器上的表如果有主键的话,复制就会快了很多
复制以下几种语句时的行锁更少:
INSERT … SELECT
包含 AUTO_INCREMENT 字段的 INSERT
没有附带条件或者并没有修改很多记录的 UPDATE 或 DELETE 语句
执行 INSERT,UPDATE,DELETE 语句时锁更少
从服务器上采用多线程来执行复制成为可能
RBR 的缺点:
binlog 大了很多
复杂的回滚时 binlog 中会包含大量的数据
主服务器上执行 UPDATE 语句时,所有发生变化的记录都会写到 binlog 中,而 SBR 只会写一次,这会导致频繁发生 binlog 的并发写疑问
UDF 产生的大 BLOB 值会导致复制变慢
不能从 binlog 中看到都复制了写什么语句(加密过的)
当在非事务表上执行一段堆积的SQL语句时,最好采用 SBR 模式,否则很容易导致主从服务器的数据不一致情况发生
另外,针对系统库 MySQL 里面的表发生变化时的处理准则如下:
如果是采用 INSERT,UPDATE,DELETE 直接操作表的情况,则日志格式根据 MySQL binlog_format 的设定而记录
如果是采用 GRANT,REVOKE,SET PASSWORD 等管理语句来做的话,那么无论如何 都采用 SBR 模式记录。
注:采用 RBR 模式后,能处理很多原先出现的主键重复问题。实例:
对于insert into db_allot_ids select from db_allot_ids 这个语句:
在BINLOG_FORMAT=STATEMENT 模式下:
BINLOG日志信息为:
<ol class="dp-xml"> <li class="alt"><span><span>BEGIN </span></span></li> <li><span>/*!*/; </span></li> <li class="alt"><span># at 173 </span></li> <li> <span>#090612 16:05:42 server id 1 end_log_pos 288 Query </span><span class="attribute">thread_id</span><span>=</span><span class="attribute-value">4</span><span> </span><span class="attribute">exec_time</span><span>=</span><span class="attribute-value">0</span><span> </span><span class="attribute">error_code</span><span>=</span><span class="attribute-value">0</span><span> </span> </li> <li class="alt"> <span>SET </span><span class="attribute">TIMESTAMP</span><span>=</span><span class="attribute-value">1244793942</span><span>/*!*/; </span> </li> <li><span>insert into db_allot_ids select * from db_allot_ids </span></li> <li class="alt"><span>/*!*/; </span></li> </ol>
在BINLOG_FORMAT=ROW 模式下:
BINLOG日志信息为:
<ol class="dp-xml"> <li class="alt"><span><span>BINLOG ' </span></span></li> <li><span>hA0yShMBAAAAMwAAAOAAAAAAAA8AAAAAAAAAA1NOUwAMZGJfYWxsb3RfaWRzAAIBAwAA </span></li> <li class="alt"> <span>hA0yShcBAAAANQAAABUBAAAQAA8AAAAAAAEAAv/</span><span class="attribute">8AQEAAAD8AQEAAAD8AQEAAAD8AQEAAAA</span><span>= </span> </li> <li><span>'/*!*/; </span></li> </ol>
以上的相关内容就是对设置自动清理MySQL binlog日志和手动删除的方法的介绍,望你能有所收获。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

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

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.

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

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.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

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.
