第二十七课 Mysql主从配置

17.1 MySQL主从介绍

1、主从复制简介

MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做做主从后,在A上写数据,另一台B也会跟着写数据,两者数据实时同步。 MySQL主从复制就是将一个MySQL实例(Master)中的数据实时复制到另一个MySQL实例(slave)中,而且这个复制是一个异步复制的过程。

实现整个复制操作主要由三个进程完成的,其中两个进程在Slave(sql_thread和IO_thread),另外一个进程在 Master(IO进程)上。

2、主从复制原理、机制

要实施复制,首先必须打开Master端的binary log(bin-log)功能,否则无法实现。因为整个复制过程实际上就是Slave从Master端获取该日志然后再在自己身上完全顺序的执行日志中所记录的各种操作。

复制的基本过程如下:

1)、Slave上面的IO_thread连接上Master,并请求从指定日志文件的指定位置(或者从最开始的日志)之后的日志内容;

2)、Master接收到来自Slave的IO_thread的请求后,通过负责复制的IO进程根据请求信息读取制定日志指定位置之后的日志信息,返回给Slave 的IO_thread。返回信息中除了日志所包含的信息之外,还包括本次返回的信息已经到Master端的bin-log file的以及bin-log pos;

3)、Slave的IO_thread接收到信息后,将接收到的日志内容依次添加到Slave端的relay-log文件的最末端,并将读取到的Master端的 bin-log的文件名和位置记录到master-info文件中,以便在下一次读取的时候能够清楚的告诉Master“我需要从某个bin-log的哪 个位置开始往后的日志内容,请发给我”;

4)、Slave的Sql_thread检测到relay-log中新增加了内容后,会马上解析relay-log的内容成为在Master端真实执行时候的那些可执行的内容,并在本数据库中执行。

2主要应用的场景

1.数据备份 2.做为读的库使用,减轻主库的压力

17.2 准备工作

两台机器都装上mysql,并且都开启mysql的服务

17.3 配置主

1.修改my.cnf配置文件

vi /etc/my.cnf
在socket=/tmp/mysql.sock下面增加配置内容:

server-id=100         
log_bin=aminglinux1  
/etc/init.d/mysqld restart  //更改完配置重启mysql
Shutting down MySQL.... SUCCESS! 
Starting MySQL.. SUCCESS! 
cd /data/mysql/
 ls -lt          //重启完成后目录下有生成新的文件

2.准备一个数据库做演示使用

mysqldump -uroot -pszyino-123 zrlog > /tmp/zrlog.sql    //备份之前旧的数据库zrlog
du -sh /tmp/zrlog.sql   //查看数据库大小
12K /tmp/zrlog.sql
mysql -uroot -pszyino-123 -e "create database aming"   //创建一个新的数据库
mysql -uroot -pszyino-123 aming < /tmp/zrlog.sql     //恢复之前备份的zrlog库

3.创建用作主从相互同步数据的用户

mysql -uroot -pszyino-123
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 1535
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> grant replication slave on *.* to 'repl'@'172.16.111.110' identified by 'szyino-123';  //创建同步账户,这里的repl是为从(slave)端设置的访问主(master)端的用户,也就是要完成主从复制的用户,密码为szyino-123,这里的127.16.111.110为slave(从)的ip。
Query OK, 0 rows affected (0.00 sec)

mysql> flush tables with read lock;  //锁定数据库写操作
Query OK, 0 rows affected (0.02 sec)

