Linux基础(day61)

17.1 MySQL主从介绍

MySQL主从介绍


  • MySQL主从又叫作Replication、AB复制。简单讲就是A和B两台机器作主从后,在A上写数据,另一台B也会跟着写数据,二者数据实时同步的
  • MySQL主从是基于binlog的,主上须开启binlog才能进行主从。
    • binlog,其实就是一个文件,文件里记录了一些日志,文件是 二进制文件,没法cat
  • 主从过程大体有3个步骤
    • 1)主将更改操做记录到binlog里
    • 2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里
      • relaylog,中文叫作 中继日志
    • 3)从根据relaylog里面的sql语句按顺序执行

  • mysql主从共有三个线程
    • 主上有一个log dump线程,用来和从的I/O线程传递binlog
    • 从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另一个SQL线程用来把relaylog里面的sql语句落地

MySQL主从原理图


输入图片说明


mysql主从使用场景:


  1. 数据备份,主机器宕机,从机器还能随时对web提供服务
  2. 做为一个从库,读的库,减轻主库的压力,数据备份且能够分担主机器被调用数据时的压力,mysql主从,是有方向性的,写数据,必须从主机器开始;若是不依照原理会致使数据紊乱

17.2 准备工做

mysql安装总结

  • mysql主从准备工做:
    • 准备两台机器,每台机器安装msyql服务,并启动mysql服务
  • mysql详细安装

  1. 首先下载二进制免编译的包,下载到/usr/local/src/目录下
  2. 解压压缩包
  3. 解压完以后,把解压出来的目录放到 /usr/local/mysql/ 目录下
  • 注意点:
    • 首先检查 /usr/local/mysql/ 目录是否存在
    • 如果这个目录存在,首先把这个目录改个名字,或者把目录下的内容删除
    • 而后把解压出来的目录放到 /usr/local/mysql/ 目录下面
  • 目录内容应该以下
[root@hanfeng ~]# ls /usr/local/mysql
bin      data  include  man     my-new.cnf  README   share      support-files
COPYING  docs  lib      my.cnf  mysql-test  scripts  sql-bench
[root@hanfeng ~]#
  1. 而后切换到 /usr/local/mysql/ 目录下,进行初始化 命令
  • 初始化命令 ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
    • 注意点:
      • 其中的--user=mysql 须要提早建立
  1. 初始化成功的标志就是两个OK,或者用 echo $? 检查是否初始化成功
  2. 编辑 /etc/my.cnf 文件——>默认是自带 my.cnf 文件的
在 /etc/my.cnf 文件中
定义 datadir=/data/mysql     
定义 socket=/tmp/mysql.sock
  1. 拷贝启动脚本
  • 命令 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
  1. 编辑启动脚本
vim /etc/init.d/mysqld //对如下两行进行指定路径

指定basedir的路径 /usr/local/mysql  
指定datadir的路径 /data/mysql
  1. 以后就能够启动mysql了
  • 命令 /etc/init.d/mysql start
  1. 若是启动失败,能够去查看错误日志
  2. 建议 :
  • 查看 /data/mysql 目录下的文件,默认属主、属组,若是不是mysql的,启动时会因没法写入数据而不能启动mysql
    • 改变属主和属组,命令 chomd mysql:mysql /data/mysql
  • 而后就能够尝试启动,命令 /etc/init.d/mysql start
  1. 若想开机启动,只须要输入命令
  • 命令 chkconfig mysqld on

17.3 配置主

主从配置 - 主上操做

  • 安装mysql
  • 修改my.cnf,增长server-id=130和log_bin=aminglinux1
  • 修改完配置文件后,启动或者重启mysqld服务
  • 把mysql库备份并恢复成aming库,做为测试数据
  • mysqldump -uroot mysql > /tmp/mysql.sql
  • mysql -uroot -e “create database aming”
  • mysql -uroot aming < /tmp/mysql.sql
  • 建立用做同步数据的用户
  • grant replication slave on . to 'repl'@slave_ip identified by 'password';
  • flush tables with read lock;
  • show master status;

主从配置 - 主上操做

  1. 在两台机器安装并启动mysql服务后,首先在主上进行操做
  2. 修改/etc/mys.cnf配置文件
  • 在配置文件下[mysqld]下添加
    • server-id=130 这个id能够自定义,这里根据ip来定义
    • log_bin=hf123 打开binlog,名字自定义为log_bin=hf123 最终以下
