zoukankan      html  css  js  c++  java
  • 搭建mysql主从复制---Mysql Replication

    • 主从复制原理

      Mysql的Replication是一个异步的复制过程,从一个Mysql Instance(master)复制到另一个Mysql Instance(slave)。中间需要三个线程slave端有1个I/O线程,一个SQL线程,Master端一个I/O线程。

      要实现Mysql的Replication,首先在Master端打开Binary log(mysql-bin.xxxxxx)功能。因为原理是slave从Master端获取mysql-bin.xxxxxx,然后在slave端按照顺序依次执行日志中的各种操作。可以在my.cnf配置文件的[mysqld]组中添加"log-bin"参数项。

    • 复制的基本过程:

      a、Slave的IO线程链接多Master,并请求日志文件的指定位置之后的内容。

      b、Master接收到请求后,负责复制的IO线程根据请求的信息读取指定日志指定位置的日志信息,返回给Slave的IO线程。内容还包括本次返回的信息在Master端的Binary Log的日志文件名和位置。

      c、Slave端的IO线程街道信息后,将内容写入Slave端的Relay Log(mysql-relay-log.xxxxxx)末端,并将Master端的bin-log文件和位置记录到master-info文件中,以便Slave的IO线程下次连接Master的时候使用。

      d、Slave端的SQL线程检测到Relay Log中的新内容后,马上解析Log内容,还原成在Master端的真实执行的Query语句,并执行。两端执行了相同的Query语句,二者数据同步。

    • 准备工作

    从原理的分析中可以看到,实现Mysql Replication 至少需要两台Mysql实例。

    这里在虚拟机vmware中准备了两个台centos5,因为同时开多个虚拟机会很卡,就简单的开启两个吧。

    Master:192.168.80.7

    Slave:192.168.80.6

    wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.21.tar.gz
    yum install vim* -y
    yum install -y gcc gcc-c++
    yum install -y bison*
    yum install cmake
    yum install ncurses* -y
    • 安装Mysql Server
    tar zxvf mysql-5.6.21.tar.gz  -C /usr/local/src/
    cd /usr/local/src/mysql-5.6.21/
    groupadd mysql
    useradd -r -g mysql mysql
    cmake .
    make install
    cd /usr/local/mysql/
    chown -R mysql .
    chgrp -R mysql .
    scripts/mysql_install_db  --user=mysql
    chown -R root .
    chown -R mysql data
    bin/mysqld_safe --user=mysql &  #测试mysql是否安装成功
    cp support-files/mysql.server  /etc/init.d/mysql
    chkconfig --add mysql #将mysql脚本加入服务 可以使用service管理
    ln -s `pwd`/my.cnf /etc/my.cnf
    • Master端

     修改Master端的主配置文件

    [mysqld]
    server-id=1
    log-bin=mysql-bin
    binlog-do-db=test
    binlog-ignore-db=mysql

    备份Master的data目录 

    这里采用停库,冷备份。实际上还可以热备份需要锁库

    flush tables with read tables;
    unlock tables;

    service mysql stop
    tar czvf data.tar.gz /usr/local/mysql/data ###/usr/local/mysql/data 为Master端mysql的datadir
    
    将data.tar.gz覆盖掉从服务器的datadir

     

    • Slave端

    将data.tar.gz覆盖掉从服务器的datadir

    获取Master的Bin-log的Log Postion

    show Master  status
    
    | mysql-bin.000003 |      120 | test,test    | mysql,mysql      |  

    在Master端,创建复制Binary Log日志的用户【才操作需要再Master端执行】

    create user 'repl'@'%' identified by '123456';
    grant replication slave on *.* to 'repl'@'%';
    flush privileges; #如不及时刷新会有错误

    接下来需要修改Slave的主配置文件

    [mysqld]
    server-id=3
    log-bin=mysql-bin
    binlog-do-db=test
    binlog-ignore-db=mysql

    修改完以后,重启salve端的mysql服务

    service mysql restart

    在mysql里执行:

    mysql>CHANGE MASTER TO
    MASTER_HOST='192.168.80.7',
    MASTER_USER='repl',
    MASTER_PASSWORD='123456',
    MASTER_LOG_FILE='mysql-bin.000003',
    MASTER_LOG_POS=120;
    start slave

    至此,Mysql Replaction搭建完成。

    • 检查是否正常执行

    在Master端执行

    show master status;
    | mysql-bin.000001 |      120 | test,test    | mysql,mysql      |  

    显示如上图说明配置成功。

    在Slave端,

    mysql> show slave statusG
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.80.7
                      Master_User: repl
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000003
              Read_Master_Log_Pos: 1291
                   Relay_Log_File: localhost-relay-bin.000002
                    Relay_Log_Pos: 1454
            Relay_Master_Log_File: mysql-bin.000003
                 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: 1291
                  Relay_Log_Space: 1631
                  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: 22dd6712-695b-11e4-b733-000c292f9c4c
                 Master_Info_File: /usr/local/mysql/data/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)
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
    这两个线程必须正常。
    显示结果如上则表示Mysql Replication正常工作。

    可以在Master端做一个CURD操作,来检查是否正常同步。






    /********************************************************/
    • 可能的问题:

    我安装的时候出现的问题有
    1、创建用户的时候 出现这个错误'repl@%',应该为 'repl'@'%'
    2、因为复制data目录的时候将data目录下的auto.cnf也复制了。
    auto.cnf的内容如下:

    [auto]
    server-uuid="xxxx"

    导致uuid重复的错误。

     3、配置 change master to的时候

    CHANGE MASTER TO
    MASTER_HOST='192.168.80.7',
    MASTER_USER='repl',
    MASTER_PASSWORD='123456',
    MASTER_LOG_FILE='mysql-bin.000003',
    MASTER_LOG_POS=120;

    将master_log_file的名称写错了.写成了'mysql_bin.00003'

    在这里还可能用到这些命令

    show variables like 'server_id';
    set global server_id=2;

    /**************************************/

    • 对于uuid

    查看uuid:
    blkid
    ls -l /dev/disk/by-uuid/
    dumpe2fs /dev/sda6 | less (查看指定设备的UUID)

    产生uuid:
    uuidgen
    为设备指定uuid
    sudo tune2fs -U `uuidgen` device

     另一篇,可以参考:http://www.cnblogs.com/luckcs/articles/2543607.html

  • 相关阅读:
    【SQL】182. Duplicate Emails
    【SQL】181. Employees Earning More Than Their Managers
    【SQL】180. Consecutive Numbers
    【SQL】178. Rank Scores
    【SQL】177. Nth Highest Salary
    如何处理postman Self-signed SSL certificate blocked错误
    Radio checked 属性
    转成百分比
    内建函数
    队列
  • 原文地址:https://www.cnblogs.com/simpman/p/4091104.html
Copyright © 2011-2022 走看看