检查mysql的replication_MySQL replication学习笔记

replication主从,主主都是以前做的,但忘的很快,今天复习一下,顺便做点笔记了,visio也有段时间没有用了,大致画了了一个原理图:

1,主服务器记录二进制日志。

2,从服务器把主服务器的二进制日志拷贝到自已的中继日志中。(详细过程:首先它先启动一个名称为I/O线程的工作线程,这个I/O线程开启一个普通的客户端连接,然后启动一个特殊的二进制日志转储进程,这个转储进程从主服务器的二进制日志中中读取事件。它不会对事件进行轮询。如果它跟上了主服务器,就会进入休眠状态,并等待有新事件发生时主服务器发出的信号。I/O线程把事件写入从服务器的中继日志中。)

3,从服务器重放中继日志中的事件,把更改应用到自已的数据上。(SQL线程读取了中继日志,并且重放其中的事件,然后更新从服务器的数据,由于这个线程能跟上I/O线程一,中继日志通常位于操作系统缓存中,所以中继日志的开销很低。)

主服务器中的复制线程:

mysql> show processlist\G;

*************************** 1. row ***************************

Id: 2

User: root

Host: localhost

db: NULL

Command: Query

Time: 0

State: NULL

Info: show processlist

*************************** 2. row ***************************

Id: 3

User: chlotte

Host: 192.168.0.178:36643

db: NULL

Command: Binlog Dump

Time: 57

State: Has sent all binlog to slave; waiting for binlog to be updated

Info: NULL

2 rows in set (0.00 sec)

ERROR:

No query specified

mysql>

从服务器上的I/O线程与SQL线程:

mysql> show processlist\G;

*************************** 1. row ***************************

Id: 3

User: root

Host: localhost

db: NULL

Command: Query

Time: 0

State: NULL

Info: show processlist

*************************** 2. row ***************************

Id: 4

User: system user

Host:

db: NULL

Command: Connect

Time: 71

State: Waiting for master to send event

Info: NULL

*************************** 3. row ***************************

Id: 5

User: system user

Host:

db: NULL

Command: Connect

Time: 48

State: Has read all relay log; waiting for the slave I/O thread to update it

Info: NULL

3 rows in set (0.00 sec)

ERROR:

No query specified

与复制相关的参数列表:

Sync_binlog=1

mysql在每次提交事务的时候把二进制日志的内容同步到磁盘上,所以即使服务器崩溃也会把事件写入日志中。

Skip_slave_start

会阻止从服务器在崩溃后自动启动,它可以给你机会修复服务器。

Mysql-bin.index

记录了磁盘上的二进制日志文件,这里的index并不是指表的索引,而是指这个文件中每一行包含了二进制日志文件的文件名。

Mysql-relay-bin.index

中继日志的索引文件,其作用和二进制日志文件的索引文件相同。

Master.info

记录了复制用户的密码,它是纯文本格式的(每行一个值),随mysql版本变化而变化,应该对该文件的访问进行限制。

Relay-log.info

记录了当前二进制日志和中继日志的坐标,删除它的后果是服务器在重启之后就会忘记复制的位置,并可能重复复制。

Expire_logs_days

定义了mysql清除过期日志的时间。

Log_slave_updates

可以把一台从服务器变成主服务器,它指导mysql把自已执行的事件写入二进制日志中,然后自已的从服务器可就可以执行这些事件。

复制的过滤主要有2种方式:

1,在主服务器在把事件从进二制日志中过滤掉,相关的参数是:binlog_do_db和binlog_ignore_db。

2,在从服务器上把事件从中继日志中过滤掉,相关的参数是replicate_*。

复制只能扩展读取,不能扩展写入,对数据进行分区可以进行扩展写入。

复制常见的问题:

一,从服务器到底落后于主服务器多少?用Maatkit工具里面的mk-heartbeat脚本。

二,如何确定主从服务器的数据是否一致?用Maatkit工具里面的mk-table-checksum脚本。

复制的优化:

在mysql复制环境中,有8个参数可以让我们控制,需要复制或需要忽略不进行复制的DB或table分别为:

下面二项需要在Master上设置:

Binlog_Do_DB:设定哪些数据库需要记录Binlog

Binlog_Ignore_DB:设定哪里数据库不需要记录Binlog

优点是Master端的Binlog记录所带来的Io量减少,网络IO减少,还会让slave端的IO线程,SQL线程减少,从而大幅提高复制性能,

缺点是mysql判断是否需要复制某个事件不是根据产生该事件的查询所在的DB,而是根据执行查询时刻所在的默认数据库(也就是登录时指定的库名或运行"use database"中指定的DB),只有当前默认DB和配置中所设定的DB完全吻合时IO线程才会将该事件读取给slave的IO线程.所以,如果在默认DB和设定须要复制的DB不一样的情况下改变了须要复制的DB中某个Table中的数据,该事件是不会被复制到Slave中去的,这样就会造成Slave端的数据和Master的数据不一致.同样,在默认的数据库下更改了不须要复制的数据库中的数据,则会被复制到slave端,当slave端并没有该数据库时,则会造成复制出错而停止.

下面六项需要在slave上设置:

Replicate_Do_DB:设定需要复制的数据库,多个DB用逗号分隔

Replicate_Ignore_DB:设定可以忽略的数据库.

Replicate_Do_Table:设定需要复制的Table

Replicate_Ignore_Table:设定可以忽略的Table

Replicate_Wild_Do_Table:功能同Replicate_Do_Table,但可以带通配符来进行设置。

Replicate_Wild_Ignore_Table:功能同Replicate_Do_Table,功能同Replicate_Ignore_Table,可以带通配符。

优点是在slave端设置复制过滤机制,可以保证不会出现因为默认的数据库问题而造成Slave和Master数据不一致或复制出错的问题.

缺点是性能方面比在Master端差一些.原因在于:不管是否须要复制,事件都会被IO线程读取到Slave端,这样不仅增加了网络IO量,也给Slave端的IO线程增加了Relay Log的写入量.

主从复制的配置实例:

主服务器:192.168.0.176

从服务器:192.168.0.178

1,分别在主从服务器上执行下面的授权语句:

mysql> grant replication slave,replication client on *.* to chlotte@'192.168.0.%' identified by 'chlotte';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

2,查看主服务器上的binlog位置:

mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000009 |      195 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

3,在从服务器上执行chang master语句:

[root@localhost ~]# mysql -uroot -pourgame;

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.1.42-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> change master to master_host='192.168.0.176',

-> master_user='chlotte',

-> master_password='chlotte',

-> master_log_file='mysql-bin.000009',

-> master_log_pos=0;

Query OK, 0 rows affected (0.04 sec)

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State:

Master_Host: 192.168.0.176

Master_User: chlotte

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000009

Read_Master_Log_Pos: 4

Relay_Log_File: localhost-relay-bin.000001

Relay_Log_Pos: 4

Relay_Master_Log_File: mysql-bin.000009

Slave_IO_Running: No

Slave_SQL_Running: No

Replicate_Do_DB:

Replicate_Ignore_DB:

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: 4

Relay_Log_Space: 106

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: NULL

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

1 row in set (0.00 sec)

ERROR:

No query specified

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.0.176

Master_User: chlotte

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000009

Read_Master_Log_Pos: 338

Relay_Log_File: localhost-relay-bin.000002

Relay_Log_Pos: 483

Relay_Master_Log_File: mysql-bin.000009

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

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: 338

Relay_Log_Space: 642

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:

1 row in set (0.00 sec)

ERROR:

No query specified

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值