The master-slave replication function provided by the MySQL database itself can easily realize the automatic backup of data in multiple places and realize the expansion of the database. Multiple data backups can not only enhance data security, but also further improve the load performance of the database by implementing read-write separation.
The following figure shows the working principle of MySQL master-slave replication
MySQL master-slave replication configuration steps
First, the main modification configuration file opens bin-log
[mysqld] log-bin = /var/lib/mysql/bin-log server-id = 3307
Second, set the server-id from the modified configuration file (not the same as the main id)
[mysqld] server-id = 3308
Third, the main master backup database
mysqldump -uroot -p123456 -h 192.168.0.12 -P 3307 -A --master-data=1 >backup.sql
-P specifies the port
-A backup all
Fourth, import from the slave backup database
mysql -uroot -p123456 -h 192.168.0.12 -P3307 < backup.sql
5. Log in to the main MySQL and set the synchronization user
grant replication slave on *.* to repl@'%' identified by 'repl'; flush privileges;
Sixth, check the main bin-log file and the POS location of the bin-log
show master status;
Seven, log in from the configuration synchronization
mysql> stop slave; mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.0.12', -> MASTER_USER='repl', -> MASTER_PASSWORD='repl', -> MASTER_PORT=3307 -> MASTER_LOG_FILE='bin-log.000001', -> MASTER_LOG_POS=890; mysql> start slave;
View sync status
show slave status\G
The communication process between the IO process master and the slave is YES on behalf of normal
The SQL process executes the process for the binary file synchronized from the main YES means normal
Create a test database test on the main
create database test;
It can be seen from above
In order to avoid data asynchrony, the slave server can be set to start read-only in production, and all write operations are performed on the master.