Table of Contents
MySQL 复制配置主从同步
MySQL 复制配置双机互为主从
Home Database Mysql Tutorial MySQL5.5主从同步配置及问题

MySQL5.5主从同步配置及问题

Jun 07, 2016 pm 04:39 PM
Synchronize Install Configuration question

安装网上的一些文章配置MySQL的主从同步机制,无奈重启从MySQL时出现异常,说不认识参数master_host /usr/sbin/mysqld: unknown variable master_host=10.0.2.160 原来我使用的是MySQL5.5,而大部分配置是基于5.5之前的版本的,Mysql版本从5.1.7以后开始就不

安装网上的一些文章配置MySQL的主从同步机制,无奈重启从MySQL时出现异常,说不认识参数master_host

/usr/sbin/mysqld: unknown variable ‘master_host=10.0.2.160′

原来我使用的是MySQL5.5,而大部分配置是基于5.5之前的版本的,Mysql版本从5.1.7以后开始就不支持“master-host”类似的参数。详情可以查看http://dev.mysql.com/doc/refman/5.5/en/replication-howto.html

MySQL 复制配置主从同步

A B 为两台 MySQL 服务器,均开启二进制日志,数据库版本 MySQL 5.5。

在两台服务器上编辑配置文件,以下配置添加到[mysqld]

一.MySQL数据库迁移
首先如果要同步已经存在的MySQL数据,我们最好先通过全量同步到从库。
同步的方法可以参考:
1.
2.MySQL下的数据库迁移
二.服务器配置

[A 主服务器10.0.2.160]

server-id = 1
binlog-ignore-db = mysql
binlog-ignore-db =?information_schema
sync-binlog = 1
Copy after login

[B 从服务器10.0.2.151]

server-id = 2
replicate-ignore-db = mysql
replicate-ignore-db =?information_schema
Copy after login

重新启动MySQL服务。

使用命令?SHOW MASTER STATUS查看主服务状态:

mysql> SHOW MASTER STATUS;
+---------------------+----------+--------------+--------------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+---------------------+----------+--------------+--------------------------+
| mysql-binlog.000001 | 525      |              | mysql,information_schema |
+---------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
Copy after login

主服务器A授权同步账户:

GRANT REPLICATION SLAVE ON *.* TO 'root'@'10.0.2.151' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
Copy after login

slave?start开启复制:

CHANGE MASTER TO MASTER_HOST='10.0.2.160',MASTER_USER='root', MASTER_PASSWORD='password',MASTER_LOG_FILE='?mysql-binlog.000001',MASTER_LOG_POS=106;
Copy after login

停止slave同步:mysql>slave?start

停止slave同步:mysql> slave stop

在slave服务器上使用show slave status查看slave同步的状态

mysql主从同步

mysql主从同步

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

如果上面为yes表示同步成功。

很不幸的是我第一次实验,上述参数都为No

解决:MySQL主从同步位置不一致
1.首先停掉Slave服务:

mysql> slave stop
Copy after login

2.到主服务器上查看主机状态:
记录File和Position对应的值。

mysql> SHOW MASTER STATUS;
+---------------------+----------+--------------+--------------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+---------------------+----------+--------------+--------------------------+
| mysql-binlog.000002 |      958 |              | mysql,information_schema |
+---------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
Copy after login

3.到slave服务器上执行手动同步:

CHANGE MASTER TO MASTER_HOST='10.0.2.160',MASTER_USER='root', MASTER_PASSWORD='password',MASTER_LOG_FILE='?mysql-binlog.000002',MASTER_LOG_POS=958;
Copy after login

启动slave同步:slave start

再次查看slave状态发现:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Copy after login

如果上面为yes表示同步成功。

我们知道,因为DATA REPLICATION机制完全是基于在主上执行的增量SQL要被传播到辅服务器上,并且被成功运行。这就势必要求:在运行此机制前,主辅数据库中数据是一致的;以及在运行此机制中,辅数据库禁止来自其他的SQL(非主上传播过来SQL)的写操作。但是在运行中仍然可能遇到不一致的产生,这会导致通信无法正常继续下去。因此一旦主从出现问题,首先应该解决同步位置的问题,修复丢失的数据。

MySQL 复制配置双机互为主从

在两台服务器上编辑配置文件,以下配置添加到[mysqld]

一、服务器参数

[A 服务器 192.168.1.100]

server-id = 1
binlog-do-db = test
binlog-ignore-db = mysql
replicate-do-db = test
replicate-ignore-db = mysql
sync-binlog = 1
Copy after login

