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语句落地
MySQL主从原理图
主从配置
配置主服务器
编辑配置文件添加如下参数:
[root@123 ~]# vim /etc/my.cnf
……
server-id=137
#自定义
log_bin=linux1
#指定log前缀
编辑完成后重启mysql服务:
[root@harry-01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL...... SUCCESS!
Starting MySQL.................. SUCCESS!
查看mysql库文件:
[root@123 ~]# ls -lt /data/mysql/
……
-rw-rw---- 1 mysql mysql 20 8月 30 15:52 linux1.index
-rw-rw---- 1 mysql mysql 120 8月 30 15:52 linux1.000001
……
重启后生成两个前缀为adailinux1的二进制文件。
新建一个数据库为试验做准备:
[root@harry-01 ~]# cd /data/mysql/
#备份一个数据库:
[root@harry-01 mysql]# mysqldump -uroot -p123456 zrlog > /tmp/zrlog.sql
#新建一个数据库:
[root@harry-01 mysql]# mysql -uroot -p123456 -e "create database test"
#将备份的数据恢复到新建的数据库中:
[root@harry-01 mysql]# mysql -uroot -p123456 test < /tmp/zrlog.sql
创建一个用于同步数据的用户:
[root@harry-01 mysql]# mysql -uroot -p
mysql>
mysql> grant replication slave on *.* to 'repl'@'192.168.31.139' identified by '123123';
Query OK, 0 rows affected (0.00 sec)
#IP为“从”的IP
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
#锁定数据表(目的是暂时使其不能继续写,保持现有状态用于同步)
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| linux1.000002 | 331 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#记住file和position(设置主从同步时会使用)
mysql> quit
Bye
[root@harry-01 mysql]#
备份主库中所有数据库:
[root@harry-01 mysql]# mysqldump -uroot -p123123 db1 > /tmp/db1.sql
[root@harry-01 mysql]# mysqldump -uroot -p123123 test > /tmp/test.sql
配置从服务器
编辑配置文件:
[root@harry-01 ~]# vim /etc/my.cnf
[mysqld]
server-id=139
socket=/tmp/mysql.sock
[root@harry-01~]# /etc/init.d/mysqld restart
Shutting down MySQL...... SUCCESS!
Starting MySQL.............................. SUCCESS!
配置完成后将主中备份的数据发送到从中:
[root@harry-01 ~]# scp 192.168.31.137:/tmp/*.sql /tmp
The authenticity of host '192.168.31.137 (192.168.31.137)' can't be established.
ECDSA key fingerprint is SHA256:jRFlrf6aLThlCgR1+qtwwkj4N1EA/5uWD+UD44ABACY.
ECDSA key fingerprint is MD5:05:9f:71:e0:f2:50:06:51:8d:be:9f:50:84:3f:96:82.
root@192.168.31.137's password:
db1.sql 100% 1774 953.0KB/s 00:00
mysql_all.sql 100% 649KB 4.5MB/s 00:00
mysqlbak.sql 100% 647KB 7.2MB/s 00:00
test.sql 100% 0 0.0KB/s 00:00
zrlog.sql 100% 9779 5.7MB/s 00:00
恢复数据库:
[root@harry-01 mysql]# mysql -uroot -p123123 db1 < /tmp/db1.sql
[root@harry-01 mysql]# mysql -uroot -p123123 test < /tmp/test.sql
[root@harry-01 mysql]# mysql -uroot -p123123 zrlog < /tmp/zrlog.sql
[root@harry-01 mysql]# mysql -uroot -p123123 mysql < /tmp/mysql_all.sql
[root@harry-01 mysql]# mysql -uroot -p123123 mysql < /tmp/mysqlbak.sql
注意: 该过程要保证主从数据库内容的一致。
实现主从同步
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.31.137',master_user='repl',master_password='123123',master_log_file='linux1.000002',master_log_pos=331;
#IP为主的IP;file、pos分别为主的filename和position。
#检测主从是否建立成功:
mysql> start slave;
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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.
#致命错误:由于主从服务器uuid相等,从I/O线程停止;要使复制正常工作,这些uuid必须不同。
出现error信息
原因分析:
mysql 5.6的复制引入了uuid的概念,各个复制结构中的server_uuid得保证不一样,但是查看到直接copy data文件夹后server_uuid是相同的,show variables like '%server_uuid%';
解决方法:
找到/data/mysql下的auto.cnf文件,修改里面的uuid值,保证各个mysql的uuid不一样,重启msyql即可
以上操作完成后,解锁主库的表(在主上操作):
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
测试主从
参数介绍:
主服务器:
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=
进行从服务器的配置时尽量使用参数“replicate_wild_”,使匹配更精确,提升使用性能。
测试
主服务器:
mysql> use harry
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>
mysql> show tables;