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';

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

There are four main index types in MySQL: B-Tree index, hash index, full-text index and spatial index. 1.B-Tree index is suitable for range query, sorting and grouping, and is suitable for creation on the name column of the employees table. 2. Hash index is suitable for equivalent queries and is suitable for creation on the id column of the hash_table table of the MEMORY storage engine. 3. Full text index is used for text search, suitable for creation on the content column of the articles table. 4. Spatial index is used for geospatial query, suitable for creation on geom columns of locations table.

TocreateanindexinMySQL,usetheCREATEINDEXstatement.1)Forasinglecolumn,use"CREATEINDEXidx_lastnameONemployees(lastname);"2)Foracompositeindex,use"CREATEINDEXidx_nameONemployees(lastname,firstname);"3)Forauniqueindex,use"CREATEU

The main difference between MySQL and SQLite is the design concept and usage scenarios: 1. MySQL is suitable for large applications and enterprise-level solutions, supporting high performance and high concurrency; 2. SQLite is suitable for mobile applications and desktop software, lightweight and easy to embed.

Indexes in MySQL are an ordered structure of one or more columns in a database table, used to speed up data retrieval. 1) Indexes improve query speed by reducing the amount of scanned data. 2) B-Tree index uses a balanced tree structure, which is suitable for range query and sorting. 3) Use CREATEINDEX statements to create indexes, such as CREATEINDEXidx_customer_idONorders(customer_id). 4) Composite indexes can optimize multi-column queries, such as CREATEINDEXidx_customer_orderONorders(customer_id,order_date). 5) Use EXPLAIN to analyze query plans and avoid

Using transactions in MySQL ensures data consistency. 1) Start the transaction through STARTTRANSACTION, and then execute SQL operations and submit it with COMMIT or ROLLBACK. 2) Use SAVEPOINT to set a save point to allow partial rollback. 3) Performance optimization suggestions include shortening transaction time, avoiding large-scale queries and using isolation levels reasonably.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
