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

相关推荐

  • CentOS7常用网络管理命令总结

    一、CentOS7网络接口命名策略(systemd)二、CentOS7的基础网络管理命令2.1 NetworkManager服务2.2 nmtui文本交互式工具2.3 nmcli命令行接口工具nmcli的语法说明NetworkManager整体状态显示显示所有连接或仅活动的连接显示所有设备的状态显示指定设备的所有连接属性添加动态的以太网连接(DHCP)添加静…

    Linux干货 2016-06-09
  • Nginx

    Nginx简介     Nginx(”engine x”)是俄罗斯人Igor Sysoev(伊戈尔.塞索耶夫)编写的一款高性能的HTTP和反向代理服务器。Nginx能够选择高效的epoll、Kqueue、eventport作为网络I/O模型,在高连接并发的情况下,Nginx是Apache服务器不错的替代品,它能够支持高…

    2017-05-07
  • five

    1;显示当前系统上root, fedora或user1用户的默认shell。 #   grep "^\(root\|fedora\|user1\)" /etc/passwd #   grep -E "^(root|fedora|u…

    Linux干货 2017-01-16
  • 马哥教育网络班第21期+第五周课程作业

    1、 显示/boot/grub/grub.conf中以至少一个空白字符开头的行; [root@redhat6 ~]# grep '^[[:space:]]\+' /boot/grub/grub.conf   2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的…

    Linux干货 2016-08-08
  • 基础命令

    2018-03-13
  • LVS-net模型

    net模型拓扑图 注:rip的网关需指向DIP 搭建LVS net模式 基于httpd服务 首先准备三台主机 主机A 主机B 主机C 主机A(vs主机) 主机A设置两个IP 一个内网一个外网 #yum -y install ipvsadm #echo 1 > /proc/sys/net/ipv4/ip_forward :打开核心转发功能 #iptabl…

    Linux干货 2017-05-17