[root@hanfeng ~]# vim /etc/my.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]
server_id = 130
log_bin=hf123
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

保存退出
  1. 更改完配置文件后,须要重启mysql
  • /etc/init.d/mysqld restart
[root@hanfeng ~]# /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@hanfeng ~]#
  1. 这时候来/data/mysql/目录下,会生成一些文件
  • ls -lt /data/mysql/
[root@hanfeng ~]# ls -lt /data/mysql/
总用量 110728
-rw-rw----. 1 mysql mysql 50331648 1月  19 23:35 ib_logfile0
-rw-rw----. 1 mysql mysql 12582912 1月  19 23:35 ibdata1
-rw-rw----. 1 mysql mysql   104317 1月  19 23:35 hanfeng.err
-rw-rw----. 1 mysql mysql        5 1月  19 23:35 hanfeng.pid
-rw-rw----. 1 mysql mysql       15 1月  19 23:35 hf123.index
-rw-rw----. 1 mysql mysql      120 1月  19 23:35 hf123.000001
drwx------. 2 mysql mysql     4096 1月  19 20:58 zrlog
-rw-rw----. 1 mysql mysql       56 1月   2 22:41 auto.cnf
drwx------. 2 mysql mysql     4096 1月   2 22:28 mysql
drwx------. 2 mysql mysql     4096 1月   2 22:28 performance_schema
-rw-rw----. 1 mysql mysql 50331648 1月   2 22:28 ib_logfile1
drwx------. 2 mysql mysql        6 1月   2 22:26 test
[root@hanfeng ~]#
  • 其中 .index 索引页,这个文件是必需要有的
  • 其中 .000001 这个是二进制日志文件,会持续生成二、三、4等等(这个文件是实现主从配置的根本,没有这个文件根本没有办法完成主从)
  1. 测试,准备一个数据作演示用的
  2. 首先作一个备份
  • mysqldump -uroot -phanfeng zrlog > /tmp/zrlog.sql
[root@hanfeng ~]# mysqldump -uroot -phanfeng zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]#
  1. 而后建立一个新的库
  • mysql -uroot -phanfeng -e "create database han"
[root@hanfeng ~]# mysql -uroot -phanfeng -e "create database han"
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]#
  1. 建立好库后,还须要把数据恢复一下,那也就是说作的主从,参考的对象就是 han 这个库
  • mysql -uroot -phanfeng han < /tmp/zrlog.sql
[root@hanfeng ~]# mysql -uroot -phanfeng han < /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]#
  1. 再来查看/data/mysql/目录下的文件
  • ls -lt /data/mysql/
[root@hanfeng ~]# ls -lt /data/mysql/
总用量 110740
-rw-rw----. 1 mysql mysql 50331648 1月  19 23:40 ib_logfile0
-rw-rw----. 1 mysql mysql 12582912 1月  19 23:40 ibdata1
-rw-rw----. 1 mysql mysql    10337 1月  19 23:40 hf123.000001
drwx------. 2 mysql mysql     4096 1月  19 23:40 han
-rw-rw----. 1 mysql mysql   104317 1月  19 23:35 hanfeng.err
-rw-rw----. 1 mysql mysql        5 1月  19 23:35 hanfeng.pid
-rw-rw----. 1 mysql mysql       15 1月  19 23:35 hf123.index
drwx------. 2 mysql mysql     4096 1月  19 20:58 zrlog
-rw-rw----. 1 mysql mysql       56 1月   2 22:41 auto.cnf
drwx------. 2 mysql mysql     4096 1月   2 22:28 mysql
drwx------. 2 mysql mysql     4096 1月   2 22:28 performance_schema
-rw-rw----. 1 mysql mysql 50331648 1月   2 22:28 ib_logfile1
drwx------. 2 mysql mysql        6 1月   2 22:26 test
[root@hanfeng ~]#
  1. 能看到hf123.000001二进制文件是有增长的,hf123.000001增加的大小是和zrlog这个库的保持一致的,hf123.000001文件里完整的记录了数据库的建立的库,建立的表,以及表里的内容全都有
  2. 下面建立用于主从相互同步数据的用户
  3. 先进入到mysql里面去
  • mysql -uroot -phanfeng
