zoukankan      html  css  js  c++  java
  • Mysql Replication 主从同步


    简介:

    Mysql 的主从同步功能,这种解决方案是企业很常见的一种。常用于备份数据库,当客户端操作主库时,主库会产生binlog日志文件,
    从库通过复制主库的binlog日志文件,然后解析成相应的 SQL 语句在从库执行,实现主从一致的效果。
    这种解决方案只提供了日志的同步执行功能,而从库只能提供读操作,当主服务器发生故障时,必须手动处理故障转移,一般情况下的做法是将一台从服务器改为主服务器。

    Master : 192.168.1.88
    Slave : 192.168.1.80

    一、配置 Master

    [root@master ~]# vim /etc/my.cnf
    
    log-bin = mysql-bin # 开启 binlog 日志记录
    server-id = 1 # 设置 id 号,此 ID 号全局唯一
    
    [root@master ~]# service mysqld start
    
    [root@master ~]# netstat -anpt | grep 3306 # 检测 Mysql 是否启动成功
    
    [root@master ~]# chkconfig --add mysqld
    [root@master ~]# chkconfig --level 35 mysqld on
    
    [root@master ~]# iptables -I INPUT -s 192.168.1.80 -p tcp --dport 3306 -j ACCEPT
    
    [root@master ~]# service iptables save

    二、配置 Slave

    [root@slave ~]# vim /etc/my.cnf
    
    server-id = 2                           # 设置 id 号,此 ID 号全局唯一
    relay-log = mysql-relay-bin             # 指定 relay-log 日志文件的命名格式
    replicate-wild-ignore-table = mysql.%   # 过滤不需要复制的表
    replicate-wild-ignore-table = test.%
    replicate-wild-ignore-table = information_schema.%
    replicate-wild-ignore-table = performance_schema.%
    
    [root@slave ~]# service mysqld start
    
    [root@slave ~]# chkconfig --add mysqld
    [root@slave ~]# chkconfig --level 35 mysqld on
    
    [root@slave ~]# iptables -I INPUT -s 192.168.1.88 -p tcp --dport 3306 -j ACCEPT
    
    [root@slave ~]# service iptables save

    三、小插曲

    四、Master 创建复制用户并授权

    [root@master ~]# mysql -u root -p123456
    
    mysql> grant replication slave on *.* to 'rep1_user'@'192.168.1.80' identified by 'rep1_passwd';
    
    mysql> show master status;
    +------------------+----------+--------------+------------------+
    | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000008 | 324 | | |
    +------------------+----------+--------------+------------------+
    1 row in set (0.00 sec)

    五、Slave 添加 Master 信息

    [root@slave ~]# mysql -u root -p123456
    
    mysql> change master to 
    -> master_host='192.168.1.88',
    -> master_user='rep1_user',
    -> master_password='rep1_passwd',
    -> master_log_file='mysql-bin.000008',
    -> master_log_pos=324;
    
    mysql> start slave;
    
    mysql> show slave statusG
    *************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
    Master_Host: 192.168.1.88
    Master_User: rep1_user
    Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: mysql-bin.000008
    Read_Master_Log_Pos: 324
    Relay_Log_File: mysql-relay-bin.000002
    Relay_Log_Pos: 267
    Relay_Master_Log_File: mysql-bin.000008
    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: mysql.%,test.%,information_schema.%,performance_schema.%
    Last_Errno: 0
    Last_Error:
    Skip_Counter: 0
    Exec_Master_Log_Pos: 324
    Relay_Log_Space: 430
    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: b5e20203-86b5-11e4-b69c-000c29d099fa
    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:
    1 row in set (0.00 sec)

    ## 重点关注:Slave_IO_Running 跟 Slave_SQL_Running 这两个主从复制线程,正常情况下这两项都为 YES !

    ## 还需要关注:Slave_IO_State、Master_Host、Master_Log_File、Read_Master_Log_Pos、Relay_Log_File、Relay_Log_Pos、Relay_Master_Log_File

    ## Replicate_Wild_Ignore_Table 这项可以看到同步过程中过滤了哪些库(表)。

    附:

    三、小插曲

    ## 在做主从同步前,主库已经有数据时,需要做以下处理。

    mysql> flush tables with read lock;   # 主库锁表操作,让数据库只能读,不能写

    ## 保留此终端,重新打开一个 tty ,如果关闭此终端则锁表失效。

    [root@master ~]# cd /usr/local/
    
    [root@master local]# tar zcf mysql.tar.gz mysql   # 备份整个数据库
    
    [root@master local]# rsync -a mysql.tar.gz 192.168.1.80:/usr/local/   # 将数据传到从库
    
    [root@slave local]# service mysqld stop     # 关闭从库的 Mysql
    
    [root@slave local]# mv mysql old.mysql      # 将原有 mysql 目录改名
    
    [root@slave local]# tar zxf mysql.tar.gz    # 解压出主库的 mysql 目录
    
    [root@master local]# service mysqld restart # 重启主库 Mysql 服务
    
    [root@slave local]# service mysqld start    # 启动从库 Mysql 服务
  • 相关阅读:
    Days like that:
    获取网站访问来源URL
    论坛里面的一个帖子
    Days like that:
    简单项目布署
    论坛里面的一个帖子
    获取网站访问来源URL
    piwik开放源代码的Web统计软件
    p标签里面不能嵌套div
    IO操作 第二篇 学习(转载)
  • 原文地址:https://www.cnblogs.com/wangxiaoqiangs/p/5639920.html
Copyright © 2011-2022 走看看