mysql主从复制

MySQL主从复制
    
    主从复制原理:

mysql主从同步其实是一个异步复制的过程,要实现复制首先在master上开启bin-log日志功能。整个过程需要开启3个线程,
分别是master开启IO线程,slave开启IO线程和SQL线程.

(1) 在slave服务器执行start slave,服务器的IO线程的请求后,master服务器的IO线程根据slave服务器发送指定的bin-log日志之后得内容,
   然后返回给slave端的IO线程;(返回的信息中除了bin-log日志内容外,还有本地返回日志内容后再master服务器的新的binlog文件
   及在binlog中的下一个指定更新位置)
   
(2) slave的IO线程接收到信息后,将接受到的日志一次添加到slave端的relay-log文件的最末端,并将读取到的master端的bin-log的文件名
   和位置记录到master-info文件中,以便在下一次读取的时候能够清楚的告诉master, 
   "我需要从某个bin-log的哪个位置开始往后的日志内容,请发给我;"
  
(3) slave的SQL线程检测到relay-log中新增加了内容后,会马上解析relay-log的内容成为master端真实执行
   时间的那些可执行的内容,并且重新回放.

        安装其实很简单,主要得理解原理。

        环境:mariadb-5.5.44源码

        mysql 主:192.168.155.12
        mysql 从:192.168.155.13

        实现mysql主从同步,以下是mysql主从操作步骤、(源码环境以及按照好)

   mysql安装目录 /usr/local/mysql
   mysql数据目录/mydata/data
   mysql配置文件/etc/my.cnf

在mysql主上,操作如下:

# vim /etc/my.cnf
[client]
port        = 3306
socket        = /tmp/mysql.sock
[mysqld]
port        = 3306
socket        = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 8
datadir = /mydata/data                       ##mysql数据目录
innodb_file_per_table = on                   ##InnoDB为独立表空间模式,每一个数据库,每一个表都会生成一个数据空间.
skip_name_resolve = on                       ##关闭dns反解
log-bin=mysql-bin                            ##binlog文件名字,文件默认存放于数据目录中,即:/mydata/data
binlog_format=row                            ##binlog格式为基于行的复制
server-id = 1                                ##master必须指定唯一server_id
sync_binlog = 1                              ##及时写入bin-log日志
log_error= /mydata/data/error.log            ##mysql错误日志
[mysqld-safe]
pid-file=/mydata/data/mysqld.pid             ##mysql的pid存放路径
replicate-do-db =all                         ##同步全部的数据库,如果只同步某一个数据库,改成数据库名称就可以.
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
# /etc/init.d/mysqld restart
# mysql
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.155.13' IDENTIFIED BY 'replpass';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show master status \G
*************************** 1. row ***************************
            File: mysql-bin.000004
        Position: 500
    Binlog_Do_DB: 
Binlog_Ignore_DB: 
1 row in set (0.00 sec)

在mysql从上,操作如下:

# vim /etc/my.cnf
[client]
port        = 3306
socket        = /tmp/mysql.sock
[mysqld]
port        = 3306
socket        = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 8
datadir = /mydata/data                    
innodb_file_per_table = on               
skip_name_resolve = on              
relay-log=relay-log                        ##开启中继日志,中继日志名为relay-log,默认存放于数据目录/mydata/data下
relay-log-index=relay-log.index            ##定义relay_log的位置和名称             
binlog_format=row               
server-id = 2                              ##slave必须指定唯一server_id                        
sync_binlog = 1                      
log_error= /mydata/data/error.log  
[mysqld-safe]
pid-file=/mydata/data/mysqld.pid    
replicate-do-db =all          
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
# /etc/init.d/mysqld restart
# mysql
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.155.12',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=500;
Query OK, 0 rows affected (0.04 sec)

MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.155.12
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 500
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: No            ##没开启IO线程
            Slave_SQL_Running: No            ##没开启SQL线程
              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: 500
              Relay_Log_Space: 245
              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: NULL
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: 0
1 row in set (0.00 sec)
MariaDB [(none)]> start slave ;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.155.12
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 500
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Connecting
            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: 500
              Relay_Log_Space: 245
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 2003
                Last_IO_Error: error connecting to master 'repluser@192.168.155.12:3306' - retry-time: 60  retries: 86400  message: Can't connect to MySQL server on '192.168.155.12' (113)
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
1 row in set (0.00 sec)
问题:为什么开启IO/SQL线程还是不能同步;
解决思路:检查下iptables是否关闭,ip:port是否通了

# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]

