Detailed operation steps for MySQL to implement master-slave replication (Windows)

1. Teaching video address

Video address: video link

2. Design ideas

Prepare two versions of MySQL 5.7 , one as the master database and the other as the slave database.
Use the master database as the write database and the slave database as the read database.
insert image description here

3. Specific steps

1. Make a copy of your mysql directory first , and name the copied one mysql-slave.
insert image description here
2. First modify the my.ini file of the main database, add the following code, mainly to configure the database to be synchronized and shield the database to be synchronized:

log-bin=mysql-bin
server-id	= 1
#设置需要同步的数据库
binlog-do-db=db_user
#屏蔽系统库同步
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema

insert image description here
3. Then register the master database as a system service.

mysqld install mysql-master --defaults-file="D:\xampp\mysql\bin\my.ini"

4. Then modify the my.ini file of the slave database, modify the startup port and the path of the slave database: insert image description here
add the database table configuration to be synchronized at the same time, remember to modify the server-id, and change it to be inconsistent with the master database:
insert image description here

server-id=2
#设置需要同步的数据库
replicate_wild_do_table=db_user.%
#屏蔽系统库同步
replicate_wild_ignore_table=mysql.%
replicate_wild_ignore_table=information_schema.%
replicate_wild_ignore_table=performance_schema.%

5. After the modification, also register the slave database as a system service.

mysqld install mysql-slave --defaults-file="D:\xampp\mysql-slave\bin\my.ini"

Enter the data directory of the master and slave database directories, modify the auto.cnf file, and change the two uuids to be different. Here, I modified the last digit.
insert image description here
Then restart the two services of the master and slave databases.
insert image description here
6. Grant master-slave replication permissions to the master library:

GRANT REPLICATION SLAVE ON *.* TO 'root'@'%' IDENTIFIED BY 'root';

Then refresh permissions

FLUSH PRIVILEGES;

insert image description here
Then check the confirmation position of the main library. Remember the File and Position in the figure below, which will be used later when configuring the slave library:

show master status;

insert image description here
7. Open the command window of the slave library, and enter the following commands in sequence:
stop synchronization first:

STOP SLAVE;

Then configure the slave library to point to the main library, using the file name File and position recorded in the previous step:

CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=1178,MASTER_HOST='localhost',MASTER_USER='root',MASTER_PASSWORD='',MASTER_PORT=
3306;

Then enable sync:

START SLAVE;

insert image description here

Then check the synchronization status of the slave library:

show slave status;

Both Slave_IO_Runing and Slave_SQL_Runing are Yes, indicating that the synchronization is successful. If it is not Yes, please check the error_log.
insert image description here
PS: If the slave library has been pointed to by the main library before, you need to execute the following command to clear it first:

STOP SLAVE IO_THREAD FOR CHANNEL '';
reset slave all;

8. This completes the configuration of the master-slave replication of the MySQL database. When you modify the data in the table in the db_user database in the master database, the data in the corresponding table in the db_user database in the slave database will also change accordingly.

Guess you like

Origin blog.csdn.net/dgfdhgghd/article/details/128427294