MySQL复制(一) --- 二进制日志干什么的_MySQL
bitsCN.com
与小站点相比,大型站点的数据库管理员,需要提前做好以下的事情:
提供灾难发生时核心业务数据的恢复计划。理论上这个过程至少需要执行一次。
通过采集大量用户数据并监控网站各节点的负载,提供优化计划
当用户数量急剧增长时的快速横向扩展计划
对于所有这些事情,提前计划并准备好必要时的快速应对是很重要的。
为了保证站点可响应和可用性,需要做两件事:系统的数据备份和冗余。备份可以将节点恢复到它崩溃之前的状态,备份根据需求有几种比如及时恢复(PITR:point-in-time-recovery),在线备份等等;而冗余则保证即使在一个或更多节点停止服务的情况下,站点仍能继续运行,备份一般通过硬件副本来实现,让几个实例并行运行,并通过复制在几个机器上保存相同数据的多个可用副本。复制的主要应用场景:1)针对高读写比的,scale out;2)添加冗余保证高可用性,比如双主配置(dual-master setup)。
二进制日志(binlog)
复制过程需要二进制日志。二进制日志的目的是记录数据库中表的更改,然后用于复制和PITR,另外少数审计情况下也会用到。
传统意义上说,MySQL复制记录了产生变化的SQL语句,称为基于语句的复制(statement-based replication)。基于语句的复制的缺点是无法保证所有语句都正确复制。所以在5.1版本中,MySQL还提供了基于行的复制(row-based replication)。
查看二进制日志:
# 强制把缓存的东西刷到LOGS中,并产生一个Rotate事件写入binlog中FLUSH LOGS;# 正常使用很久的DB不建议使用这个命令,需要加参数,指定具体的binlog文件名 【IN 'xxxx'】SHOW BINLOG EVENTS/G
二进制日志中事件所包含的字段:
Event_type: 比如Format_desc, Query, RotateServer_id : 创建事件的服务器idLog_name : 存储事件的文件名,一个事件只能存储在一个文件中Pos : 事件在文件中的开始位置,及事件的第一个字节End_log_pos:事件在文件中的结束位置,也就是下一个事件的开始位置Info : 具体事件的信息,Query的时候就是SQL语句
二进制日志的结构和内容:
二进制日志不是一个的单独的文件,它包括一组存储实际内容的二进制日志文件和一个二进制日志索引文件。每个二进制日志文件都以format description event开始,以rotate event结束。rotate event包含下一个二进制日志文件的名称,以告知二进制日志继续写入哪个文件。因此FLUSH LOGS的时候会新建一个新binlog文件。
获取当前正在写入的是哪一个二进制日志文件:
SHOW MASTER STATUS/G
RESET MASTER命令删除了所有的二进制日志文件并清空了二进制日志索引文件。RESET SLAVE命令删除了Slave复制所用的所有文件,重新开始。
CHANGE MASTER TO命令用于改变slave连接master的一些参数,其中就包括slave读取master二进制日志文件的参数。比如使用MASTER_LOG_FILE和MASTER_LOG_POS来指定master开始发送事件的binlog位置。
如何建立新Slave
1:配置新的Slave2:备份Master(或者备份已经复制了Master的Slave)3:接下该备份相应的binlog位置4:在新Slave上恢复备份5:配置Slave从这个binlog位置开始复制
区别就在于第二步,一种是直接从Master进行备份,一种是通过现有Slave备份,下面分别介绍两种。
1:克隆Master:
# 刷新所有的表并锁定数据库,防止在检查binlog位置之前数据库发生改变FLUSH TABLES WITH READ LOCK;# 获取当前的binlog文件和posSHOW MASTER STATUS/G# 备份mastermysqldump --all-databases --host=master-1 >backup.sql# 解锁UNLOCK TABLES;# 在slave上恢复备份mysql --host=slave-1
其实mysqldump命令提供了master_data选项,自动把MASTER_LOG_FILE和MASTER_LOG_POS信息dump到backup.sql中
另外:FLUSH TABLES WITH READ LOCK对于InnoDB是不安全的,因为虽然会锁表,不会产生新事务,但是后台仍然有一些活动在继续进行。
所以安全的创建InnoDB数据表的备份可以使用下面的方法。
1:关闭服务器,然后复制文件。如果数据库很大 ,最好采取这种方法,因为这时使用mysqldump进行数据恢复会很慢2:执行FLUSH TABLES WITH READ LOCK之后,使用mysqldump3:执行FLUSH TABLES WITH READ LOCK之后,使用快照的方法,比如LVM(Linux),ZFS(Solaris)快照
2:克隆Slave:
# 防止出现不一致的备份映像,备份Slave之前需要先停止replicationSTOP SLAVE;# 确定从哪里开始复制,注意Relay_Master_Log_File和Exec_Master_Log_PosSHOW SLAVE STATUS/G# 配置新的slave,指向masterCHANGE MASTER TOMASTER_HOST = 'master-1',MASTER_PORT = 3306,MASTER_USER = 'slave-1',MASTER_PASSWORD = 'xxxx',MASTER_LOG_FILE = 'master-bin.000042',MASTER_LOG_POS = 546632;# 启动新的slaveSTART SLAVE
摘自 foxracle
bitsCN.com
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

AI Hentai Generator
Generate AI Hentai for free.

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



With the rapid development of social media, Xiaohongshu has become one of the most popular social platforms. Users can create a Xiaohongshu account to show their personal identity and communicate and interact with other users. If you need to find a user’s Xiaohongshu number, you can follow these simple steps. 1. How to use Xiaohongshu account to find users? 1. Open the Xiaohongshu APP, click the "Discover" button in the lower right corner, and then select the "Notes" option. 2. In the note list, find the note posted by the user you want to find. Click to enter the note details page. 3. On the note details page, click the "Follow" button below the user's avatar to enter the user's personal homepage. 4. In the upper right corner of the user's personal homepage, click the three-dot button and select "Personal Information"

The Local Users and Groups utility is built into Computer Management and can be accessed from the console or independently. However, some users find that local users and groups are missing in Windows 11. For some people who have access to it, the message suggests that this snap-in may not work with this version of Windows 10. To manage user accounts for this computer, use the User Accounts tool in Control Panel. The issue has been reported in previous iterations of Windows 10 and is usually caused by issues or oversights on the user's side. Why are local users and groups missing in Windows 11? You are running Windows Home edition, local users and groups are available on Professional edition and above. Activity

In Ubuntu systems, the root user is usually disabled. To activate the root user, you can use the passwd command to set a password and then use the su- command to log in as root. The root user is a user with unrestricted system administrative rights. He has permissions to access and modify files, user management, software installation and removal, and system configuration changes. There are obvious differences between the root user and ordinary users. The root user has the highest authority and broader control rights in the system. The root user can execute important system commands and edit system files, which ordinary users cannot do. In this guide, I'll explore the Ubuntu root user, how to log in as root, and how it differs from a normal user. Notice

Certain folders are not always accessible due to permissions, and in today’s guide we will show you how to access user folders on your old hard drive on Windows 11. The process is simple but can take a while, sometimes even hours, depending on the size of the drive, so be extra patient and follow the instructions in this guide closely. Why can't I access my user folders on my old hard drive? User folders are owned by another computer, so you cannot modify them. You don't have any permissions on the folder other than ownership. How to open user files on old hard drive? 1. Take ownership of the folder and change permissions Find the old user directory, right-click on it and select Properties. Navigate to "An

Many users have been added to the Ubuntu system. I want to delete the users that are no longer in use. How to delete them? Let’s take a look at the detailed tutorial below. 1. Open the terminal command line and use the userdel command to delete the specified user. Be sure to add the sudo permission command, as shown in the figure below. 2. When deleting, be sure to be in the administrator directory. Ordinary users do not have this permission. , as shown in the figure below 3. After the delete command is executed, how to judge whether it has been truly deleted? Next we use the cat command to open the passwd file, as shown in the figure below 4. We see that the deleted user information is no longer in the passwd file, which proves that the user has been deleted, as shown in the figure below 5. Then we enter the home file

sudo (superuser execution) is a key command in Linux and Unix systems that allows ordinary users to run specific commands with root privileges. The function of sudo is mainly reflected in the following aspects: Providing permission control: sudo achieves strict control over system resources and sensitive operations by authorizing users to temporarily obtain superuser permissions. Ordinary users can only obtain temporary privileges through sudo when needed, and do not need to log in as superuser all the time. Improved security: By using sudo, you can avoid using the root account during routine operations. Using the root account for all operations may lead to unexpected system damage, as any mistaken or careless operation will have full permissions. and

Microsoft began rolling out KB2 to the public as an optional update for Windows 503145511H22 or later. This is the first update to enable Windows 11 Moment 4 features by default, including Windows Copilot in supported areas, preview support for items in the Start menu, ungrouping of the taskbar, and more. Additionally, it fixes several Windows 11 bugs, including potential performance issues that caused memory leaks. But ironically, the optional update for September 2023 will be a disaster for users trying to install the update, or even for those who have already installed it. Many users will not install this Wi

Analysis of user password storage mechanism in Linux system In Linux system, the storage of user password is one of the very important security mechanisms. This article will analyze the storage mechanism of user passwords in Linux systems, including the encrypted storage of passwords, the password verification process, and how to securely manage user passwords. At the same time, specific code examples will be used to demonstrate the actual operation process of password storage. 1. Encrypted storage of passwords In Linux systems, user passwords are not stored in the system in plain text, but are encrypted and stored. L
