How to implement MySQL master-slave replication on a Windows host?
MySQL’s master-slave replication is implemented through binlog logs. The “master” in master-slave replication refers to the database on the MySQL master server, and the “slave” refers to the MySQL slave server. database, and this kind of replication is based on the database level. For this reason, the database name in the slave server must be consistent with the database name in the master server. Then, to achieve master-slave replication, we must have at least two MySQL servers ( It is best if the two MySQL servers are located on different hosts, or two MySQL servers are installed on one host with different ports).
Generally speaking, the main database and slave database of MySQL database are distributed on different hosts. If we only have one host now and it is a windows system, how to implement master-slave replication of MySQL? The method is as follows:
Here we only introduce the operation method of one master and one slave .
My computer has installed the xampp integrated environment (similar to the wamp installation package), and the MySQL service in it can be used as the main server of MySQL. Then, we also need to install another MySQL on this computer as the slave server of the database.
The MySQL version installed in xampp of my computer is 5.6.20, and the port is 3306.
We need to install another MySQL (it is best to install the same version or a similar version to avoid problems), and change the port to 3307
Database server parameters:
Master server (master): IP is 127.0.0.1, port is 3306
Slave: IP is 127.0.0.1, port is 3307
Master server Configuration:
Modify the database configuration file of the main server (E:\xampp\mysql\bin\my.ini), at the bottom of the [mysqld] label , add the following code:
#The database that needs to be backed up binlog-do-db=test#The database that does not need to be backed up binlog-ignore-db=mysql #Enable binary loglog-bin=mysql-bin#Server idserver-id=1 Save and exit, restart the MySQL main server. binlog-do-db is used to specify the database that needs to be synchronized,binlog-ignore-db specifies the database that does not need to be synchronized. If neither parameter is set, the slave server will copy the master server all databases.
Generally, the root account is not used for synchronization account. For this reason, we need to create a new user on the main server (such as user01, password is 123456).
Here we use the command line to create it. The method is as follows:
Open cmd and switch to E:\xampp\mysql \bin, use the root account to connect to the MySQL main server:
mysql -uroot -p -P3306
Create a new user:
create user 'user01'@'127.0.0.1' identified by '123456';
( The IP address after @ is the IP address of the client that is allowed to connect.)
Then, configure the master-slave replication permissions for the new user:
grant replication slave on *.* to 'user01'@'127.0.0.1' identified by '123456';
##(The IP address after @ is allowed to connect The client's IP address, if changed to '%', it means that the client has no IP address restrictions)
If there is already data in the main server's database (test), we need First manually copy the data from the master server to the slave server. The method is as follows:
In this case, we only back up one database (test). There is a table basic_user in test and there is already data in the table. In order to prevent the data in the database test from being updated when we copy the data, we need to lock the database first. The command is as follows:
flush tables with read lock;
This command is a global read lock. It will add read locks to all databases in the main server. By the way, let’s talk about the difference between read locks and write locks. :
read lock: also called shared lock, allows all reads operation, but blocks write operations, that is, all connections can only read data, but are not allowed to write data.
write lock (write lock): also called exclusive lock, exclusive lock, only allows reading and writing of the current connection, and does not allow other concurrent reading operations and write operations.
After locking the database of the master server, we also create a database test in the slave server and add all Tables (including table structure and table data) are imported.
Then, we execute the following command to unlock:
##unlock tables;
View the master status of the main server: ##mysql> show master status;
------------------ ---------- --------------- ------- ----------- ------------------- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |------------------ ---------- --------------- ----- ------------- ------------------ | mysql-bin.000008 | 498 | test | mysql | | ------------------ ------------------ Slave server configuration:
Modify the database configuration file of the slave server (E:\mysql\
my.ini) , at the bottom of the[mysqld] tag, add the following code: #Port
port = 3307
#serveridserver_id = 2
#Enable binary log (the slave server does not have to enable binary log)
log-bin=mysql-bin
Save and exit, restart the MySQL service.
Connect to the MySQL slave server:
mysql -uroot -p -P3307
##Configure the parameters for replication:
change master to master_host='127.0.0.1',master_user='user01',master_password='123456',master_port=3306,master_log_file= 'mysql-bin.000008',master_log_pos=498;
Parameter details:master_host: IP of the main server
master_user: The newly created user name on the master server
master_password: The user’s password
master_port: The port of the master server, if it has not been modified , the default is enough.
master_log_file: The name of the main server binary log file, fill in the value of File displayed when viewing the master status of the main server
master_log_pos: The location of the log, fill in the Position value displayed when viewing the master status of the master server
Start Slave replication function from the server: start slave;View the slave status of the slave server: mysql> show slave status \G*** ********************** 1. row **************************** ***** Slave_io_State: WAITING for Master to Send Event Master_host: 127.0.0.1## Master_user: User01
## Master_Port: 3306 ThisConnect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 120
Relay_Log_F ile: hp-PC-relay-bin.000004
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
If the values of Slave_IO_Running and Slave_SQL_Running are both Yes, it means that all configurations of master-slave replication have been successful, that is, the slave server can automatically synchronize with the database data of the master server.
After that, as long as the data on the main server is updated (for example: a new table is created in the test database or the data in the table changes), the slave server will The server will automatically remain consistent with the main server. But if someone deliberately changes the data on the slave server, the data on the master server will not be updated synchronously unless we set the two MySQL servers to be mutual master-slave.
The above is what I have compiled about the master-slave architecture of configuring mysql in the window environment. Interested friends can try it.
Related articles:
Installing mysql-5.7.21 under windows
Summary of basic knowledge of MySQL
navicat for mysql download, installation and simple use
The above is the detailed content of How to implement MySQL master-slave replication on a Windows host?. For more information, please follow other related articles on the PHP Chinese website!

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



Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Multithreading in the language can greatly improve program efficiency. There are four main ways to implement multithreading in C language: Create independent processes: Create multiple independently running processes, each process has its own memory space. Pseudo-multithreading: Create multiple execution streams in a process that share the same memory space and execute alternately. Multi-threaded library: Use multi-threaded libraries such as pthreads to create and manage threads, providing rich thread operation functions. Coroutine: A lightweight multi-threaded implementation that divides tasks into small subtasks and executes them in turn.

Python binary library (.whl) download method explores the difficulties many Python developers encounter when installing certain libraries on Windows systems. A common solution...

Solving the problem of slow Photoshop startup requires a multi-pronged approach, including: upgrading hardware (memory, solid-state drive, CPU); uninstalling outdated or incompatible plug-ins; cleaning up system garbage and excessive background programs regularly; closing irrelevant programs with caution; avoiding opening a large number of files during startup.

Efficient reading of Windows system logs: Reversely traverse Evtx files When using Python to process Windows system log files (.evtx), direct reading will be from the earliest...

Multithreading is an important technology in computer programming and is used to improve program execution efficiency. In the C language, there are many ways to implement multithreading, including thread libraries, POSIX threads, and Windows API.

How to implement Windows-like in front-end development...

Why can't my code get the data returned by the API? In programming, we often encounter the problem of returning null values when API calls, which is not only confusing...
