MySQL主从配置
17.1 MySQL主从介绍
MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步。
MySQL主从是基于binlog的,主上须开启binlog才能进行主从。
主从过程
- 主将更改操作记录到binlog中
- 从将主的binlog事件(SQL语句)同步到本机并记录在relaylog中
- 从根据relaylog里面的SQL语句按顺序执行
说明: 该过程有三个线程,主上有一个log dump线程,用来和从的i/o线程传递binlog;从上有两个线程,其中i/o线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的SQL语句落地。
工作原理
应用环境
- 备份重要数据
- 分担主库数据读取压力
准备工作
安装并启动MySQL服务。
配置主服务器
编辑配置文件添加如下参数:
- [root@123 ~]# vim /etc/my.cnf
- ……
- server-id=132
- #自定义
- log_bin=linux1
- #指定log前缀
编辑完成后重启mysql服务:
- [root@123 ~]# /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@123 ~]# cd /data/mysql/
-
- 备份一个数据库:
- [root@123 mysql]# mysqldump -uroot -p123456 zrlog > /tmp/zrlog.sql
-
- 新建一个数据库:
- [root@123 mysql]# mysql -uroot -p123456 -e "create database test"
-
- 将备份的数据恢复到新建的数据库中:
- [root@123 mysql]# mysql -uroot -p123456 test < /tmp/zrlog.sql
创建一个用于同步数据的用户:
- [root@123 mysql]# mysql -uroot -p123456
- Welcome to the MySQL monitor.
- mysql> grant replication slave on *.* to 'repl'@'192.168.8.130' identified by '123456';
- Query OK, 0 rows affected (0.01 sec)
- #IP为“从”的IP
- mysql> flush tables with read lock;
- Query OK, 0 rows affected (0.12 sec)
- #锁定数据表(目的是暂时使其不能继续写,保持现有状态用于同步)
- mysql> show master status;
- +-------------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +-------------------+----------+--------------+------------------+-------------------+
- | linux1.000001 | 10844 | | | |
- +-------------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
- #记住file和position(设置主从同步时会使用)
- mysql> quit
- Bye
备份主库中所有数据库:
- [root@123 mysql]# mysqldump -uroot -p123456 linux > /tmp/linux.sql
- [root@123 mysql]# mysqldump -uroot -p123456 linux2 > /tmp/linux2.sql
配置从服务器
编辑配置文件:
- [root@localhost ~]# vim /etc/my.cnf
- ……
- server-id=130
- ……
-
- [root@localhost ~]# /etc/init.d/mysqld restart
- Shutting down MySQL...... SUCCESS!
- Starting MySQL.............................. SUCCESS!
说明: 从的server-id要和主的server-id不同。
配置完成后将主中备份的数据发送到从中:
- [root@localhost ~]# scp 192.168.8.132:/tmp/*.sql /tmp/
- The authenticity of host '192.168.8.132 (192.168.8.132)' can't be established.
- ECDSA key fingerprint is 78:22:19:9e:d5:4a:9d:cb:71:54:d7:c0:9a:13:18:9c.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added '192.168.8.132' (ECDSA) to the list of known hosts.
- root@192.168.8.132's password:
- alinux2.sql 100% 1259 1.2KB/s 00:00
- linux.sql 100% 1258 1.2KB/s 00:00
- zrlog.sql 100% 9864 9.6KB/s 00:00
创建库:
- [root@localhost ~]# mysql -uroot
- Welcome to the MySQL monitor.
- mysql> create database linux;
- Query OK, 1 row affected (0.03 sec)
- mysql> create database linux2;
- Query OK, 1 row affected (0.00 sec)
- mysql> create database zrlog;
- Query OK, 1 row affected (0.00 sec)
- mysql> quit
- Bye
恢复数据库:
- [root@localhost ~]# mysql -uroot linux < /tmp/linux.sql
- [root@localhost ~]# mysql -uroot linux2 < /tmp/linux2.sql
- [root@localhost ~]# mysql -uroot zrlog < /tmp/zrlog.sql
注意: 该过程要保证主从数据库内容的一致。
实现主从同步
- [root@localhost ~]# mysql -uroot
- Welcome to the MySQL monitor.
- mysql> stop slave;
- Query OK, 0 rows affected, 1 warning (0.06 sec)
- mysql> change master to master_host='192.168.8.132',master_user='repl',master_password='123456',master_log_file='linux1.000001',master_log_pos=10844;
- Query OK, 0 rows affected, 2 warnings (0.46 sec)
- #IP为主的IP;file、pos分别为主的filename和position。
-
- 检测主从是否建立成功:
- mysql> start slave;
- Query OK, 0 rows affected (0.22 sec)
- mysql> show slave status\G
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
以上操作完成后,解锁主库的表(在主上操作):
- [root@123 mysql]# mysql -uroot -p123456
- Welcome to the MySQL monitor.
- 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> show tables;
- +---------------------------+
- | Tables_in_adaitest |
- +---------------------------+
- | columns_priv |
- | db |
- | event |
- +---------------------------+
-
- 删除表:
- mysql> drop table db;
- mysql> show tables;
- +---------------------------+
- | Tables_in_adaitest |
- +---------------------------+
- | columns_priv |
- | event |
- +---------------------------+
从服务器:
- 主服务器删除表之前:
- mysql> show tables;
- +---------------------------+
- | Tables_in_adaitest |
- +---------------------------+
- | columns_priv |
- | db |
- | event |
- +---------------------------+
- 主服务器删除表之后:
- mysql> show tables;
- +---------------------------+
- | Tables_in_adaitest |
- +---------------------------+
- | columns_priv |
- | event |
- +---------------------------+
即,主从配置成功!
遇到主从不能正常同步,提示uuid相同的错误。这是因为克隆机器导致。
https://www.2cto.com/database/201412/364479.html
https://www.2cto.com/database/201412/364479.html