17.1

17.1 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主从原理图:

mark
应用环境:备份重要数据、分担主库数据读取压力

17.2准备工作

安装并启动mysql服务;

17.3配置主服务器

主从配置 - 主上操作

安装mysql
修改my.cnf,增加server-id=130和log_bin=aminglinux1
修改完配置文件后,启动或者重启mysqld服务
把mysql库备份并恢复成aming库,作为测试数据
mysqldump -uroot mysql > /tmp/mysql.sql
mysql -uroot -e “create database aming”
mysql -uroot aming < /tmp/mysql.sql
创建用作同步数据的用户
grant replication slave on *.* to 'repl'@slave_ip identified by 'password';
flush tables with read lock;
show master status;

编辑配置文件/etc/my.cnf并重启服务:

[root@localhost ~]# vim /etc/my.cnf
……
server-id=132    //自定义
log_bin=DasonCheng1    //指定log前缀
[root@DasonCheng ~]# /etc/init.d/mysqld restart
Shutting down MySQL...... SUCCESS! 
Starting MySQL.................. SUCCESS! 

新建一个数据库做准备:

[root@DasonCheng ~]# ls -lt /data/mysql/    //服务重启之后,会生成两个指定前缀的文件;
……
-rw-rw----  1 mysql mysql       20 8月  30 15:52 DasonCheng1.index
-rw-rw----  1 mysql mysql      120 8月  30 15:52 DasonCheng1.000001
……
[root@DasonCheng ~]# cd /data/mysql/
[root@DasonCheng mysql]# mysqldump -uroot -p123456 zrlog > /tmp/zrlog.sql     //备份一个数据库;
[root@DasonCheng mysql]# mysql -uroot -p123456 -e "create database Dasontest"    //新建一个数据库;
[root@DasonCheng mysql]# mysql -uroot -p123456 Dasontest < /tmp/zrlog.sql    //将备份的数据恢复到新建的数据库中;

创建一个用于同步用户:

[root@DasonCheng 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 |
+-------------------+----------+--------------+------------------+-------------------+
| DasonCheng1.000001 |    10844 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#记住file和position(设置主从同步时会使用)
mysql> quit
Bye
[root@DasonCheng mysql]# mysqldump -uroot -p123456 Dason > /tmp/Dason.sql
[root@DasonCheng mysql]# mysqldump -uroot -p123456 Dason2 > /tmp/Dason2.sql

主从配置 - 从上操作

 安装mysql
 查看my.cnf,配置server-id=132,要求和主不一样
 修改完配置文件后,启动或者重启mysqld服务
 把主上aming库同步到从上
 可以先创建aming库,然后把主上的/tmp/mysql.sql拷贝到从上,然后导入aming库
 mysql -uroot
 stop slave;
 change master to master_host='', master_user='repl', master_password='', master_log_file='', master_log_pos=xx,
 start slave;
 还要到主上执行 unlock tables

编辑配置文件并重启

[root@localhost ~]# vim /etc/my.cnf
……
server-id=130
……
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL...... SUCCESS! 
Starting MySQL.............................. SUCCESS! 

配置完成后将主中备份的数据发送到从中:

[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: 
Dason2.sql                                             100% 1259     1.2KB/s   00:00    
Dason.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 Dason;
Query OK, 1 row affected (0.03 sec)
mysql> create database Dason2;
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 Dason < /tmp/Dason.sql 
[root@localhost ~]# mysql -uroot Dason2 < /tmp/Dason2.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='DasonCheng1.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@DasonCheng 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=
  • 注: 进行从服务器的配置时尽量使用参数“replicate_wild_”,使匹配更精确,提升使用性能。
测试:

主服务器:

mysql> show tables;
+---------------------------+
| Tables_in_Dasontest        |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
+---------------------------+
删除表:
mysql> drop table db;
mysql> show tables;
+---------------------------+
| Tables_in_Dasontest        |
+---------------------------+
| columns_priv              |
| event                     |
+---------------------------+

从服务器:

主服务器删除表之前:
mysql> show tables;
+---------------------------+
| Tables_in_Dasontest        |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
+---------------------------+
主服务器删除表之后:
mysql> show tables;
+---------------------------+
| Tables_in_Dasontest        |
+---------------------------+
| columns_priv              |
| event                     |
+---------------------------+

配置完成!

转载于:https://my.oschina.net/u/3651233/blog/1527348

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值