【11.5】MySQL 主从

【11.5】MySQL 主从

  • 17.1 MySQL 主从介绍
  • 17.2 准备工作
  • 17.3 配置主
  • 17.4 配置从
  • 17.5 测试主从同步

17.1 MySQL 主从介绍

  • MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的
  • MySQL主从是基于binlog的,主上须开启binlog才能进行主从。
  • 主从过程大致有 3 个步骤
    1)主将更改操作记录到binlog里
    2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里
    3)从根据relaylog里面的sql语句按顺序执行
    主上有一个log dump线程,用来和从的I/O线程传递binlog
    从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的sql语句落地
    【11.5】MySQL 主从_第1张图片
  • 两个场景:
    1,从作为数据备份
    2,不仅仅备份,主上写,从上读,减轻主的压力

17.2 准备工作

从机器上安装 mysql:

[root@alexis-02 src]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
[root@alexis-02 src]# tar xvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz 
[root@alexis-02 src]# mv mysql-5.6.43-linux-glibc2.12-x86_64/ /usr/local/mysql
[root@alexis-02 mysql]# useradd mysql
[root@alexis-02 mysql]# mkdir /data/
[root@alexis-02 mysql]# mkdir /data/mysql/
[root@alexis-02 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/
[root@alexis-02 mysql]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock    
[root@alexis-02 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@alexis-02 mysql]# vim /etc/init.d/mysqld 
basedir=/usr/local/mysql
datadir=/data/mysql

[root@alexis-02 mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 
[root@alexis-02 mysql]# ps aux|grep mysqld
root       7488  0.1  0.1 113312  1616 pts/0    S    16:56   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/arslinux-02.pid
mysql      7652  8.7 45.1 1302736 449792 pts/0  Sl   16:56   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysql.log --pid-file=/data/mysql/arslinux-02.pid --socket=/tmp/mysql.sock
root       7676  0.0  0.0 112724   984 pts/0    R+   16:57   0:00 grep --color=auto mysqld
[root@alexis-02 mysql]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      6926/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7061/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      7652/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      6926/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      7061/master
[root@alexis-02 mysql]# chkconfig mysqld on
[root@alexis-02 mysql]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关

安装过程参考:https://blog.51cto.com/11530642/2390722

17.3 配置主

  • 修改 my.cnf,增加 server-id(可以根据ip来定),log_bin(logbin的前缀)
[root@alexis-01 mysql]# vim /etc/my.cnf
server-id = 130
log_bin = alexis
  • 重启 mysql
[root@alexis-01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL... SUCCESS!
[root@alexis-01 ~]# ll /data/mysql/
总用量 110748
-rw-rw---- 1 mysql mysql      120 11月   1 17:35 arselinux.000001
-rw-rw---- 1 mysql mysql       19 11月   1 17:35 arselinux.index
-rw-rw---- 1 mysql mysql   128324 11月   1 17:35 arslinux-01.err
-rw-rw---- 1 mysql mysql        5 11月   1 17:35 arslinux-01.pid
-rw-rw---- 1 mysql mysql       56 10月  13 21:55 auto.cnf
-rw-rw---- 1 mysql mysql 12582912 11月   1 17:35 ibdata1
-rw-rw---- 1 mysql mysql 50331648 11月   1 17:35 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 11月  13 21:50 ib_logfile1
drwx------ 2 mysql mysql     4096 10月  21 22:49 mysql
drwx------ 2 mysql mysql     4096 10月  21 22:43 mysql2
drwx------ 2 mysql mysql     4096 10月  13 21:50 performance_schema
drwx------ 2 mysql mysql        6 10月  13 21:50 test
drwx------ 2 mysql mysql      324 10月  28 23:03 zrlog
  • 备份 mysql 库,创建 arslinux 库,将备份导入 arslinux 库
[root@alexis-01 ~]# mysqldump -uroot -parslinux mysql > /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.
[root@alexis-01 ~]# mysql -uroot -parslinux -e'create database arslinux'
Warning: Using a password on the command line interface can be insecure.
[root@alexis-01 ~]# mysql -uroot -parslinux arslinux < /tmp/mysql.sql 
Warning: Using a password on the command line interface can be insecure.
  • 创建用作主从同步的用户
mysql> grant replication slave on *.* to 'repl'@192.168.194.132 identified by '123456';
Query OK, 0 rows affected (0.00 sec)
  • 锁表,防止数据变动
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)
  • 查看主状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| arselinux.000001 |   655378 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  • 备份 /data/mysql 下需要主从同步的数据库
[root@alexis-01 ~]# mysqldump -uroot -parslinux mysql2 > /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.
[root@alexis-01 ~]# mysqldump -uroot -parslinux zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@alexis-01 ~]# mysqldump -uroot -parslinux arslinux > /tmp/arslinux.sql
Warning: Using a password on the command line interface can be insecure.

17.4 配置从

  • 编辑配置 /etc/my.cnf,增加server-id(从不需要bin_log,只有主才需要)
[root@alexis-02 mysql]# vim /etc/my.cnf
server-id = 132
  • 重启 mysql
[root@alexis-02 mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@alexis-02 mysql]# ll /data/mysql/
总用量 110608
-rw-rw---- 1 mysql mysql        5 11月   1 18:19 arslinux-02.pid
-rw-rw---- 1 mysql mysql       56 11月   1 16:56 auto.cnf
-rw-rw---- 1 mysql mysql 12582912 11月   1 18:19 ibdata1
-rw-rw---- 1 mysql mysql 50331648 11月   1 18:19 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 11月   1 16:51 ib_logfile1
drwx------ 2 mysql mysql     4096 11月   1 16:51 mysql
drwx------ 2 mysql mysql     4096 11月   1 16:51 performance_schema
drwx------ 2 mysql mysql        6 11月   1 16:48 test
  • 传输数据,将主上库备份传到从上
[root@alexis-02 mysql]# scp 192.168.194.130:/tmp/*.sql /tmp/
arslinux.sql                                                                         100%  638KB  14.6MB/s   00:00    
mysql2.sql                                                                           100%  632KB  14.2MB/s   00:00    
mysql.sql                                                                            100%  638KB  13.6MB/s   00:00    
zrlog.sql                                                                            100%   10KB   2.2MB/s   00:00
  • 给 mysql 和 mysqldump 做 alias(因为没加入 PATH )
[root@alexis-02 mysql]# alias 'mysql=/usr/local/mysql/bin/mysql'
[root@alexis-02 mysql]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'
  • 创建库
[root@alexis-02 mysql]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> create database arslinux;
Query OK, 1 row affected (0.00 sec)

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

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

mysql> quit;
Bye
  • 恢复数据到数据库
[root@alexis-02 mysql]# mysql -uroot arslinux < /tmp/arslinux.sql 
[root@alexis-02 mysql]# mysql -uroot mysql2 < /tmp/mysql2.sql 
[root@alexis-02 mysql]# mysql -uroot zrlog < /tmp/zrlog.sql 
[root@alexis-02 mysql]# ll /data/mysql/
总用量 226264
drwx------ 2 mysql mysql     4096 11月   1 18:28 arslinux
-rw-rw---- 1 mysql mysql        5 11月   1 18:19 arslinux-02.pid
-rw-rw---- 1 mysql mysql       56 11月   1 16:56 auto.cnf
-rw-rw---- 1 mysql mysql 79691776 11月   1 18:29 ibdata1
-rw-rw---- 1 mysql mysql 50331648 11月   1 18:29 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 11月   1 16:51 ib_logfile1
drwx------ 2 mysql mysql     4096 11月   1 16:51 mysql
drwx------ 2 mysql mysql     4096 11月   1 18:28 mysql2
drwx------ 2 mysql mysql     4096 11月   1 16:51 performance_schema
drwx------ 2 mysql mysql        6 11月   1 16:48 test
drwx------ 2 mysql mysql      324 11月   1 18:29 zrlog
  • 实现主从同步
[root@alexis-02 mysql]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='192.168.194.130',master_user='repl',master_password='123456',master_log_file='arselinux.000001',master_log_pos=655378;
Query OK, 0 rows affected, 2 warnings (0.04 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.194.130
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: arselinux.000001
          Read_Master_Log_Pos: 655378
               Relay_Log_File: arslinux-02-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: arselinux.000001
             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: 655378
              Relay_Log_Space: 462
              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: c164593f-7586-11e9-a13a-000c2924eaf2
             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)

★★★ status中 Slave_IO_Running 和 Slave_SQL_Running 如果两个都是 Yes 表示配置主从成功
还需关注以下参数
Seconds_Behind_Master: 0 //为主从延迟的时间
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:

  • 到主上将表恢复写操作
[root@alexis-01 ~]# mysql -uroot -parslinux
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 15
Server version: 5.6.43-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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 tables;
Query OK, 0 rows affected (0.00 sec)
  • 错误汇总:
    1,Slave_IO_Running: No
    查看错误问题:
    Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

查看主从服务器两台机器 /data/mysql/auto.cnf 的UUID是否一样,如果一样,删除其中一个,并重启该服务器,再次进行从服务器 mysql 的 change master 操作就能成功了。

17.5 测试主从同步

  • 在 my.cnf 中定义:可以在主上,也可以在从上

  • 主服务器上
    binlog-do-db= //仅同步指定的库
    binlog-ignore-db= //忽略指定库

  • 从服务器上
    replicate_do_db=
    replicate_ignore_db=
    replicate_do_table=
    replicate_ignore_table=
    replicate_wild_do_table= //如arslinux.%, 支持通配符%
    replicate_wild_ignore_table=
    最常用是最后两条

  • 验证是否同步
    主:

mysql> select count(*) user;
+------+
| user |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

从:

mysql> select count(*) user;
+------+
| user |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

主:

mysql> truncate table user;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from user;
Empty set (0.00 sec)

从:

mysql> select * from user;
Empty set (0.01 sec)
  • 主上直接删除表
    主:
mysql> drop table user;
Query OK, 0 rows affected (0.00 sec)

从:

mysql> select * from user;
ERROR 1146 (42S02): Table 'arslinux.user' doesn't exist

如果主从出现错误,可以尝试从上 stop slave,再 start slave,如果还是有问题,那么就要重新做主从了

  • 测试主从:
    主上 mysql -uroot arslinux
    select count() from db;
    truncate table db;
    到从上 mysql -uroot arslinux
    select count(
    ) from db;
    主上继续 drop table db;
    从上查看 db 表

有人遇到主从不能正常同步,提示 uuid 相同的错误。这是因为克隆机器导致。
https://www.2cto.com/database/201412/364479.html

你可能感兴趣的:(学习笔记)