一、     [endif]mysql主从配置和主主配置

[if !supportLists]1.      [endif]主从配置

[if !supportLists]a)      [endif]环境描述

主服务器A:192.168.56.102:3307

从服务器B:192.168.56.102:3309

[if !supportLists]b)      [endif]配置过程

[if !supportLists]                            i.          [endif]在A新建同步用户master,密码为master,并赋予master REPLICAITON SLAVE权限

grant replication slave on *.* to 'master'@'localhost' identified by 'master';

flush privileges;

停止服务器A和B的mysql服务

[if !supportLists]                           ii.          [endif]修改my.cnf配置文件

服务器A :

      log-bin = /usr/local/mysql/data/mysql3307-log

     binlog-do-db = mysqltemp

     binlog-ignore-db = mysql    

服务器B :

       log-bin = /usr/local/mysql_1/mysql_1/data/mysql3310-log

      replicate-do-db = mysqltemp

      replicate-ignore-db = mysql

重启A和B的mysql服务

[if !supportLists]                          iii.          [endif]在服务器A上查看做为主服务器状态

服务器A:show master status\G

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

             File: mysql3307-log.000003

         Position: 379

     Binlog_Do_DB: mysqltemp

     Binlog_Ignore_DB: mysql

     Executed_Gtid_Set:

     1 row in set (0.00 sec)

[if !supportLists]                          iv.          [endif]在B上用change master指定同步位置

服务器B:change master to master_host='localhost',master_port=3307,master_user='master', master_password='master',

     master_log_file='mysql3307-log.000003', master_log_pos=379;

  注:master_log_file,master_log_pos由上面主服务器查出的状态值中确定。master_log_file对应File,master_log_pos对应Position。

[if !supportLists]                           v.          [endif]在B上启动从服务器线程:

start slave;

查看从服务器状态:

   show slave status\G;

   Slave_IO_Running: Yes

   Slave_SQL_Running: Yes

查看以上两项的值,均为Yes则表示状态正常

[if !supportLists]2.      [endif]主主配置

[if !supportLists]a)      [endif]环境描述

主服务器A:192.168.56.102:3307

主服务器B:192.168.56.102:3308

[if !supportLists]b)      [endif]配置过程

[if !supportLists]                            i.          [endif]在A和B上分别新建同步用户master,密码为master,并赋予master

REPLICAITON SLAVE权限

grant replication slave on *.* to 'master'@'localhost' identified by 'master';

flush privileges;

停止服务器A和B的mysql服务

[if !supportLists]                           ii.          [endif]修改my.cnf文件

服务器A :

      log-bin = /usr/local/mysql/data/mysql3307-log

     binlog-do-db = mysqltemp

     binlog-ignore-db = mysql

     log-slave-updates

     sync_binlog = 1

     auto_increment_offset = 1

     auto_increment_increment = 2

     replicate-do-db = mysqltemp

     replicate-ignore-db = mysql

  服务器B :

       log-bin = /usr/local/mysql_1/mysql_1/data/mysql3310-log

      replicate-do-db = mysqltemp

      replicate-ignore-db = mysql

      binlog-do-db = mysqltemp

      binlog-ignore-db = mysql

      log-slave-updates

      sync_binlog = 1

      auto_increment_offset = 2

      auto_increment_increment = 2

    重启A和B的mysql服务

[if !supportLists]                          iii.          [endif]分别在A和B上查看主服务器状态

服务器A:show master status\G

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

             File: mysql3307-log.000003

         Position: 379

     Binlog_Do_DB: mysqltemp

     Binlog_Ignore_DB: mysql

     Executed_Gtid_Set:

     1 row in set (0.00 sec)

   服务器B:show master status\G

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

             File: mysql3310-log.000003

         Position: 379

     Binlog_Do_DB: mysqltemp

     Binlog_Ignore_DB: mysql

     Executed_Gtid_Set:

     1 row in set (0.00 sec)

