17.1 MySQL 主从介绍
MySQL 主从又叫做 ReplicationAB 复制简单讲就是 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 语句落地
工作原理
mark
应用环境
备份重要数据
分担主库数据读取压力
17.2 准备工作
安装并启动 MySQL 服务
17.3 配置主服务器
编辑配置文件添加如下参数:
- [root@localhost ~]# vim /etc/my.cnf
- server-id=132
- # 自定义
- log_bin=centos-01linux1
- # 指定 log 前缀
编辑完成后重启 mysql 服务:
- [root@centos-01linux ~]# /etc/init.d/mysqld restart
- Shutting down MySQL...... SUCCESS!
- Starting MySQL.................. SUCCESS!
查看 mysql 库文件:
[root@centos-01linux ~]# ls -lt /data/mysql/
-rw-rw---- 1 mysql mysql 20 8 月 30 15:52 centos-01linux1.index
-rw-rw---- 1 mysql mysql 120 8 月 30 15:52 centos-01linux1.000001
说明: 重启后生成两个前缀为 centos-01linux1 的二进制文件
新建一个数据库为试验做准备:
[root@centos-01linux ~]# cd /data/mysql/
备份一个数据库:
[root@centos-01linux mysql]# mysqldump -uroot -p123456 zrlog> /tmp/zrlog.sql
新建一个数据库:
[root@centos-01linux mysql]# mysql -uroot -p123456 -e "create database centos-01test"
将备份的数据恢复到新建的数据库中:
[root@centos-01linux mysql]# mysql -uroot -p123456 centos-01test </tmp/zrlog.sql
创建一个用于同步数据的用户:
- [root@centos-01linux 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 |
- +-------------------+----------+--------------+------------------+-------------------+
- | centos-01linux1.000001 | 10844 | | | |
- +-------------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
- # 记住 file 和 position(设置主从同步时会使用)
- mysql> quit
- Bye
备份主库中所有数据库:
- [root@centos-01linux mysql]# mysqldump -uroot -p123456 centos-01> /tmp/centos-01.sql
- [root@centos-01linux mysql]# mysqldump -uroot -p123456 centos-012> /tmp/centos-012.sql
17.4 配置从服务器
编辑配置文件:
- [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) cant 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.132s password:
- centos-012.sql 100% 1259 1.2KB/s 00:00
- centos-01.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 centos-01;
- Query OK, 1 row affected (0.03 sec)
- mysql> create database centos-012;
- 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 centos-01 </tmp/centos-01.sql
- [root@localhost ~]# mysql -uroot centos-012 < /tmp/centos-012.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=centos-01linux1.000001,master_log_pos=10844;
- Query OK, 0 rows affected, 2 warnings (0.46 sec)
- #IP 为主的 IP;filepos 分别为主的 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@centos-01linux mysql]# mysql -uroot -p123456
- Welcome to the MySQL monitor.
- mysql> unlock tables;
- Query OK, 0 rows affected (0.00 sec)
至此主从配置搭建完成!!!
17.5 测试主从
参数介绍
主服务器:
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=
注: 进行从服务器的配置时尽量使用参数 replicatewild, 使匹配更精确, 提升使用性能
测试
主服务器:
- mysql> show tables;
- +---------------------------+
- | Tables_in_centos-01test |
- +---------------------------+
- | columns_priv |
- | db |
- | event |
- +---------------------------+
删除表:
- mysql> drop table db;
- mysql> show tables;
- +---------------------------+
- | Tables_in_centos-01test |
- +---------------------------+
- | columns_priv |
- | event |
- +---------------------------+
从服务器:
主服务器删除表之前:
- mysql> show tables;
- +---------------------------+
- | Tables_in_centos-01test |
- +---------------------------+
- | columns_priv |
- | db |
- | event |
- +---------------------------+
主服务器删除表之后:
- mysql> show tables;
- +---------------------------+
- | Tables_in_centos-01test |
- +---------------------------+
- | columns_priv |
- | event |
- +---------------------------+
即, 主从配置成功!
来源: http://www.bubuko.com/infodetail-2547780.html