Home Database Mysql Tutorial windows环境下mysql数据库的主从同步备份步骤(单向同步)_MySQL

windows环境下mysql数据库的主从同步备份步骤(单向同步)_MySQL

Jun 01, 2016 pm 01:20 PM
mysql windows database article website

bitsCN.com

windows下mysql双向同步备份实现方法

以下的文章主要讲述的是在windows环境下实现MySQL数据库的主从同步备份的正确操作方案,我在一些相关的网站看见关于windows环境下实现MySQL数据库的主从同步备份的操作步骤描述,但是很少有对其成功操作到底的,所以拿出此篇较为完整的方案与大家一起分享。
以下配置在本机上已经成功:

实现功能:A为主服务器,B为从服务器,初始状态时,A和B中的数据信息相同,当A中的数据发生变化时,B也跟着发生相应的变化,使得A和B的数据信息同步,达到备份的目的。

环境:

A、B的MySQL数据库版本同为4.1.20

A:

操作系统:Windows 2003 server

IP地址:192.168.100.1

B:

操作系统:Windows 2003 server

的IP地址:192.168.100.2

配置过程:
1、在A的MySQL数据库中建立一个备份帐户,命令如下:

GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.*
TO backup@'192.168.100.2'
IDENTIFIED BY '1234';

建立一个帐户backup,并且只能允许从192.168.100.2这个地址上来登陆,密码是1234。
2、因为mysql版本新密码算法不同,所以进入mysql下,输入:set password for 'backup'@'192.168.100.2'=old_password('1234');
3、关停A服务器,将A中的数据拷贝到B服务器中,使得A和B中的数据同步,并且确保在全部设置操作结束前,禁止在A和B服务器中进行写操作,使得两数据库中的数据一定要相同!
4、对A服务器的配置进行修改,打开mysql/my.ini文件,在[mysqld]下面添加如下内容:
server-id=10
log-bin=c:/log-bin.log
server-id:为主服务器A的ID值
log-bin:二进制变更日值
5、重启A服务器,从现在起,它将把客户堆有关数据库的修改记载到二进制变更日志里去。
6、关停B服务器,对B服务器锦熙配置,以便让它知道自己的镜像ID、到哪里去找主服务器以及如何去连接服务器。最简单的情况是主、从服务器分别运行在不同的主机上并都使用着默认的TCP/IP端口,只要在从服务器启动时去读取的mysql/my.ini文件里添加以下几行指令就行了。

[mysqld]
server-id=11
master-host=192.168.100.1
master-user=backup
master-password=1234
//以下内容为可选
replicate-do-db=backup
server-id:从服务器B的ID值。注意不能和主服务器的ID值相同。
master-host:主服务器的IP地址。
master-user:从服务器连接主服务器的帐号。
master-password:从服务器连接主服务器的帐号密码。
replicate-do-db:告诉主服务器只对指定的数据库进行同步镜像。


7、重启从服务器B。至此所有设置全部完成。更新A中的数据,B中也会立刻进行同步更新。如果从服务器没有进行同步更新,你可以通过查看从服务器中的mysql_error.log日志文件进行排错。
8、由于设置了slave的配置信息,mysql在数据库data目录下生成master.info,所以如有要修改相关slave的配置要先删除该文件,否则修改的配置不能生效。

binlog-do-db

官方文档推荐的是,在master端不指定binlog-do-db,在slave端用replication-do-db来过滤。

告诉主服务器,如果当前的数据库(即USE选定的数据库)是db_name,应将更新记录到二进制日志中。其它所有没有明显指定的数据库 被忽略。如果使用该选项,你应确保只对当前的数据库进行 ...

例如你设置了

binlog-do-db=db1
然后你执行了以下语句
use db1;
create database db2;
第二句语句并不会记入log
即使当前数据库是db1,因为考虑的是语句中的数据库名;

相反,除了CREATE DATABASE, ALTER DATABASE, and DROP DATABASE 以外的语句是对当前数据库有关的。
例如你设置了

binlog-do-db=db1
然后你执行了以下语句
use db2;
insert into db1.table1 values(1);
这条语句是不会计入log的,因为当前db是db2。而与操作db无关。


先在服务器端查询

mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| updatelog.000012 | 15016 | data | mysql |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
由此可见两者的File、Position存在问题,所要要去Slave上设置对应主库的Master_Log_File、Read_Master_Log_Pos;执行如下语句;
mysql>slave stop;
mysql>CHANGE MASTER TO MASTER_HOST='192.168.0.1',MASTER_USER='test', MASTER_PASSWORD='******',MASTER_LOG_FILE='updatelog.000012',MASTER_LOG_POS=15016;
确保 Slave_IO_Running: Yes 、Slave_SQL_Running: Yes都要为YES才能证明Slave的I/O和SQL进行正常。

CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=6267;

CHANGE MASTER TO
MASTER_HOST='192.168.1.100',
MASTER_USER='jb51',
MASTER_PASSWORD='www.bitsCN.com',
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=76146;

然后 在slave端查看 如果正常就oK了,都是yes


Slave_IO_Running: Yes
Slave_SQL_Running: Yes


[ERROR] The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).
查看 server_id; 一般情况下不建议大家用 1或2,建议用10,11防止出现
show variables like 'server_id';

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.

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.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

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.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

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.

See all articles