主从复制原理
- MySQL Replication是一个从Master复制到一台或多台Slave的异步复制过程。
- Master和Slave之间实现整个复制过程主要由三个线程来完成,其中一个IO线程在Master端,两个线程(SQL线程和IO线程)在Slave端。
- 通过Master服务器打开Binary Log(二进制日志文件)的功能,Slave端从Master端获取该日志信息,然后将二进制文件解析为SQL语句,并完全顺序地执行SQL语句所记录的各种操作。(Slave获取到的二进制文件同时也会写入到自身的Relay Log文件中)
Replication概念
- MySQL Replication技术是一个日志复制过程,在复制过程中一台服务器充当主,一台或多台其他服务器充当从服务器;
- 从服务器到主服务器拉取二进制日志文件,将日志文件解析成相应的SQL语句,然后在从服务器上重新执行一遍主服务器的操作,通过这种方式保证数据的一致性。
主从复制配置步骤:
- 设置server-id(服务器标识,在一组主从中不能重复)
- 开启二进制日志并指定二进制日志文件保存的路径
- 记录bin-log文件和bin-log(position)位置
- 若不停在Master时,加入全局锁,将需要同步的数据库备份到Slave节点上,解除全局锁
- 创建用于同步复制的用户
- 使用change master 在Slave和Master建立连接(Slave节点设置主服务器)
- 启动Slave
- 检查Slave的状态
Step1:配置master和slave的/etc/my.cnf文件| 12345678910111213 | [mysqld] basedir=/usr/local/mysqldatadir=/data/mysql/mysqlport=3306 socket=/var/lib/mysql/mysql.sock server-id=1 log-bin=/data/mysql/binlog/mysql-bin binlog-cache-size=10m sync-binlog=1 expire_logs_days=30 |
| 12345678910111213 | [mysqld] basedir=/usr/local/mysqldatadir=/data/mysql/mysqlport=3306 socket=/var/lib/mysql/mysql.sock server_id=2 relay-log=/data/mysql/binlog/mysql-relay-binreplicate-wild-do-table=testdb1.% replicate-wild-do-table=testdb2.% |
Step2:手动同步数据库到slave1、锁定Master的表的写操作(不要退出终端)| 12 | mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) |
2、Master上备份数据库并传到Slave上| 12345 | [root@node1 ~][root@node1 ~] [root@node1 ~][root@node1 ~] |
3、Slave上创建同步的数据库并导入数据文件| 123456789 | [root@node2 ~][root@node2 ~]mysql> show tables; +-------------------+ | Tables_in_testdb1 | +-------------------+ | tt1 | | tt2 | +-------------------+ |
Step3:Master创建同步的用户 1、Master解除锁定2、创建同步用户| 12 | mysql> grant replication slave on *.* to "repl_user"@"192.168.1.211" identified by "repl_passwd"; mysql> flush privileges; |
Step4:Slave连接Master,将node1设置为自己的主服务器1、查看Master的master-log-file文件和position位置| 123456 | mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 120 | | | | +------------------+----------+--------------+------------------+-------------------+ |
2、Slave上连接Master,并启动slave| 12345678910 | mysql> change master to master_host="192.168.1.210", master_user="repl_user", master_password="repl_passwd", master_port=3306, master_log_file="mysql-bin.000001", master_log_pos=120; mysql> start slave; |
3、Slave上查看slave状态(Slave_IO_Running、Slave_SQL_Running和Seconds_Behind_Master)| 1234567891011121314151617181920212223242526272829303132333435 | mysql> show slave statusG; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.210 Master_User: repl_user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 120 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: testdb1.%,testdb2.% Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 471 Relay_Log_Space: 807 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 |
Step5:测试主从同步功能1、Master上创建数据库和表| 123 | mysql> insert into tt1(id,name) values(1,"hoai"),(2,"dime"); mysql> create database testdb2; |
2、Slave上查看是否同步| 12345678910111213141516171819202122 | mysql> select * from tt1; +------+------+ | id | name | +------+------+ | 1 | hoai | | 2 | dime | +------+------+ 2 rows in set (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | testdb1 | | testdb2 | +--------------------+ 6 rows in set (0.00 sec) |
清除二进制日志方法(重置主从):mysql> reset master;mysql> reset slave;(针对从上的relay-log文件)
本文永久更新链接地址