关闭防火墙之后,进入mysql查看下slave状态;
# mysql
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.155.12
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 500
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes            ##代表已经开启IO线程
            Slave_SQL_Running: Yes            ##代表已经开启SQL线程
              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: 500
              Relay_Log_Space: 817
              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
1 row in set (0.00 sec)

mysql主从已经搭建好,接下来就是测试主从复制是否成功;

测试阶段:

在master上操作:
MariaDB [(none)]> create database mydb;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use mydb;
Database changed
MariaDB [mydb]> create table tb1 (id int , name varchar(30), age int);
Query OK, 0 rows affected (0.02 sec)

MariaDB [mydb]> insert into tb1 (id, name, age) values (1,'hx',20),(2,'yl','21');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [mydb]> select * from tb1;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | hx   |   20 |
|    2 | yl   |   21 |
+------+------+------+
2 rows in set (0.00 sec)
在slave上测试,查看是否同步过来.
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> use mydb;
Database changed
MariaDB [mydb]> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| tb1            |
+----------------+
1 row in set (0.00 sec)

MariaDB [mydb]> select * from tb1;
+------+------+------+
| id   | name | age  |
+------+------+------+
|    1 | hx   |   20 |
|    2 | yl   |   21 |
+------+------+------+
2 rows in set (0.00 sec)

MariaDB [mydb]> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.155.12
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 893
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 922
        Relay_Master_Log_File: mysql-bin.000004
             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: 893
              Relay_Log_Space: 1210
              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
1 row in set (0.00 sec)

slave以及同步了maste的数据库、表、以及数据.
结论:
因为数据量很小,虽然是个异步复制的过程,但是我们可以感觉到复制的速度很快,和同步没有两样。
在我们的生产环境,也是使用mysql主从数据。因为insert,update,delete是操作会比较频繁,会有5-10秒的延迟时间.

主从同步的过程中,也会出现主从数据不一致的问题。这样可能就需要你重新做一遍主从复制了。

有些地方如果理解有错误,可以评论下本文章,一起努力学习及分享。

作者: kattall 
Q Q : 532461968 
感谢: MageEdu

原创文章,作者:Net20_赤羽,如若转载,请注明出处:http://www.178linux.com/20082

(1)
Net20_赤羽Net20_赤羽
上一篇 2016-06-26
下一篇 2016-06-26

相关推荐

  • Linux基础目录名称命名法则及功能规定

    Linux中有非常多的目录文件,那么这些目录文件的命令规则,和功能都有那些,今天学习了下,下面是我的学习记录,跟大家分享下。 文件命名规则 (1) 除了/之外,所有字符都合法 (2) 特殊字符如@、#、¥、&、()、-、空格等最好不要使用,当使用空格作为文件名时,执行命令会出错 (3) 避免使用”.”作为文件名的第一个字符,因为在Linux系统中以”…

    Linux干货 2016-08-15
  • Centos图形界面和命令界面模式切换

    大家都知道,centos有两种用户界面模式,一种是图形界面,也就是我们常说的Xwindows界面;另一种就是命令界面。有时因为方便需要在图形模式下安装一些程序,因图形界面占用的内存等资源较大,影响系统的运行,安装完后需要把系统切换到命令模式下运行,下面就两种模式如何进行切换做一下说明。 CentOS的启动模式共分为7级,分别是: 0-  停机 1-…

    系统运维 2015-07-19
  • shell-语句总结

    shell脚本语法总结 过程式编程语言: 顺序执行 选择执行 循环执行 shell默认是顺序执行,如果有判断或循环语句则执行判断或循环。 条件判断 if     单分支         if 判断条件:then  &nb…

    Linux干货 2016-08-21
  • 马哥教育网络班20期-第四周课程作业

    Table of Contents 1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 2、编辑/etc/group文件,添加组hadoop。 3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/h…

    Linux干货 2016-06-26
  • Bash编程之流程控制

    Bash作为一种过程式编程语言,拥有一套流程控制体系,可完成选择执行,循环执行功能。下面分别介绍if/else,case,select,for,while/until等语法的使用。

    Linux干货 2016-08-21
  • RAID特性和常见级别

    简介:    RAID全称为独立磁盘冗余阵列(Redundant Array of Independent Disks),基本思想就是把多个相对便宜的硬盘组合起来,成为一个硬盘阵列组,使性能达到甚至超过一个价格昂贵、 容量巨大的硬盘。RAID通常被用在服务器电脑上,使用完全相同的硬盘组成一个逻辑扇区,因此操作系统只会把它当做一个硬盘。 R…

    Linux干货 2016-02-14