MySQL master-slave presentation
(Two machines Data Synchronization)
Main: -> binlog
From: -> relaylog
There is a thread on the main log dump, from and to the I / O threads pass binlog
From there are two threads, wherein the I / O of the main thread is used to synchronize and generate relaylog binlog, another SQL thread for the inside of the sql statement relaylog floor (execution)
From the main application scenarios:
(1) for data backup, (primary: for reading and writing data, from: real-time synchronization, when the master is down, even if the service may be)
(2) is to do a backup. (Client reads data from this machine (but can not write from), reduce the pressure of the Lord)
Ready to work
1. Both machines are fitted with mysql, mysql service and are open
Placement main
Owner: 192.1683.136.133 From: 192.168.136.134
1. Modify my.cnf, increase server-id = 133 and log_bin = canshenglinux1
2. After modifying the configuration file, start or restart the mysqld service
After the restart, we can find became to define log_bin = canshenglinux1 for the beginning of the file in the configuration file in / data / mysql swells
These documents are important, or can not achieve master-slave
3.
The mysql database backup and restore to cansheng library, as the test data (the library is used to call the shots from)
mysqldump -uroot -p123456 mysql > /tmp/mysql.sql
mysql -uroot -p123456 -e “create database cansheng”
mysql -uroot -p123456 cansheng < /tmp/mysql.sql
4. Create a user as synchronization data
(1) first into mysql
(2) re-create a user to synchronize data
grant replication slave on *.* to 'repl'@slave_ip identified by '123456';
5. Lock at the table:
flush tables with read lock;
(为了同步前数据一致。)
show master status (查看一下position和file)
(退出mysql)
此时查看一下/data/mysql又是什么库
(一般同步不要同步mysql库,因为里面有很多用户名和密码)
6.备份一下所需要同步的库:
mysqldump -uroot -p123456 zrlog > /tmp/zrlog.sql
配置从
1.查看my.cnf,配置server-id=134,要求和主不一样
2.修改完配置文件后,启动或者重启mysqld服务
/etc/init.d/mysqld restart
3.把主上需要同步的库复制过来:
scp 192.168.136.133:/tmp/*.sql /tmp/
临时创建alias,方便使用命令:
alias 'mysql=/usr/local/mysql/bin/mysql'
alias 'mysqldump=/usr/local/mysql/bin/mysqldump'
4.进入mysql
创建相对应的库:
create database cansheng;
create database zrlog;
create database mysql2;
5.数据恢复:
mysql -uroot cansheng < /tmp/mysql.sql
6.进入mysql
(1)stop slave;
(2)change master to master_host='192.168.136.133', master_user='repl', master_password='123456', master_log_fil_log_file='canshenglinux1.000002', master_log_pos=646207;
(3)start slave;
(4) from the master determines whether the configuration
show slave status\G
This is unsuccessful successfully configured to appear only two yes success.
Unsuccessful factors:
(1) firewall systemctl stop firewalld
(2) selinux -> Close sentenforce 0
(3) the user name is correct
{And I factor is the designated user name and password error when the configuration of the Lord, to start from scratch once}
7. Return to the main, proceeds to release the lock mysql Table (table)
unlock tables;
This completes the master-slave configuration:
Master-slave synchronization test
(Only in the master or from one of the configuration on it. These parameters are not configured by default, there is no manual configuration, it defaults to synchronize all libraries)
From the configuration to make use of in the last two can be avoided, ignoring a library or table, when the cascade query to avoid data loss, it should not be overlooked also ignored! !
From the main test is successful, whether synchronous!
1. Both machines into mysql under test into the database: cansheng2
Empty comment at this time on the main table; comment table to see whether data from empty, thereby synchronizing.
truncate table comment;
Drop table test:
We can not follow mysql operations from this machine, or when we will disconnect from the main.
(At this time because our position has changed)
If at this time disconnected from our Lord, should be re-run
(1)stop slave;
(2)change master to master_host='192.168.136.133', master_user='repl', master_password='123456', master_log_fil_log_file='canshenglinux1.000002', master_log_pos=646207;
(3)start slave;
(4) from the master determines whether the configuration
show slave status\G
This is unsuccessful successfully configured to appear only two yes success.