[if !supportLists]                          iv.          [endif]在A和B上分别指定同步位置

服务器A:change master to master_host='localhost',master_port=3310,master_user='master', master_password='master',

     master_log_file='mysql3310-log.000003', master_log_pos=379;

服务器B:change master to master_host='localhost',master_port=3307,master_user='master', master_password='master',

     master_log_file='mysql3307-log.000003', master_log_pos=379;

 注:master_log_file,master_log_pos由上面主服务器查出的状态值中确定。master_log_file对应File,master_log_pos对应Position。

[if !supportLists]                           v.          [endif]在A和B上启动从服务器线程

start slave;

   查看从服务器状态:

   show slave status\G;

   Slave_IO_Running: Yes

   Slave_SQL_Running: Yes

   查看以上两项的值,均为Yes则表示状态正常。

[if !supportLists]c)      [endif]配置参数说明

1、server-id

    ID值唯一的标识了复制群集中的主从服务器,因此它们必须各不相同。master_id必须为1到232–1之间的一个正整数值,slave_id值必须为2到232–1之间的一个正整数值。

 2、log-bin

    表示打开binlog,打开该选项才可以通过I/O写到Slave的relay-log,也是可以进行replication的前提。如果文件目录为自定义的其他路径,必须赋予使用赋予权限,避免 

 mysql启动失败。mysql为安装数据库时的用户

    shell>> chown -R mysql 目标文件目录;

 3、binlog-do-db

    表示需要记录进制日志的数据库。如果有多个数据库可用逗号分隔,或者使用多个binlog-do-db选项。

 4、binlog-ignore-db

    表示不需要记录二进制日志的数据库。如果有多个数据库可用逗号分隔,或者使用多个binlog-do-db选项。

 5、replicate-do-db

    表示需要同步的数据库,如果有多个数据库可用逗号分隔,或者使用多个replicate-do-db选项。

 6、replicate-ignore-db

    表示不需要同步的数据库,如果有多个数据库可用逗号分隔,或者使用多个replicate-ignore-db选项。

 7、log-slave-updates

    配置从库上的更新操作是否写入二进制文件,如果这台从库,还要做其他从库的主库,那么就需要打这个参数,以便从库的从库能够进行日志同步。

 8、sync_binlog=1 or N

    binlog在每N次binlog写入后与硬盘同步。当sync_binlog变量设置为1是最安全的,因为在crash崩溃的情况下,你的二进制日志binary log只有可能丢失最多一个语句

 或者一个事务。

 9、auto_increment_offset和auto_increment_increment

    这两个是为了防止两台互相复制的服务器产生关键字段的冲突的,如果他们使用不同的偏移,就可以实现一台服务器按照1、3、5、7...增加,另一台服务器按照2、4、6、8...增加,如果服务器的复制是单向的,就不需要这两个参数了。

    auto_increment_offset=1 自动增加的字段的初始值是1;auto_increment_increment=2 自动增加的字段每次增加2

[if !supportLists]d)      [endif]用于控制主从服务器的sql语句

