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 22:42
下一篇 2016-06-26 22:44

相关推荐

  • yum源设置

      yum是我们经常用到的一个程序,我们主要用它来安装或删除安装包,下面我来介绍一下怎样配置yum安装源?    配置yum安装源主要有两种方法:一种是用本地镜像做安装源,二是用提供镜像下载的网站做安装源。    一 配置本地安装源    本地安装源需要有系统镜像,操作步骤如下:…

    Linux干货 2016-01-16
  • 第十七周作业

    1、结合图形描述LVS的工作原理; 工作方式: LVS由前端的负载均衡器(Load Balancer,LB)和后端的真实服务器(Real Server,RS)群组成。RS间可通过局域网或广域网连接。LVS的这种结构对用户是透明的,用户只能看见一台作为LB的虚拟服务器(Virtual Server),而看不到提供服务的RS群。当用户的请求发往虚拟服务器,LB根…

    2017-05-27
  • (一)Linux发行版及基础命令简述

    计算机 Linux 发行版 命令 帮助

    2017-12-11
  • 正则表达式详解

    正则表达式详解

    2017-09-20
  • shell中的if else语句与文件查找find浅析

    shell中的if else语句与文件查找find浅析    上篇文章中我们讲述了shell脚本编程的初步入门,其中讲到了shell编程中的顺序执行,顺序执行时一种简单的小脚本,如果在编辑脚本的时候遇到要做出条件判断执行的时候要怎么办呢?我们学习过if之后你会发现这会很简单。if 语句通过关系运算符判断表达式的真假来决定执行哪个分支。 S…

    Linux干货 2016-08-16
  • until 练习题

    一、用until实现下列作业 1、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并提示该用户退出系统。   2、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出 3、编写脚本,求100以内所有正整数之和   4、编写脚本,通过p…

    Linux干货 2016-08-17