5. Master-slave replication
Rationale for Replication
slave will read binlog from master for data synchronization
Three steps + schematic diagram
The MySQL replication process is divided into three steps
- The master records changes to the binary log (binary log). These recording processes are called binary log events, binary log events;
- The slave copies the master's binary log events to its relay log;
- The slave redoes the events in the relay log, applying the changes to its own database. MySQL replication is asynchronous and serialized
Basic Principles of Replication
- Each slave has only one master
- Each slave can only have one unique server ID
- Each master can have multiple slaves
The biggest problem with replication is latency .
Common configuration of one master and one slave
-
The version of mysql is the same and it runs as a service in the background
-
Both the master and the slave are configured under the [mysqld] node, both in lowercase
The host modifies the my.ini configuration file
-
[Required] The unique ID of the main server
- server-id=1
-
[must] enable binary logging
- log-bin=your own local path/mysqlbin
- log-bin=D:/devSoft/MySQLServer5.5/data/mysqlbin
-
[Optional] Enable error logging
- log-err=your own local path/mysqlerr
- log-err=D:/devSoft/MySQLServer5.5/data/mysqlerr
-
[optional] root directory
- basedir="your own local path"
- basedir=“D:/devSoft/MySQLServer5.5/”
-
[optional] temporary directory
- tmpdir="your own local path"
- tmpdir=“D:/devSoft/MySQLServer5.5/”
-
[optional] data directory
- datadir="your own local path /Data/"
- datadir=“D:/devSoft/MySQLServer5.5/Data/”
-
Host, can read and write
- read-only=O
-
[Optional] Set databases not to be replicated
- binlog-ignore-db=mysql
-
[Optional] Set the database to be replicated
- binlog-do-db=The name of the primary database that needs to be replicated
Modify the my.cnf configuration file from the machine
- [Required] Unique ID of the slave server
- [Optional] Enable binary logging
Configuration file, please restart the background mysql service on both the host and slave
- service mysql stop
- service mysql start
Both the host and the slave close the firewall
-
windows closed manually
-
Turn off the virtual machine linux firewall
View firewall status
firewall-cmd --state
stop firewall
systemctl stop firewalld.service
Prohibit firewall from booting
systemctl disable firewalld.service
Create an account on the Windows host and authorize the slave
- GRANT REPLICATION SLAVE ON *.* TO 'zhangsan'@'slave machine database IP' IDENTIFIED BY '123456';
- flush privileges;//refresh
- Query the status of the master
- show master status;
- Record the value of File and Position
- Do not operate the main server MYSQL after performing this step to prevent the status value of the main server from changing
Configure the host to be copied on the Linux slave
-
CHANGE MASTER TO MASTER_HOST='host IP', MASTER_USER='zhangsan', MASTER_PASSWORD='123456', MASTER_LOG_FILE='File name', MASTER_LOG_POS=Position number;
-
Start the replication function from the server
- start slave;
-
show slave status\G
- The following two parameters are both Yes, indicating that the master-slave configuration is successful!
- Slave_IO_Running:Yes
- Slave_SQL_Running:Yes
The host creates a new database, creates a new table, inserts records, and replicates from the slave
How to stop copy function from service
- stop slave;