[B 服务器 192.168.1.101]

server-id = 2
binlog-do-db = test
binlog-ignore-db = mysql
replicate-do-db = test
replicate-ignore-db = mysql
sync-binlog = 1
Copy after login

二、操作步骤

# A B 服务器停止同步

STOP SLAVE;
Copy after login

# A B 服务器清空MASTER日志

RESET MASTER;
Copy after login

# A B 服务器清空SLAVE日志

RESET SLAVE;
Copy after login

# A 服务器授权同步账户 (我们会同步一次复制数据库文件,所以授权为192.168.1%)

GRANT REPLICATION SLAVE ON *.* TO 'master_slave'@'192.168.1%' IDENTIFIED BY 'master_slave123!@#';
FLUSH PRIVILEGES;
Copy after login

# A 服务器锁表(锁表状态下不能终止mysql进程,否则会失败)

FLUSH TABLES WITH READ LOCK;
Copy after login
Copy after login

# 如果使用SSH,需要重新开启,复制数据库文件。

tar -cvf /tmp/mysql-data.tar /www/mysql
tar -xvf /tmp/mysql-data.tar -C /
Copy after login

# 查看 A 服务器主机状态(记录二进制开始文件,位置)

SHOW MASTER STATUS;
Copy after login
Copy after login

# B 服务器锁表(锁表状态下不能终止mysql进程,否则会失败)

FLUSH TABLES WITH READ LOCK;
Copy after login
Copy after login

# 修改 B 服务器配置 (修改为A服务器的主机状态)

CHANGE MASTER TO MASTER_HOST='192.168.1.100',MASTER_USER='master_slave', MASTER_PASSWORD='master_slave123!@#',MASTER_LOG_FILE='binlog.000001',MASTER_LOG_POS=106;
Copy after login

# 开启 B 服务器同步进程

START SLAVE;
Copy after login
Copy after login

# 查看 B 服务器同步状态是否正常

SHOW SLAVE STATUS;
Copy after login

# 查看 B 服务器主机(记录二进制开始文件,位置)

SHOW MASTER STATUS;
Copy after login
Copy after login

# 修改 A 服务器配置 (修改为B服务器的主机状态)

CHANGE MASTER TO MASTER_HOST='192.168.1.101',MASTER_USER='master_slave',MASTER_PASSWORD='master_slave123!@#',MASTER_LOG_FILE='binlog.000001',MASTER_LOG_POS=106;
Copy after login

# 开启 A 服务器同步进程

START SLAVE;
Copy after login
Copy after login

# 分别查看 A B 服务器同步状态,确定是否成功

SHOW SLAVE STATUS;SHOW MASTER STATUS;
Copy after login

# 解锁 A B 服务器

UNLOCK TABLES;
Copy after login

# 数据测试分别在 A B 服务器上创建表插入数据测试

DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`value` varchar(100) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO `test` VALUES ('1', 'Hello');
Copy after login

注意:
1. 数据库目录下的master.info的内容会覆盖命令行或my.cnf中指定的部分选项,更改配置需删除master.info

2. my.cnf中的master配置在MySQL 6.0以后会取消,官方建议使用动态的CHANGE MASTER

3. 如果只指定ignore-db而不指定do-db。则新创建数据库的操作也会同步。

互为同步配置实例:

1. A B 互为主从同步test, 不同步mysql:

两个数据库配置中均设置:binlog-do-db=test, binlog-ignore-db=mysql,replicate-do-db=test,replicate-ignore-db=mysql

2. A B 互为主从只同步test,不同步其他数据库,新创建的也不会同步

两个数据库配置中均设置:binlog-do-db=test,replicate-do-db=test

3. A B 互为主从不同步mysql, 同步其他数据库,譬如创建的新数据库也会同步

两个数据库配置中均设置:binlog-ignore-db=mysql,replicate-ignore-db=mysql

4. A B 互为主从同步所有数据库,包括新建的数据库

两个数据库配置中均不设置上述四项。

参考:
官网:http://dev.mysql.com/doc/refman/5.5/en/replication-howto-mysqldump.html
5.17之前的版本配置:http://www.drupal001.com/2012/03/mysql-master-slave-troubles/
MySQL 复制配置双机互为主从:http://www.ncq8.com/2010/10/250.html

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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months 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)

Solution to the problem that Win11 system cannot install Chinese language pack Solution to the problem that Win11 system cannot install Chinese language pack Mar 09, 2024 am 09:48 AM

