mysql主从复制原理

MySQL主从复制与读写分离

 

MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践mysql

Mysql做为目前世界上使用最普遍的免费数据库,相信全部从事系统运维的工程师都必定接触过。但在实际的生产环境中,由单台Mysql做为独立的数据库是彻底不能知足实际需求的,不管是在安全性,高可用性以及高并发等各个方面。sql

所以,通常来讲都是经过 主从复制(Master-Slave)的方式来同步数据,再经过读写分离(MySQL-Proxy)来提高数据库的并发负载能力 这样的方案来进行部署与实施的。数据库

以下图所示:
vim

 

主从原理

mysql主从复制原理
 

 

从库生成两个线程,一个I/O线程,一个SQL线程;
 
i/o线程去请求主库 的binlog,并将获得的binlog日志写到relay log(中继日志) 文件中;
主库会生成一个 log dump 线程,用来给从库 i/o线程传binlog;
 
SQL 线程,会读取relay log文件中的日志,并解析成具体操做,来实现主从的操做一致,而最终数据一致;
 
 

操做步鄹:安全

准备2台机子  分别启动数据库,一台为master,一台slavebash

master 10.0.0.1  3000服务器

slave   10.0.0.2   3003并发

 

一.修改master配置文件 vim  /etc/my.confapp

   log-bin = 3306-bin    开启bin-log,也能够指定路径运维

     erver-id = 1            惟一标示

二.在master库建立个用户容许从库来同步

mysql> grant replication slave on *.* to 'rep'@'60.206.137.244' identified by 'test123';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement                            ####若是报错mysql> flush privileges;下在执行建立用户

# mysqldump -h127.0.0.1 -uroot -poldboy -P3000 -A -B --master-data=2 --events  > /tmp/mysql.sql     ##把主的数据备份出来

# mysql -h127.0.0.1 -uroot -poldboy -P3003 < mysql.sql                                                  ##把主库的数据导入到从库

三,修改从配置文件

     修改slave配置文件 vim  /etc/my.conf

     log-bin = 3306-bin    开启bin-log,也能够指定路径

          server-id = 2          惟一标示             ##主从要不同

经过命令行登陆管理MySQL服务器
mysql -uroot -p'密码'

执行从库SQL语句

change master to
master_host='60.206.137.244',
master_port=3000,
master_user='rep',
master_password='test123',
master_log_file='3306-bin.000001',
master_log_pos=333;

正确执行后启动Slave同步进程
mysql> start slave;

主从同步检查
mysql> show slave status\G

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 60.206.137.244
                  Master_User: rep
                  Master_Port: 3000
                Connect_Retry: 60
              Master_Log_File: 3306-bin.000001
          Read_Master_Log_Pos: 606
               Relay_Log_File: 3308-relay-bin.000002
                Relay_Log_Pos: 555
        Relay_Master_Log_File: 3306-bin.000001
             Slave_IO_Running: Yes                          ###若是是OK  证实成功
            Slave_SQL_Running: Yes                         ###若是是OK 证实成功
              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: 606
              Relay_Log_Space: 727
              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相差的数据差 通常是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: 1f18ad9e-8e2d-11e8-9752-5254005c6bc7
             Master_Info_File: /data/ops/app/mysql-5.6.23/3308/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

验证:

mysql> create database test;                   ##主库建立库
Query OK, 1 row affected (0.00 sec) 

mysql> show databases;                              ##从库能收到数据证实成功 

相关文章
相关标签/搜索