Mysql 主从

来源:互联网 发布:知乎 陈茂辉 编辑:程序博客网 时间:2024/06/14 20:30
mysql主从复制简述:
Mysql复制的方式:
1: 基于SQL语句的复制(statement-based replication, SBR)
     在主服务器上执行的SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高。一旦发现没法精确复制时,   会自动选着基于行的复制  
2: 基于行的复制(row-based replication, RBR)
     把改变的内容复制过去,而不是把命令在从服务器上执行一遍. 从mysql5.0开始支持
3: 混合模式复制(mixed-based replication, MBR)
    默认采用基于语句的复制,一旦发现基于语句的无法精确的复制时,就会采用基于行的复制。

Mysql主从的特点:

1:  数据分布 (Data distribution )

2:  负载平衡(load balancing)
3:  备份(Backups)
4:  高可用性和容错行 High availability and failover

Mysql主从复制如何工作
1:  master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);
2:  slave将master的binary log events拷贝到它的中继日志(relay log);    
3:  slave重做中继日志中的事件,将改变反映它自己的数据。


Mysql主从复制配置:
Master和slave的MySQL数据库版本必须完全相同,防火墙开通3306端口
1:配置:
 Master
  server-id=1  #Master服务器ID值,一般可以设置为IP最后一位
  log-bin=mysql-bin  #二进制变更日志

  配置完成后,重启mysql服务,然后通过SHOW MASTER STATUS查看状态

  +------------------+----------+--------------+------------------+
  | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  +------------------+----------+--------------+------------------+
  | mysql-bin.000006 |     3757 |              | mysql            |
  +------------------+----------+--------------+------------------+

 Slave:
  log_bin = mysql-bin  #二进制变更日志,Slave中非必须项
  server_id = 2  #Slave服务器ID值,一般可以设置为IP最后一位
  relay_log = mysql-relay-bin  #中继日志
  log_slave_updates = 1  #slave将复制事件写进自己的二进制日志
  read_only = 1  #防止改变数据(除了特殊的线程)
2:添加账户
  Master服务器中添加同步数据的账户,并授予授予REPLICATION SLAVE权限
  GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO 'tester'@192.168.81.112’ IDENTIFIED BY '123456'
  只允许192.168.81.112访问
3:复制数据
需要将Master数据库复制至Slave中,同时保证复制过程中Master中无新数据写入。这样保证数据一致性。如果数据库数据都是全新的,可忽略

4:启动Slave:
  change master to master_host='192.168.81.133',master_user='tester',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=0
  其中master_log_file和SHOW MASTER STATUS中的保持一致
  通过SHOW SLAVE STATUS\G命令查看Slave设置,然后START SLAVE;启动Slave,

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.81.113
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000006
          Read_Master_Log_Pos: 3757
               Relay_Log_File: mysqld-relay-bin.000020
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000006
             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: 3757
              Relay_Log_Space: 552
              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:
               
Notice:
其中以下四项均一一对应,保持一致。同时Slave_SQL_Running和Slave_IO_Running任意一个为No均为有问题存在
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Master_Log_File: mysql-bin.000006
Relay_Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 3757
Exec_master_log_pos: 3757


主从深入....后续

0 0
原创粉丝点击