Name | Version |
---|---|
CentOS 7 | 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux |
MySQL | 14.14 Distrib 5.6.35, for linux-glibc2.5 (x86_64) using EditLine wrapper |
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语句落地
主从特性:不能在从上写,主从有它的方向性,只能主上写,否则就会紊乱。
应用场景:
1、备份主上的数据,主宕机了即可启用从。
2、减轻主的压力,在从上读数据。
准备工作
mysql开机启动:chkconfig mysqld on
修改my.cnf,增加server-id=130和log_bin=zyshanlinux1
修改完配置文件后,启动或者重启mysqld服务
[root@zyshanlinux-001 src]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=128
log_bin=zyshanlinux1
# Disabling symbolic-links is recommended to prevent assorted security risks
...
[root@zyshanlinux-001 src]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@zyshanlinux-001 src]# pwd
/usr/local/src
[root@zyshanlinux-001 src]# cd /data/mysql
[root@zyshanlinux-001 mysql]# ls -lt
total 110724
-rw-rw---- 1 mysql mysql 50331648 Jul 19 23:24 ib_logfile0
-rw-rw---- 1 mysql mysql 12582912 Jul 19 23:24 ibdata1
-rw-rw---- 1 mysql mysql 105453 Jul 19 23:24 zyshanlinux-001.err
-rw-rw---- 1 mysql mysql 6 Jul 19 23:24 zyshanlinux-001.pid
-rw-rw---- 1 mysql mysql 22 Jul 19 23:24 zyshanlinux1.index
-rw-rw---- 1 mysql mysql 120 Jul 19 23:24 zyshanlinux1.000001
drwx------ 2 mysql mysql 324 Jul 18 00:21 zrlog
drwx------ 2 mysql mysql 4096 Jul 10 22:46 mysql2
-rw-rw---- 1 mysql mysql 56 Jul 3 21:38 auto.cnf
drwx------ 2 mysql mysql 4096 Jul 3 21:31 mysql
drwx------ 2 mysql mysql 4096 Jul 3 21:31 performance_schema
-rw-rw---- 1 mysql mysql 50331648 Jul 3 21:31 ib_logfile1
drwx------ 2 mysql mysql 6 Jul 3 21:31 test
把mysql库备份并恢复成zyshan库,作为测试数据
[root@zyshanlinux-001 mysql]# mysqldump -uroot -pmysql01 zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@zyshanlinux-001 mysql]# du -sh /tmp/zrlog.sql
12K /tmp/zrlog.sql
[root@zyshanlinux-001 mysql]# mysql -uroot -pmysql01 -e "create database zyshan"
Warning: Using a password on the command line interface can be insecure.
[root@zyshanlinux-001 mysql]# mysql -uroot -pmysql01 zyshan < /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
以上的数据库操作都已经记录到binlog里面去了,多出来了2条,只要binlog二进制数据完整,也可以用binlog恢复数据库的,完全没问题。
创建用作同步数据的用户
进入数据库mysql
[root@zyshanlinux-001 mysql]# mysql -uroot -pmysql01
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 11
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.
创建用户repl,目标是从机的IP,赋予的权限replication slave,所有的库所有的表*.*,密码可以复杂点zyshanlinux111。
mysql> grant replication slave on *.* to 'repl'@'192.168.106.130' identified by 'zyshanlinux111';
Query OK, 0 rows affected (0.00 sec)
mysql>
锁表,是为了让表不继续写了。保持这个状态,便于从以同等的状态开始主从同步。
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
记住File和Position,(即位置和binlog的文件名。)
mysql> show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| zyshanlinux1.000003 | 671072 | | | |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> quit
Bye
为了跟生产环境一样,尽可能把红线的库都做备份并同步;mysql不用同步,因为mysql里面存在很多用户密码,从机上不可能全部复制过去的。测试就选2个做同步备份就好:等下要把这2个mysql2.sql和zrlog.sql拷贝到从上去。
[root@zyshanlinux-001 mysql]# ls
auto.cnf ib_logfile0 mysql performance_schema zrlog zyshanlinux-001.err zyshanlinux1.000001 zyshanlinux1.000003
ibdata1 ib_logfile1 mysql2 test zyshan zyshanlinux-001.pid zyshanlinux1.000002 zyshanlinux1.index
[root@zyshanlinux-001 mysql]# ls /tmp/*sql
/tmp/mysql2.sql /tmp/zrlog.sql
配置从
查看my.cnf,配置server-id=130(和从机的IP一样),要求和主不一样,从是不用二进制文件日志文件的。
[root@zyshanlinux-02 ~]# vim /etc/my.cnf
修改完配置文件后,启动或者重启mysqld服务
把主上zyshan库同步到从上, 可以先创建zyshan库,然后把主上的/tmp/zrlog.sql拷贝到从上,然后导入zysahn库
[root@zyshanlinux-02 ~]# ls /data/mysql
auto.cnf ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test zyshanlinux-02.err zyshanlinux-02.pid
[root@zyshanlinux-02 ~]# scp 192.168.106.128:/tmp/*sql /tmp/
root@192.168.106.128's password:
mysql2.sql 100% 30KB 1.1MB/s 00:00
zrlog.sql 100% 10KB 5.8MB/s 00:00
[root@zyshanlinux-02 ~]# mysql -uroot
-bash: mysql: command not found
[root@zyshanlinux-02 ~]# alias 'mysql=/usr/local/mysql/bin/mysql'
[root@zyshanlinux-02 ~]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'
[root@zyshanlinux-02 ~]# 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 zyshan;
Query OK, 1 row affected (0.03 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
导入库,保证2边数据一致。
[root@zyshanlinux-02 ~]# mysql -uroot zrlog < /tmp/zrlog.sql
[root@zyshanlinux-02 ~]# mysql -uroot zyshan < /tmp/zrlog.sql
[root@zyshanlinux-02 ~]# mysql -uroot mysql2 < /tmp/mysql2.sql
这一步要根据主上的File和Position来填写,实现主从关键的一步
[root@zyshanlinux-02 ~]# 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='192.168.106.128', master_user='repl', master_password='zyshanlinux111', master_log_file='zyshanlinux1.000003', master_log_pos=671072;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql>
判断主从是否成功:show slave status\G
是否有这两个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:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.106.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: zyshanlinux1.000003
Read_Master_Log_Pos: 671072
Relay_Log_File: zyshanlinux-02-relay-bin.000002
Relay_Log_Pos: 286
Relay_Master_Log_File: zyshanlinux1.000003
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: 671072
Relay_Log_Space: 468
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: 128
Master_UUID: 69cdc157-7ec6-11e8-9834-000c29a1d4eb
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)
还要到主上执行 unlock tables
[root@zyshanlinux-001 mysql]# mysql -uroot -pmysql01
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 20
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>
主从上的数据一定要同步,如果不一致,后期会非常麻烦。
测试主从同步
几个配置参数:在/etc/my.cnf配置文件里配置。
主服务器上
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= //推荐用
测试主从
1、数据变化
主操作:
mysql> use zyshan;
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_zyshan |
+------------------+
| comment |
| link |
| log |
| lognav |
| plugin |
| tag |
| type |
| user |
| website |
+------------------+
9 rows in set (0.00 sec)
mysql> select count(*) from lognav;
+---------+
| count(*) |
+---------+
| 2 |
+---------+
1 row in set (0.00 sec)
mysql>
从操作:
mysql> use zyshan;
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(*) from lognav;
+---------+
| count(*) |
+---------+
| 2 |
+---------+
1 row in set (0.00 sec)
mysql>
主操作:清空表的数据
mysql> select count(*) from lognav;
+----------+
| count(*) |
+----------+
| 2 |
+----------+
1 row in set (0.00 sec)
mysql> truncate table lognav;
Query OK, 0 rows affected (0.01 sec)
mysql> select count(*) from lognav;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
mysql>
mysql> select * from lognav; ##确实是没数据了。
Empty set (0.00 sec)
从操作:
mysql> select count(*) from lognav;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
mysql> select * from lognav; ##确实是没数据了。
Empty set (0.00 sec)
2、表格删除
主操作:删除website表
mysql> show tables;
+------------------+
| Tables_in_zyshan |
+------------------+
| comment |
| link |
| log |
| lognav |
| plugin |
| tag |
| type |
| user |
| website |
+------------------+
9 rows in set (0.00 sec)
mysql> drop table website;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+------------------+
| Tables_in_zyshan |
+------------------+
| comment |
| link |
| log |
| lognav |
| plugin |
| tag |
| type |
| user |
+------------------+
8 rows in set (0.00 sec)
mysql>
从查看:website表也没了
mysql> show tables;
+------------------+
| Tables_in_zyshan |
+------------------+
| comment |
| link |
| log |
| lognav |
| plugin |
| tag |
| type |
| user |
| website |
+------------------+
9 rows in set (0.00 sec)
mysql> show tables;
+------------------+
| Tables_in_zyshan |
+------------------+
| comment |
| link |
| log |
| lognav |
| plugin |
| tag |
| type |
| user |
+------------------+
8 rows in set (0.00 sec)
mysql>
3、错误操作,主从不同步报错,恢复主从
从错误操作:在从上操作库,删除zyshan库,用show slave status\G查看虽然还是2个yes,但操作主后就会报错的。
mysql> drop database zyshan;
Query OK, 8 rows affected (0.12 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.106.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: zyshanlinux1.000003
Read_Master_Log_Pos: 671072
Relay_Log_File: zyshanlinux-02-relay-bin.000002
mysql> drop database zyshan;
Query OK, 8 rows affected (0.12 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.106.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: zyshanlinux1.000003
Read_Master_Log_Pos: 681862
Relay_Log_File: zyshanlinux-02-relay-bin.000002
Relay_Log_Pos: 11076
Relay_Master_Log_File: zyshanlinux1.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
主操作:
mysql> drop database zyshan;
Query OK, 8 rows affected (0.18 sec)
mysql>
从操作:报错了,Last_Error:错误提示是没有这个zyshan这个库可删除的。
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.106.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: zyshanlinux1.000003
Read_Master_Log_Pos: 681960
Relay_Log_File: zyshanlinux-02-relay-bin.000002
Relay_Log_Pos: 11076
Relay_Master_Log_File: zyshanlinux1.000003
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1008
Last_Error: Error 'Can't drop database 'zyshan'; database doesn't exist' on query. Default database: 'zyshan'. Query: 'drop database zyshan'
从操作:
在两边数据一致的时候(两边都删除,其他都没动);可以尝试修复错误,不一定成功。
先停止从,再启动从;查看还是不行,只能从新做主从了。
mysql> stop slave;
Query OK, 0 rows affected (0.00 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: 192.168.106.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: zyshanlinux1.000003
Read_Master_Log_Pos: 681960
Relay_Log_File: zyshanlinux-02-relay-bin.000002
Relay_Log_Pos: 11076
Relay_Master_Log_File: zyshanlinux1.000003
Slave_IO_Running: Yes
Slave_SQL_Running: No
从新做主从
主操作:
mysql> show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| zyshanlinux1.000003 | 681960 | | | |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
从操作:成功恢复主从
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_host='192.168.106.128', master_user='repl', master_password='zyshanlinux111', master_log_file='zyshanlinux1.000003', master_log_pos=681960;
Query OK, 0 rows affected, 2 warnings (0.01 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.106.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: zyshanlinux1.000003
Read_Master_Log_Pos: 681960
Relay_Log_File: zyshanlinux-02-relay-bin.000002
Relay_Log_Pos: 286
Relay_Master_Log_File: zyshanlinux1.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
上面是主从数据还一致才能这么简单恢复成功主从。如果主从数据不一致的话,就必须去备份数据再恢复数据,再来做主从。
拓展:
遇到主从不能正常同步,提示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.htmlmysql主从延迟 http://f.dataguru.cn/thread-461916-1-1.html 深入探究主从延迟 http://ningg.top/inside-mysql-master-slave-delay/mysql主从不同步如何做 http://www.jb51.net/article/33052.htmmysql 主主 http://www.cnblogs.com/ygqygq2/p/6045279.htmlmysql-proxy 实现读写分离 http://my.oschina.net/barter/blog/93354 mycat实现读写分离 http://www.th7.cn/db/mysql/201708/250280.shtmlatlas相关 http://www.oschina.net/p/atlasmysql一主多从 http://blog.sina.com.cn/s/blog_4c197d4201017qjs.htmlmysql环形主从 http://ask.apelearn.com/question/11437cobar实现分库分表 http://blog.csdn.net/huoyunshen88/article/details/37927553mysql分库分表方案 http://my.oschina.net/ydsakyclguozi/blog/199498mysql架构演变 http://www.aminglinux.com/bbs/thread-8025-1-1.htmlfMHA架构 http://www.dataguru.cn/thread-457284-1-1.html比较复杂的mysql集群架构 http://ask.apelearn.com/question/17026
直播拓展:
Mysql主从不同步解决方法http://www.rfyy.net/archives/2309.htmlMYSQL主从同步故障一例及解决过程!http://blog.51cto.com/storysky/259280MySQL auto_increment_increment,auto_increment_offset 用法https://blog.csdn.net/leshami/article/details/39779509配置Mysql-proxy,实现读写分离http://blog.51cto.com/zzclinux/1980487MySQL分布式中间件MYCAT解析https://my.oschina.net/ruoli/blog/1789370学会数据库读写分离、分表分库——用Mycat https://www.cnblogs.com/joylee/p/7513038.htmlMySQL + Atlas 部署读写分离https://blog.csdn.net/AnPHPer/article/details/80566385Step By Step 搭建 MySql MHA 集群http://blog.51cto.com/xiaoshuaigege/2060768mysql双主+双从+keepalived+lvs架构http://ask.apelearn.com/question/17026