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!

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi


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

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

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development 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.