[root@hanfeng ~]# mysql -uroot -phanfeng
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 194
Server version: 5.6.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
  1. 建立用户
  • grant replication slave on . to 'repl'@'192.168.202.131' identified by 'hanfeng';
    • 指定权限,replication slave权限
    • 针对repl这个用户
    • 针对 从 的那个IP,指定来源(如果写全部的IP会很危险)
mysql> grant replication slave on *.* to 'repl'@'192.168.202.131' identified by 'hanfeng';
Query OK, 0 rows affected (0.00 sec)

mysql>
  1. 锁定表,目的是不让表继续写,由于一会须要作 从 机器配置,须要进行一个同步,让两台机器同步,保证两台机器的数据一致,同步才不会出错
  • flush tables with read lock;
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql>
  1. 查看一下binlog的文件和大小,并记住binlog的filename
  • show master status;
mysql> show master status;
+--------------+----------+--------------+------------------+-------------------+
| File         | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------+----------+--------------+------------------+-------------------+
| hf123.000001 |    10549 |              |                  |                   |
+--------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> quit
Bye
  1. 而后退出数据库,作一个数据同步
  2. 查看/data/mysql/下有哪些库,主上有哪些库,一会从上也得有哪些库,同步这些库,就意味着这些数据都得备份过去
[root@hanfeng ~]# ls /data/mysql
auto.cnf  hanfeng.err  hf123.000001  ibdata1      ib_logfile1  performance_schema  zrlog
han       hanfeng.pid  hf123.index   ib_logfile0  mysql        test
[root@hanfeng ~]#
  1. 备份数据库,除了mysql库,由于mysql库里面有帐号密码,从上的时候不可能把全部权限复制过去,因此mysql不须要备份
  • 备份其余的库
[root@hanfeng ~]# mysqldump -uroot -phanfeng test > /tmp/test.sql
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]#
  1. 等会把/tmp/目录下 .sql文件都拷贝到 从上 去
[root@hanfeng ~]# ls /tmp/*sql
/tmp/test.sql  /tmp/zrlog.sql
[root@hanfeng ~]#
  1. 主上操做完成,接下来从上操做

17.4 配置从

主从配置 - 从上操做

  • 安装mysql
  • 查看my.cnf,配置server-id=132,要求和主不同
  • 修改完配置文件后,启动或者重启mysqld服务
  • 把主上aming库同步到从上
  • 能够先建立aming库,而后把主上的/tmp/mysql.sql拷贝到从上,而后导入aming库
  • mysql -uroot
  • stop slave;
  • change master to master_host='', master_user='repl', master_password='', master_log_file='', master_log_pos=xx,
  • start slave;
  • 还要到主上执行 unlock tables

主从配置 - 从上操做

  • 在从上机器配置
  1. 首先在从上安装并启动mysql,而后查看my.cnf,配置server-id=131,要求和主不同,在配置文件的 log_bin参数 就不须要配置的,由于只有 主上 才须要二进制日志文件