Solution to the problem that Win11 system cannot install Chinese language pack With the launch of Windows 11 system, many users began to upgrade their operating system to experience new functions and interfaces. However, some users found that they were unable to install the Chinese language pack after upgrading, which troubled their experience. In this article, we will discuss the reasons why Win11 system cannot install the Chinese language pack and provide some solutions to help users solve this problem. Cause Analysis First, let us analyze the inability of Win11 system to

Unable to install guest additions in VirtualBox Unable to install guest additions in VirtualBox Mar 10, 2024 am 09:34 AM

You may not be able to install guest additions to a virtual machine in OracleVirtualBox. When we click on Devices>InstallGuestAdditionsCDImage, it just throws an error as shown below: VirtualBox - Error: Unable to insert virtual disc C: Programming FilesOracleVirtualBoxVBoxGuestAdditions.iso into ubuntu machine In this post we will understand what happens when you What to do when you can't install guest additions in VirtualBox. Unable to install guest additions in VirtualBox If you can't install it in Virtua

One or more items in the folder you synced do not match Outlook error One or more items in the folder you synced do not match Outlook error Mar 18, 2024 am 09:46 AM

When you find that one or more items in your sync folder do not match the error message in Outlook, it may be because you updated or canceled meeting items. In this case, you will see an error message saying that your local version of the data conflicts with the remote copy. This situation usually happens in Outlook desktop application. One or more items in the folder you synced do not match. To resolve the conflict, open the projects and try the operation again. Fix One or more items in synced folders do not match Outlook error In Outlook desktop version, you may encounter issues when local calendar items conflict with the server copy. Fortunately, though, there are some simple ways to help

What should I do if Baidu Netdisk is downloaded successfully but cannot be installed? What should I do if Baidu Netdisk is downloaded successfully but cannot be installed? Mar 13, 2024 pm 10:22 PM

If you have successfully downloaded the installation file of Baidu Netdisk, but cannot install it normally, it may be that there is an error in the integrity of the software file or there is a problem with the residual files and registry entries. Let this site take care of it for users. Let’s introduce the analysis of the problem that Baidu Netdisk is successfully downloaded but cannot be installed. Analysis of the problem that Baidu Netdisk downloaded successfully but could not be installed 1. Check the integrity of the installation file: Make sure that the downloaded installation file is complete and not damaged. You can download it again, or try to download the installation file from another trusted source. 2. Turn off anti-virus software and firewall: Some anti-virus software or firewall programs may prevent the installation program from running properly. Try disabling or exiting the anti-virus software and firewall, then re-run the installation

How to install Android apps on Linux? How to install Android apps on Linux? Mar 19, 2024 am 11:15 AM

Installing Android applications on Linux has always been a concern for many users. Especially for Linux users who like to use Android applications, it is very important to master how to install Android applications on Linux systems. Although running Android applications directly on Linux is not as simple as on the Android platform, by using emulators or third-party tools, we can still happily enjoy Android applications on Linux. The following will introduce how to install Android applications on Linux systems.

Understand Linux Bashrc: functions, configuration and usage Understand Linux Bashrc: functions, configuration and usage Mar 20, 2024 pm 03:30 PM

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

How to install Podman on Ubuntu 24.04 How to install Podman on Ubuntu 24.04 Mar 22, 2024 am 11:26 AM

If you have used Docker, you must understand daemons, containers, and their functions. A daemon is a service that runs in the background when a container is already in use in any system. Podman is a free management tool for managing and creating containers without relying on any daemon such as Docker. Therefore, it has advantages in managing containers without the need for long-term backend services. Additionally, Podman does not require root-level permissions to be used. This guide discusses in detail how to install Podman on Ubuntu24. To update the system, we first need to update the system and open the Terminal shell of Ubuntu24. During both installation and upgrade processes, we need to use the command line. a simple

How to Install and Run the Ubuntu Notes App on Ubuntu 24.04 How to Install and Run the Ubuntu Notes App on Ubuntu 24.04 Mar 22, 2024 pm 04:40 PM

While studying in high school, some students take very clear and accurate notes, taking more notes than others in the same class. For some, note-taking is a hobby, while for others, it is a necessity when they easily forget small information about anything important. Microsoft's NTFS application is particularly useful for students who wish to save important notes beyond regular lectures. In this article, we will describe the installation of Ubuntu applications on Ubuntu24. Updating the Ubuntu System Before installing the Ubuntu installer, on Ubuntu24 we need to ensure that the newly configured system has been updated. We can use the most famous "a" in Ubuntu system

See all articles