一、用于控制主服务器的语句

 1、PURGE MASTER LOGS

   用于删除列于在指定的日志或日期之前的日志索引中的所有二进制日志。这些日志也会从记录在日志索引文件中的清单中被删除,这样被给定的日志成为第一个。

    A、要清理日志,需要按照一下步骤

       1)、在每个从服务器上运行show slave status来检查它正在读取哪个日志。

       2)、使用show master logs 获得主服务器上的一系列日志。

       3)、在所有的从服务器中判定最早的日志。这个是目标日志。

       4)、制作将要删除的所有日志的备份(建议备份)。

       5)、清理所有日志,但不包括目标日志。

    purge语法

       #删除binlog.000002之前的而不包含binlog.000002

      mysql> PURGE MASTER LOGS TO 'binlog.000002';

      #删除2011-05-28 1:35:00之前的

       mysql> PURGE MASTER LOGS BEFORE '2011-05-28 1:35:00';

      #清除3天前的binlog

      mysql> PURGE MASTER LOGS BEFORE DATE_SUB(NOW( ), INTERVAL 3 DAY);

   注:删除日志还可以通过在my.cnf中设置expire-logs-days参数

 2、RESET MASTER

   可以删除列于索引文件中的所有二进制日志,把二进制日志索引文件重新设置为空,并创建一个新的二进制日志文件。

 3、SET SQL_LOG_BIN = {0|1}

   如果客户端使用一个有SUPER权限的账户连接,则可以禁用或启用当前连接的二进制日志记录。如果客户端没有此权限,则语句被拒绝,并伴随有错误。

 4、SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]

   用于在二进制日志中显示事件。如果您不指定’log_name’,则显示第一个二进制日志。

 5、SHOW MASTER LOGS

   用于列出服务器中的二进制日志文件。

 6、SHOW MASTER STATUS

   用于提供主服务器二进制日志文件的状态信息。

 7、SHOW SLAVE HOSTS

   用于显示当前使用主服务器注册的复制从属服务器的清单。

二、用于控制从服务器的语句

 1、CHANGE MASTER TO

   可以更改从属服务器用于与主服务器进行连接和通讯的参数。

 2、MASTER_POS_WAIT()

   SELECT MASTER_POS_WAIT('master_log_file', master_log_pos)

   这实际上是一个函数,而不是一个语句。它被用于确认,从属服务器已读取并执行了到达主服务器二进制日志的给定位置。

 3、RESET SLAVE

   用于让从属服务器忘记其在主服务器的二进制日志中的复制位置。本语句被用于进行一个明确的启动:它会删除master.inforelay-log.info文件,以及所有的中继日志,

 并启动一个新的中继日志。

 4、SET GLOBAL SQL_SLAVE_SKIP_COUNTER = n

   从主服务器中跳过后面的n个事件。要复原由语句导致的复制中止,这是有用的。

 5、SHOW SLAVE STATUS

   用于提供有关从属服务器线程的关键参数的信息。

 6、START SLAVE

   用于启动从属服务器线程

 7、STOP SLAVE

   用于中止从属服务器线程

[if !supportLists]e)      [endif]主从监控脚本

以下脚本可以在192.168.12.238/app/forMysql/目录下载

           自动监控脚本mysql_slave_3307.sh

[if !supportLists]3.      [endif]mysql主从同步异常处理及处理

1、Slave I/O: error connecting to master 'backup@192.168.1.x:3306' - retry-time: 60  retries: 86400, Error_code: 1045

           1045:不能连接数据库,用户名或密码错误

           删除从服务器上的二进制日志文件,包括master.info和hostname-relay-bin开头的文件,重新使用change master to命令。

2、Slave SQL: Error 'Table 'xxxx' doesn't exist' on query. Default database: 't591'. Query: 'INSERT INTO `xxxx`(type,post_id,browsenum)

 SELECT type,post_id,browsenum FROM xxxx WHERE hitdate='20090209'', Error_code: 1146

           1146:数据表不存在。

           找不到表,在从服务器上执行slave stop ,创建数据表,使用slave start 。如果同步不执行,则使用change master to 命令。

3、Error 'Duplicate entry '1' for key 1' on query. Default database: 'movivi1'. Query: 'INSERT INTO `v1vid0_user_samename`

 VALUES(null,1,'123','11','4545','123')'


Error 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax 

to use near '‑' at line 1' on query. Default database: 'club'. Query: 'INSERT INTO club.point_process ( GIVEID, GETID, POINT, CREATETIME, DEMO ) 

VALUES ( 0, 4971112, 5, '2010-12-19 16:29:28',‑'

1 row in set (0.00 sec)


1062:字段值重复,入库失败。

先执行slave stop,在执行set global sql_slave_skip_counter =1,然后slave start。4、Relay log read failure: Could not parse relay log event entry. The possible reasons are: 

the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), 