[root@hf-01 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=131
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
 # log-error=/var/log/mariadb/mariadb.log
 # pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
# !includedir /etc/my.cnf.d
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M
保存退出
  1. 重启mysql服务
[root@hf-01 ~]#  /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL... SUCCESS! 
[root@hf-01 ~]#
  1. 在增长 server-id 后,对于mysql 是没有任何变化的
[root@hf-01 ~]# ls /data/mysql
aria_log.00000001  hf-01.err       ibdata1      localhost.localdomain.err  performance_schema
aria_log_control   hf-01.pid       ib_logfile0  multi-master.info          test
auto.cnf           ib_buffer_pool  ib_logfile1  mysql
  1. 把主机器上备份的 .sql 数据,拷贝到从机器上,而后作一个数据恢复
  • scp 192.168.202.130:/tmp/*.sql /tmp/
  • 拷贝数据失败
    • 缘由:
      • 在拷贝的数据的时候,一直拷贝数据失败,由于在主上的机器里有开机启动脚本,因此致使拷贝数据失败
[root@hf-01 ~]# scp 192.168.202.130:/tmp/*.sql /tmp/
root@192.168.74.129's password: 
                                  _oo0oo_
[root@hf-01 ~]#
- 解决方法:
    - 将主机器的里的开机启动脚本关闭vi .bashrc配置文件文件中注释掉脚本,再来拷贝数据到从机器上,会发现成功
[root@hf-01 ~]# scp 192.168.202.130:/tmp/*.sql /tmp/
The authenticity of host '192.168.202.130 (192.168.202.130)' can't be established.
ECDSA key fingerprint is 25:ce:a9:96:36:88:84:ab:4f:7e:80:a3:e7:36:2c:9f.
Are you sure you want to continue connecting (yes/no)? yes\
Warning: Permanently added '192.168.202.130' (ECDSA) to the list of known hosts.
root@192.168.202.130's password: 
test.sql                                                      100% 1258     1.2KB/s   00:00    
zrlog.sql                                                     100%   10KB   9.8KB/s   00:00    
[root@hf-01 ~]#
  1. 进入到从上数据库中——>由于从上没设置密码,因此能够直接进去
[root@hf-01 ~]# mysql -uroot
-bash: mysql: 未找到命令
[root@hf-01 ~]#
  • mysql -uroot命令不存在,是由于没有加入到PATH里面
    • 这里作个alias别名设置,用单引号' '
[root@hf-01 ~]# alias 'mysql=/usr/local/mysql/bin/mysql'
[root@hf-01 ~]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'
  • 这时候就能够进入到mysql里面去了
[root@hf-01 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
  1. 建立库
  • create database zrlog;
  • create database test1;
mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)

mysql> create database test1;
Query OK, 1 row affected (0.00 sec)

mysql> quit
Bye

退出数据库
  1. 而后将数据库作一个恢复
  • mysql -uroot test < /tmp/test.sql
[root@hf-01 ~]# mysql -uroot test < /tmp/test.sql
[root@hf-01 ~]# mysql -uroot zrlog < /tmp/zrlog.sql
[root@hf-01 ~]#
  • 保证两边数据一致
  1. 而后查看/data/mysql/目录下的数据是否和主机器上的/data/mysql/目录是否一致
  2. 开始实现主从
  3. 在从机器登陆到mysql
[root@hf-01 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
  1. 而后在数据库里面执行命令,中止slave
  • stop slave;
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>
  1. 配置主机器相关配置
  • change master to master_host='192.168.202.130', master_user='repl', master_password='hanfeng', master_log_file='hf123.000001', master_log_pos=10549;
    • master_host='192.168.202.130',指定主机器host
    • master_user='repl',指定主机器用户
    • master_password='hanfeng',指定主机器密码
    • master_log_file='hanfeng1.000001',指定binlog文件名
    • master_log_pos=10549,指定binlog文件大小
  • 也能够指定主机器的port,由于在生产环境中,也会有人更改mysql的默认端口 master_port=3306
mysql> change master to master_host='192.168.202.130', master_user='repl', master_password='hanfeng', master_log_file='hf123.000001', master_log_pos=10549;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
  1. 开始slave
  • start slave;
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql>
  1. 这时候经过 show slave status\G 判断主从是否配置成功

查看slave配置html

  • show slave status\G
    • 在G后面不须要加分号,\G自己就是一种结束符
    • 看 Slave_IO_Running: Yes 是否为yes
    • 看 Slave_SQL_Running: Yes 是否为yes
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.202.130
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: hf123.000001
          Read_Master_Log_Pos: 10549
               Relay_Log_File: hf-01-relay-bin.000002
                Relay_Log_Pos: 279
        Relay_Master_Log_File: hf123.000001
             Slave_IO_Running: Yes            //检查是否为Yes
            Slave_SQL_Running: Yes         //检查是否为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: 10549
              Relay_Log_Space: 452
              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: 130
                  Master_UUID: fd146e3b-efca-11e7-b3d6-000c29ff458f
             Master_Info_File: /data/mysql/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)

mysql>
  1. 解锁“主”上的表(在主上机器操做)
  • unlock tables;
[root@hf-01 mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.6.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> unlock table;
Query OK, 0 rows affected (0.00 sec)
  1. 到这里主从搭建就算完成了

查看主从同步是否正常

  • 从上执行mysql -uroot
  • show slave stauts\G
  • 看是否有
  • Slave_IO_Running: Yes
  • Slave_SQL_Running: Yes
  • 还需关注
  • Seconds_Behind_Master: 0 //为主从延迟的时间
  • Last_IO_Errno: 0
  • Last_IO_Error:
  • Last_SQL_Errno: 0
  • Last_SQL_Error:

17.5 测试主从同步

扩展

相关文章
相关标签/搜索