MySQL主从复制,实时,双机热备份。

一. 准备工作

  1. 准备两台服务器(电脑),接入局域网中,使互相ping得通对方
  2. 两台服务器都安装mysql,必须保证mysql的版本一致
  3. 假设,服务器master:192.168.0.2,服务器slave:192.168.0.3

二. 创建同步用户

在主服务器上为从服务器建立一个连接账户,该账户必须授予replication slave权限。因为一主一从,所以只需要在服务器master建立一个同步用户就可以了。
服务器master:
创建账号:jun
密码:0000
权限:replication slave

mysql> grant replication slave on *.* to 'jun'@'%' identified by '0000';
mysql> flush privileges;

三. 配置服务器master

1、编辑my.ini文件

开启二进制日志
指定需要备份的库
指定日志格式

[mysqld] 
server-id=1
log-bin="mysql-bin"
binlog-do-db="my_db_test"
binlog-do-db="my_db_test2"##多个库就写多行
binlog_format="MIXED"

2、重启mysql服务

3、进入服务器master查看是否启用了日志

mysql> show variables like 'log_bin';

±--------------±------+
| Variable_name | Value |
±--------------±------+
| log_bin | ON |
±--------------±------+
1 row in set (0.00 sec)

4、进入服务器master主库上查看数据结点

mysql> show master status;

±---------------±---------±-------------±-----------------±------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
±---------------±---------±-------------±-----------------±------------------+
| mysql-bin.000007 | 120 | | | |
±---------------±---------±-------------±-----------------±------------------+
1 row in set (0.00 sec)

四. 配置服务器slave

1、编辑my.ini文件

[mysqld] 
server-id=2
log_slave_updates = 1
read_only = 1

2、重启mysql服务

3、从库上面开启同步

mysql>CHANGE MASTER TO MASTER_HOST='192.168.0.2',MASTER_USER='jun',MASTER_PASSWORD='0000',MASTER_LOG_FILE='mysql-bin.000007',MASTER_LOG_POS=120;
mysql>stop slave;##关闭slave模式
mysql>start slave;##开启slave模式

4、查看是否实现同步成功

mysql>  show slave status\G

*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.2 Master_User: jun Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000007 Read_Master_Log_Pos: 120 Relay_Log_File: szthdb02-relay-bin.000002 Relay_Log_Pos: 281 Relay_Master_Log_File:mysql-bin.000007 Slave_IO_Running: Yes Slave_SQL_Running: Yes

##可以发现Slave_IO_Running,和Slave_SQL_Running已经都为yes说明主从复制配置成功

参考资料

Mysql主从复制以及常见错误问题分析
https://blog.51cto.com/xiaozhagn/2063693
MySQL主从仅同步指定库
https://www.cnblogs.com/new-journey/p/11319527.html
mysql主从复制(一主一从)
https://www.2cto.com/net/201902/796213.html
mysql主从复制常见问题
https://www.cnblogs.com/shixiuxian/p/11218686.html
windows下使用mysql双机热备功能
https://www.cnblogs.com/Mr-kevin/p/5590542.html
mysql主从复制 主主复制
https://segmentfault.com/a/1190000009724090
MySQL日志格式 binlog_format
https://blog.csdn.net/mycwq/article/details/17136997
在window配置MySQL5.7主从复制遇到错误及解决方法
https://blog.csdn.net/xiaozhenzi66/article/details/81220624
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章