the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), 

a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's

 binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave.


主数据库突然停止或问题终止,更改了mysql-bin.xxx日志,slave服务器找不到这个文件,需要找到同步的点和日志文件,然后chage master to即可

5、Error 'Unknown column 'qdir' in 'field list'' on query. Default database: 'club'. Query: 'insert into club.question_del (id, pid, 

ques_name, givepoint, title, subject, subject_pid, createtime, approve, did, status, intime, order_d, endtime,banzhu_uid,

banzhu_uname,del_cause,qdir) select id, pid, ques_name, givepoint, title, subject, subject_pid, createtime, approve, did, 

status, intime, order_d, endtime,'1521859','admin0523','无意义回复',qdir from club.question where id=7330212'

1 row in set (0.00 sec)


           表中缺少字段,在主服务器上查询字段信息,添加到从服务器即可。

[if !supportLists]4.      [endif]binlog日志

    binlog日志用于记录所有更新了数据或者已经潜在更新了数据(例如,没有匹配任何行的一个DELETE)的所有语句。语句以“事件”的形式保存,它描述数据更改。

1、binglog的查看
通过mysqlbinlog命令可以查看binlog的内容。

shell > mysqlbinlog  /home/mysql/binlog/binlog.000003  | more      

[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter" /> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0" /> <v:f eqn="sum @0 1 0" /> <v:f eqn="sum 0 0 @1" /> <v:f eqn="prod @2 1 2" /> <v:f eqn="prod @3 21600 pixelWidth" /> <v:f eqn="prod @3 21600 pixelHeight" /> <v:f eqn="sum @0 0 1" /> <v:f eqn="prod @6 1 2" /> <v:f eqn="prod @7 21600 pixelWidth" /> <v:f eqn="sum @8 21600 0" /> <v:f eqn="prod @7 21600 pixelHeight" /> <v:f eqn="sum @10 21600 0" /> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" /> <o:lock v:ext="edit" aspectratio="t" /> </v:shapetype><v:shape id="图片_x0020_7" o:spid="_x0000_i1025" type="#_x0000_t75" style="width:415pt;height:96pt;visibility:visible;mso-wrap-style:square;"> <v:imagedata src="file://localhost/Users/loren/Library/Caches/TemporaryItems/msoclip/0/clip_image001.png" kesrc="file://localhost/Users/loren/Library/Caches/TemporaryItems/msoclip/0/clip_image001.png" o:title="" /> </v:shape><![endif][if !vml][endif]

1)、“at 196”说明“事件”的起点,是以第196字节开始;“end_log_pos 294”说明以第294字节结束。

2)、事件发生的时间戳:“120330 17:54:46”。

3)、事件执行花费的时间:"exec_time=28"。


2、和binlog有关参数
log_bin
           设置此参数表示启用binlog功能,并指定路径名称
log_bin_index
           设置此参数是指定二进制索引文件的路径与名称
binlog_do_db
           此参数表示只记录指定数据库的二进制日志
binlog_ignore_db
           此参数表示不记录指定的数据库的二进制日志
max_binlog_cache_size
           此参数表示binlog使用的内存最大的尺寸
binlog_cache_size
           此参数表示binlog使用的内存大小,可以通过状态变量binlog_cache_use和       binlog_cache_disk_use来帮助测试。
binlog_cache_use:使用二进制日志缓存的事务数量
binlog_cache_disk_use:使用二进制日志缓存但超过binlog_cache_size值并使用       临时文件来保存事务中的语句的事务数量
max_binlog_size
           Binlog最大值,最大和默认值是1GB,该设置并不能严格控制Binlog的大小,尤其是Binlog比较靠近最大值而又遇到一个比较大事务时,为了保证事务的完整性,不可能做切换日志的动作,只能将该事务的所有SQL都记录进当前日志,直到事务结束


  • 无标签