Article Directory
Configure MySQL master-slave replication
The master copy is MySQL from asynchronous operation.
MySQL replication types are divided into two kinds:
- Log-based replication Binary Log
- Use GTID complete the transaction-based replication
Three kinds Binary Log log format:
- Statement: store SQL statements, the minimum amount of log storage
- Row: storing event data, store the log capacity, can not be directly read
- Mixed: between the Row and Statement, the operation for recording the Row uncertain
Log-based replication
Test environment
both equipped with a virtual machine CentOS7 where MySQL version 5.7.25
Master (primary) IP is 192.168.33.101
Slave (from) IP is 192.168.33.102
Configuration Master (primary)
used vim /etc/my.cnf
to edit my.cnf, add the following configuration:
[mysqld]
server-id=1
log-bin=master-bin
After configuration is complete, you need systemctl restart mysqld.service
to restart the service
by using the command show master status
View Master (main) Status:
Creating a data synchronization users in the Master database, grant user permissions slave REPLICATION SLAVE, used data from the primary synchronization between the library.
CREATE USER 'slave'@'192.168.33.102' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.33.102';
FLUSH PRIVILEGES;
Configuring the Slave (from)
the same as adding configure Master my.cnf file as follows:
[mysqld]
server-id=2
relay-log=slave-relay-bin
After configuration is complete, you need systemctl restart mysqld.service
to restart the mysql service configuration to take effect.
Connecting Master (primary) and the Slave (from)
Connected to the Slave (from) the database Master (Main):
change master to master_host='192.168.33.101', master_user='slave', master_password='123456', master_port=3306, master_log_file='master-bin.000001', master_log_pos=775;
parameter:
- MASTER_HOST : Master Address
- MASTER_PORT : Master port number
- MASTER_USER : a user of the master-slave synchronization
- master_password : a synchronization master password from the user
- MASTER_LOG_FILE : specified log file
- master_log_pos start reading location for the log file:
Master-slave synchronization start start slave;
viewing Slave (from) state:
Slave_IO_Running and Slave_SQL_Running are Yes, explain the success of the master-slave replication configuration.
Test master-slave replication
Creating a database db_test the Master:
At this point, see Slave database, found that has been automatically sync db_test: