ITPub博客

首页 > 数据库 > MySQL > mysql 主从复制

mysql 主从复制

原创 MySQL 作者:raysuen 时间:2017-05-09 11:32:22 0 删除 编辑
主从复制之一主一从

master配置

开启binlog日志
[mysqld]
servier-id=    #2个服务器之间不能一样
log-bin = /data/3306/data/ray-bin.log  #添加log-bin参数,指定binlog日志的名称和路径


确认binlog日志是否打开
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.00 sec)

创建同步使用的用户。不建议使用root,权限较大
mysql> grant replication slave on *.* to 'rep'@'%' identified by '123456';
Query OK, 0 rows affected (0.59 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


备份主库
[root@ray ~]# mysqldump -uroot -p123456 -S /data/3306/soket/mysql.sock -B --flush-privileges --single-transaction --master-data=1 --flush-logs --routines --triggers --events --hex-blob --all-databases > /data/3306/mysql_backup_20151125.sql
Warning: Using a password on the command line interface can be insecure.


slave配置

修改从库参数,指定中继日志位置:
[root@ray data]# vi 3307/my.cnf
[mysqld]
relay_log = /data/3307/data/relay-bin.log

mysql> show global variables like 'relay%';
+---------------------------+---------------------------------+
| Variable_name             | Value                           |
+---------------------------+---------------------------------+
| relay_log                 | /data/3307/data/relay-bin.log   |
| relay_log_basename        | /data/3307/data/relay-bin       |
| relay_log_index           | /data/3307/data/relay-bin.index |
| relay_log_info_file       | relay-log.info                  |
| relay_log_info_repository | FILE                            |
| relay_log_purge           | ON                              |
| relay_log_recovery        | OFF                             |
| relay_log_space_limit     | 0                               |
+---------------------------+---------------------------------+

恢复备库
[root@ray ~]# mysql -uroot -p123456 -S /data/3307/soket/mysql.sock < /data/3306/mysql_backup_20151125.sql 
Warning: Using a password on the command line interface can be insecure.


change master to master_host='192.168.56.91', master_port=3306, master_user='rep', master_password='123456', master_log_file='ray-bin.000007', master_log_pos=120;
执行
mysql> change master to master_host='192.168.56.91', master_port=3306, master_user='rep', master_password='123456', master_log_file='ray-bin.000007', master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)


mysql> start slave;
Query OK, 0 rows affected (0.37 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.56.91
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: ray-bin.000008
          Read_Master_Log_Pos: 120
               Relay_Log_File: ray-relay-bin.000003
                Relay_Log_Pos: 281
        Relay_Master_Log_File: ray-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: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 120
              Relay_Log_Space: 805
              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: 2
                  Master_UUID: 840f94e0-8ea0-11e5-af92-080027a94012
             Master_Info_File: /data/3307/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)


级联配置
在从1库开启
log-bin = ******
log-slave-updates



读写分离思路
1 在my.cnf里指定数据库read only,super权限不被限定
[mysqld]
read-only
2 根据参数指定忽略库,或者表
    replicate-ignore-db = mysql
    binlog-ignore-db = mysql
    binlog-ignore-db = information_schema
  binlog-ignore-db = performance_schema


主从复制重要参数
master
--binlog-do-db  #二进制日志记录的数据库,多个数据库用逗号(,)分隔
--binlog-ignore-db   #二进制日志忽略的数据库,多个数据库用逗号(,)分隔
slave
--replicate-do-db  #指定需要复制的数据库,多个数据库用逗号(,)分隔
--replicate-ignore-db  #指定忽略复制的数据库,多个数据库用逗号(,)分隔
--replicate-do-table #指定需要复制的表,多个数据库用逗号(,)分隔
--replicate-ignore-table  #指定忽略复制的表,多个数据库用逗号(,)分隔
--replicate-wild-do-table  #类似--replicate-do-table,但可是使用通配符
--replicate-wild-ignore-table  #类似--replicate-ignore-table ,但可是使用通配符


错误解决
stop slave;
set global sql_slave_skip_counter=1;
start slave;

指定参数文件忽略错误
slave-skip-errors = 1032,1062,1007


主从切换
从变主:
mysql> show processlist;
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time | State                                                                       | Info             |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
|  1 | root        | localhost | ray2 | Query   |    0 | init                                                                        | show processlist |
|  2 | system user |           | NULL | Connect |  329 | Waiting for master to send event                                            | NULL             |
|  3 | system user |           | NULL | Connect |   35 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL             |

stop slave;
reset master;

删除master.info relay-log.info
rm -rf master.info relay-log.info
确认授权用户
修改参数文件
 关闭read-only,relay-log,log-slave-updates
 开启bin-log,

其他从:
stop slave;
change master to master_host='';
start slave;
show slave status\G


为了复制的安全性,可以考虑在从服务器设置:
    sync_master_info = 1    
    sync_relay_log = 1   
    sync_relay_log_info = 1

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28572479/viewspace-2138723/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: C 时间格式化
请登录后发表评论 登录
全部评论
擅长oracle,Linux。精通shell,python。

注册时间:2016-08-23

  • 博文量
    145
  • 访问量
    227105