mysql> show master status; //查看master的状态,这些数据是要记录的,一会在slave端用到。
+--------------------+----------+--------------+------------------+-------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+--------------+------------------+-------------------+
| aminglinux1.000001 |    12777 |              |                  |                   |
+--------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> quit
Bye
ls   //为了更加与生产环境一样,把所有的库都做一个备份,包括aming\mysql2\zrlog,mysql库不需要同步。
aming               aminglinux1.index  gary-tao.err  ibdata1      ib_logfile1  mysql2              test
aminglinux1.000001  auto.cnf           gary-tao.pid  ib_logfile0  mysql        performance_schema  zrlog
ls /tmp/zrlog.sql 
/tmp/zrlog.sql
mysqldump -uroot -pszyino-123 mysql2 > /tmp/my2.sql
Warning: Using a password on the command line interface can be insecure.
ls /tmp/*sql
/tmp/my2.sql   /tmp/zrlog.sql

17.4 配置从

1.修改slave的配置文件my.cnf

vi /etc/my.cnf

增加配置如下内容:

server-id=110   //设置成和master(主)不一样的数字,若一样会导致后面的操作不成功
 /etc/init.d/mysqld restart //重启MysqL
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS!

2.把主上库同步到从上

scp 172.16.111.100:/tmp/*.sql /tmp/
The authenticity of host '172.16.111.100 (172.16.111.100)' can't be established.
ECDSA key fingerprint is 89:19:99:8c:63:ff:d9:e6:19:0d:81:03:27:54:49:78.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.111.100' (ECDSA) to the list of known hosts.
root@172.16.111.100's password: 
Permission denied, please try again.
root@172.16.111.100's password: 
my2.sql                                                                                           100%  638KB 637.7KB/s   00:00    
zrlog.sql   


mysql -uroot
-bash: mysql: 未找到命令
alias 'mysql=/usr/local/mysql/bin/mysql'
alias 'mysqldump=/usr/local/mysql/bin/mysqldump'
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> create database aming;
Query OK, 1 row affected (0.00 sec)

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

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

mysql> quit
Bye

3.恢复数据

mysql -uroot zrlog < /tmp/zrlog.sql 
mysql -uroot aming < /tmp/zrlog.sql 
mysql -uroot mysql2 < /tmp/my2.sql

4.实现主从

mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
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> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='172.16.111.100', master_user='repl', master_password='szyino-123', master_log_file='aminglinux1.000001', master_log_pos=12777;
Query OK, 0 rows affected, 2 warnings (0.02 sec) \\解释说明:
change master这个命令是一条,打完逗号后可以按回车,直到你打分号才算结束,其中,master_log_file和master_log_pos是在前面使用show master status命令查到的数据。

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

mysql> show slave status\G     //查看主从是否配置成功
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.111.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: aminglinux1.000001
          Read_Master_Log_Pos: 12777
               Relay_Log_File: gary-relay-bin.000002
                Relay_Log_Pos: 285
        Relay_Master_Log_File: aminglinux1.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: 12777
              Relay_Log_Space: 457
              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: 100
                  Master_UUID: ccffc787-eafa-11e7-9474-000c2909e558
             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)
\\看是否有,两个Yes说明配置成功。
 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:
 如果主从不正常了,需要看这里的error信息。

5.到主上机器开启数据库写操作

mysql -uroot -pszyino-123
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 2407
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 tables;  //执行开启数据写操作命令
Query OK, 0 rows affected (0.00 sec)

17.5 测试主从同步

1.说明几个配置参数

主服务器上,这两个参数用一个就可以了。

binlog-do-db=       //定义需要同步指定的库,多个库用英文的逗号分隔。
binlog-ignore-db=   //定义不需要同步的库。

从服务器上

replicate_do_db=      //定义需要同步指定的库,多个库用英文的逗号分隔
replicate_ignore_db=  //定义不需要同步的库。
replicate_do_table=   //定义需要同步的表
replicate_ignore_table=  //定义不需要同步的表
replicate_wild_do_table=   //如aming库.%, 支持通配符%(主要使用)
replicate_wild_ignore_table=

2.测试主从同步

mysql -uroot -pszyino-123  
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 2407
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 tables;
Query OK, 0 rows affected (0.00 sec)

mysql> use aming;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-----------------+
| Tables_in_aming |
+-----------------+
| comment         |
| link            |
| log             |
| lognav          |
| plugin          |
| tag             |
| type            |
| user            |
| website         |
+-----------------+
9 rows in set (0.00 sec)

//进入主上查看website表行数
mysql> select count(*) website;  
+---------+
| website |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

mysql> truncate table website;
Query OK, 0 rows affected (0.03 sec)
\\当你仍要保留该表,但要删除所有记录时, 用 truncate
//进入从上查看user表行数
mysql> use aming;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

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

//清空主上的website表内容
mysql> truncate table website;
Query OK, 0 rows affected (0.03 sec)

//再次查看主上wedsite表虽然显示一行,但是内容已为空。
mysql> select count(*) website;
+---------+
| website |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

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

//查看从上的wedsite内容已为空。
mysql> select count(*) from website;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

//在主上删除website这个表
mysql> drop table website;
Query OK, 0 rows affected (0.02 sec)

//在从上查看websitep这个表已被删除
mysql> select * from webiste;
ERROR 1146 (42S02): Table 'aming.webiste' doesn't exist

注意:如果不小心在从上删除了表,你再次打开查看主从配置时,发现报错了,如下:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.111.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: aminglinux1.000001
          Read_Master_Log_Pos: 12997
               Relay_Log_File: gary-relay-bin.000002
                Relay_Log_Pos: 383
        Relay_Master_Log_File: aminglinux1.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: No   //这里变NO了
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1051
                   Last_Error: Error 'Unknown table 'aming.website'' on query. Default database: 'aming'. Query: 'DROP TABLE `website` /* generated by server */'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 12875
              Relay_Log_Space: 677
              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: 1051
               Last_SQL_Error: Error 'Unknown table 'aming.website'' on query. Default database: 'aming'. Query: 'DROP TABLE `website` /* generated by server */'
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 100
                  Master_UUID: ccffc787-eafa-11e7-9474-000c2909e558
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 180123 15:28:37
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

如果出现了这个情况只能重新做主从:

主上操作查看master:

mysql> show master status;
+--------------------+----------+--------------+------------------+-------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+--------------+------------------+-------------------+
| aminglinux1.000001 |    12997 |              |                  |                   |
+--------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

从上配置change master重做主从(为什么可以直接做主从,因为数据还是一致的,没有做其它的操作)

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

//除了最后面数字需要更改,其它参数都一样。
mysql> change master to master_host='172.16.111.100', master_user='repl', master_password='szyino-123', master_log_file='aminglinux1.000001', master_log_pos=12997;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

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

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.111.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: aminglinux1.000001
          Read_Master_Log_Pos: 12997
               Relay_Log_File: gary-relay-bin.000002
                Relay_Log_Pos: 285
        Relay_Master_Log_File: aminglinux1.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: 12997
              Relay_Log_Space: 457
              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: 100
                  Master_UUID: ccffc787-eafa-11e7-9474-000c2909e558
             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)

扩展

有的同学,遇到主从不能正常同步,提示uuid相同的错误。这是因为克隆机器导致。
https://www.2cto.com/database/201412/364479.html
扩展部分
不停库不锁表在线主从配置 http://seanlook.com/2015/12/14/mysql-replicas/
mysql主从常见问题 http://www.10tiao.com/html/706/201603/403220961/1.html
mysql主从延迟 http://f.dataguru.cn/thread-461916-1-1.html 
深入探究主从延迟 http://ningg.top/inside-mysql-master-slave-delay/
mysql主从不同步如何做 http://www.jb51.net/article/33052.htm
mysql 主主 http://www.cnblogs.com/ygqygq2/p/6045279.html
mysql-proxy 实现读写分离 http://my.oschina.net/barter/blog/93354 
mycat实现读写分离 http://www.th7.cn/db/mysql/201708/250280.shtml
atlas相关 http://www.oschina.net/p/atlas
mysql一主多从 http://blog.sina.com.cn/s/blog_4c197d4201017qjs.html
mysql环形主从 http://ask.apelearn.com/question/11437
cobar实现分库分表 http://blog.csdn.net/huoyunshen88/article/details/37927553
mysql分库分表方案 http://my.oschina.net/ydsakyclguozi/blog/199498
mysql架构演变 http://www.aminglinux.com/bbs/thread-8025-1-1.htmlf
MHA架构 http://www.dataguru.cn/thread-457284-1-1.html
比较复杂的mysql集群架构 http://ask.apelearn.com/question/17026
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值