MySQL传统复制技术

原理

复制解决的问题
数据复制技术有以下一些特点:
(1)    数据分布
(2)    负载平衡(load balancing)
(3)    备份
(4)    高可用性(high availability)和容错
复制如何工作
从高层来看,复制分成三步:
(1)    master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);
(2)    slave将master的binary log events拷贝到它的中继日志(relay log);
(3)    slave重做中继日志中的事件,将改变反映它自己的数据。

 

1.master记录二进制日志。在每个事务更新数据完成之前,master在二日志记录这些改变。MySQL将事务串行的写入二进制日志,即使事务中的语句都是交叉执行的。在事件写入二进制日志完成后,master通知存储引擎提交事务。
2.slave将master的binary log拷贝到它自己的中继日志。首先,slave开始一个工作线程——I/O线程。I/O线程在master上打开一个普通的连接,然后开始binlog dump process。Binlog dump process从master的二进制日志中读取事件,如果已经跟上master,它会睡眠并等待master产生新的事件。I/O线程将这些事件写入中继日志。
SQL slave thread处理该过程的最后一步。SQL线程从中继日志读取事件,更新slave的数据,使其与master中的数据一致。只要该线程与I/O线程保持一致,中继日志通常会位于OS的缓存中,所以中继日志的开销很小
在master中也有一个工作线程:和其它MySQL的连接一样,slave在master中打开一个连接也会使得master开始一个线程。复制过程有一个很重要的限制——复制在slave上是串行化的,也就是说master上的并行更新操作不能在slave上并行操作。

 复制类型

基于语句的复制  :在主服务器上执行的SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高。 一旦发现没法精确复制时,   会自动选着基于行的复制。    
基于行的复制    :把改变的内容复制过去,而不是把命令在从服务器上执行一遍. 从mysql5.0开始支持
混合类型的复制  :默认采用基于语句的复制,一旦发现基于语句的无法精确的复制时,就会采用基于行的复制。

实现思路

1.系统环境准备,软件版本选择
2.规划数据库目录(暂且默认)
3.安装(YUM)
4.启动初始化
5.配置文件调整
6.授权复制用户
7.同步更新日志
8.结果验证及状态查看

部署环境

操作系统:   Cents6.9
mysql版本:mysql5.6 运行端口:3306
MySQL安装方式:yum(生产要二进制安装)
数据目录:默认(生产要改)
master:192.168.18.3 slave:192.168.18.4
需要同步数据库:test2017
同步用户:tsync 授予权限:sync

部署

yum部署 详见官网:

https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/#repo-qg-yum-fresh-install

Master操作
[root@study soft]# /etc/init.d/mysqld start
[root@study soft]# mysqladmin -uroot password "nihaome"
[root@study soft]# mysql -pnihaome
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
mysql> create database test2017;
mysql> grant replication slave,replication client on *.* to tsync@'192.168.18.4' identified by "slave@1223";
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Master操作
[root@study ~]# vim /etc/my.cnf
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
server-id=1
log-bin=mysql-bin
binlog-do-db=test2017
expire_logs_days = 30
binlog-ignore-db=mysql
sync_binlog = 1
binlog_format = row
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
default-character-set=utf8
重启
Slave操作
[root@wxl ~]# vi /etc/my.cnf
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
server-id=2
log-bin=mysql-bin
replicate-do-db=test2017
replicate-ignore-db=mysql
slave-skip-errors = all
expire_logs_days = 30
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
default-character-set=utf8
重启
Slave 操作
mysql>  change  master to master_host='192.168.18.3',master_user='tsync',master_password='slave@1223',master_log_file='mysql-bin.000007',master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.05 sec)

mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.18.3
Master_User: tsync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 295
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 458
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test2017
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 295
Relay_Log_Space: 632
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
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: e79886da-998a-11e7-816b-000c291cd934
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)

ERROR:
No query specified

验证

Master操作
mysql> use  test2017
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> create table if not exists haha (id int(10) PRIMARY KEY AUTO_INCREMENT,name varchar(50) NOT NULL);
Query OK, 0 rows affected (0.03 sec)

mysql> show tables;                                
+--------------------+
| Tables_in_test2017 |
+--------------------+
| haha               |
+--------------------+

mysql> insert into test2017.haha values(1,"wxl"),(2,,"wud");
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> use test2017
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from haha;
+----+------+
| id | name |
+----+------+
| 1 | wxl |
| 2 | wud |
+----+------+
2 rows in set (0.00 sec)

Slave 操作
mysql> select * from haha;
+----+------+
| id | name |
+----+------+
|  1 | wxl  |
|  2 | wud  |
+----+------+
2 rows in set (0.00 sec)
 

常见故障

 故障现象:Last_IO_Error: error connecting to master 'tsync@192.168.18.3:3306' - retry-time: 60  retries: 5 //同步用户异常
解决方法:
检查用户名或密码以及权限是否正确,如果不确定请 重新授权复制用户;

主从数据不一致故障解决:

第一种:通过sql_slave_skip_counter跳过同步错误,适用于一般异常如插入时主键冲突
第二种:重新做主从,然后使用change master指定同步位置,这种耗时长
第三种:使用第三方工具如pt-table-sync

posted @ 2017-09-22 12:03  91King  阅读(744)  评论(0)    收藏  举报