第二次作业 运维高级 MySQL主从复制

1.理解MySQL主从复制原理

在这里插入图片描述

1、master(binlog dump thread)主要负责Master库中有数据更新的时候,会按照binlog格式,将更新的事件类型写入到主库的binlog文件中。

2、I/O thread线程在Slave中创建,该线程用于请求Master,Master会返回binlog的名称以及当前数据更新的位置、binlog文件位置的副本。将binlog保存在 「relay log(中继日志)」 中

3、SQL线程也是在Slave中创建的,当Slave检测到中继日志有更新,就会将更新的内容同步到Slave数据库中,这样就保证了主从的数据的同步

2.完成MySQL主从复制

环境准备
两台机器一主一从
由于服务器是克隆复制的,要更改从库的uuid

[root@localhost ~]# vim /var/lib/mysql/auto.cnf

[auto]
server-uuid=892dc96a-1b13-11ee-b662-000c29aba114

1、设置主从库的server-id值并开启binlog参数(从可以不用开启binlog)

[root@localhost ~]# vim /etc/my.cnf

log_bin=mysql-bin
server-id=140 # 主从的server-id唯一

2、在主库上建立同步账号

mysql> grant replication slave on *.* to 'user'@'192.168.159.%' identified by '12345';
Query OK, 0 rows affected, 1 warning (0.00 sec)

3、 备份主库数据

[root@localhost ~]# mysql -uroot -p12345 -e 'show databases'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db                 |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+

[root@localhost ~]# mysqldump -uroot -p -A -B |gzip > db.sql.gz
Enter password: 

4、主库备份数据上传到从库

[root@localhost ~]# scp db.sql.gz 192.168.159.141:/root/
root@192.168.159.141's password: 
db.sql.gz                                                        100%  205KB  68.4MB/s   00:00 

5、还原从主库备份数据

[root@localhost ~]# zcat db.sql.gz | mysql -uroot -p12345
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p12345 -e 'show databases'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db                 |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+

6、查看主库状态

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 |      742 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

7、设定从主库同步

mysql> change master to
    -> MASTER_HOST='192.168.159.140',
    -> MASTER_PORT=3306,
    -> MASTER_USER='user',
    -> MASTER_PASSWORD='12345',
    -> MASTER_LOG_FILE='mysql-bin.000007',
    -> MASTER_LOG_POS=742;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

8、重启数据库

[root@localhost ~]# systemctl restart mysqld

9、启动从库同步开关并检查状态

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show slave status\G

当都为yes是即成功
在这里插入图片描述
测试
在这里插入图片描述
问题:
在这里插入图片描述
当IO为connecting时,是由于没有关闭防火墙。关闭防火墙即可。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值