Home  >  Article  >  Database  >  Detailed explanation of the example code of MySQL5.7 master-slave configuration

Detailed explanation of the example code of MySQL5.7 master-slave configuration

黄舟
黄舟Original
2017-03-17 14:09:061233browse

This article mainly provides a detailed analysis of MySQL5.7 master-slave configuration examples, which has certain reference value. Interested friends can refer to

MySQL5.7 Master-slave configuration implementation method, the specific content is as follows

Installation environment:

Master: 10.211.55.11, Redhat6.5, MySQL5.7.12
Slave : 10.211.55.12, Redhat6.5, MySQL5.7.12

Master’s my.cnf configuration:

Slave’s my.cnf configuration:

You can specify which databases need to be copied and which ones do not need to be copied. For example, if you comment out the contents in my my.cnf configuration, I will copy them all by default. . After modifying the my.cnf configuration file, both machines restart the MySQL service: service mysqld restart. The next operation is more important:
Mysql configuration of the Master machine

1. mysql -u root -p login;
2. Execute the following command, which means that the root copy permission and File permission of 10.211.55.12 will be granted. Of course, you can also create another user to operate, not necessarily root. , 123456 represents the password.

GRANT FILE ON *.* TO 'root'@'10.211.55.12' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'root'@'10.211.55.12' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;

3. Execute the show master status command, as shown in the figure:

Detailed explanation of the example code of MySQL5.7 master-slave configuration

##MySQL configuration of the Slave machine

1. mysql -u root -p login;

2. Execute the following instructions in sequence:

mysql> stop slave;
mysql> change master to master_host='10.211.55.11',master_user='root',master_password='123456',master_log_file='mysql-bin.000003', master_log_pos=154;
mysql> start slave;

Note:
The values ​​in master_log_file and master_log_pos should correspond to the values ​​in the show master status result of the Master machine (as shown above).

3. Check the slave

status, show slave status\G; Note that the "\G" here is to display the formatting result. If there are no errors, the slave status The results are as shown in the figure:

## Test:


Create a database in Master:

mysql> create database test;
mysql> create table t1 (id int,name varchar(200),createtime timestamp,key(id));
mysql> insert into t1 values (1,'aa',now());
mysql> insert into t1 values (3,'bb',now());

After performing the above operations, switch to the Salve machine to see if it is synchronized. If nothing else, it should be synchronized immediately.

My machine displays as follows:



The above is the detailed content of Detailed explanation of the example code of MySQL5.7 master-slave configuration. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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