MySQL双机热备

主机: 192.168.1.22

[root@localhost ~]# mysql -V
mysql  Ver 15.1 Distrib 5.5.65-MariaDB, for Linux (x86_64) using readline 5.1

从机: 192.168.1.148

[root@kafka3 opt]# mysql -V
mysql  Ver 15.1 Distrib 5.5.68-MariaDB, for Linux (x86_64) using readline 5.1
0. 准备,先全量同步两边数据库

主数据库全量导出

mysqldump -uroot -p111111 game > /tmp/game.sql

从数据库导入

mysql -uroot -p111111 game < /tmp/game.sql 

其他同步方式亦可,此时注意不要再操作主数据库了。

1. 创建复制账号

在主数据库中创建账号rep1,授权复制的权限,用来做主从复制。
. 这里指定了权限的范围,rep1是新增的用户,a5a55a5a是密码

mysql>GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.168.1.148' identified by 'a5a55a5a';
mysql>FLUSH PRIVILEGES;

从机验证下账号是否可用,如下表明是可用的

[root@kafka3 tmp]# mysql -h192.168.1.22 -urep1 -pa5a55a5a
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>
2. 配置主库

也就是修改在 192.168.1.22 这台主机上的mysql配置
修改主数据库配置文件, vim /etc/my.cnf
server-id一般采用ip的后八位,binlog-do-db指定要备份的数据库

[mysqld]
log-bin = mysql-bin
server-id = 22
# 只同步game数据库 
binlog-do-db = game 

查看主库master状态

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      245 | game         |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
3. 配置从库

也就是修改在 192.168.1.148 这台主机上的mysql配置

[mysqld]
server-id=148 #设置server-id,必须唯一

配置MASTER_HOST

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.1.22',
    -> MASTER_PORT=3306,
    -> MASTER_USER='rep1',
    -> MASTER_PASSWORD='a5a55a5a',
    -> MASTER_LOG_FILE='binlog.000003',
    -> MASTER_LOG_POS=245; 
Query OK, 0 rows affected (0.20 sec)

重启数据库
启动同步任务

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

查看状态

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.1.22
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000003
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: binlog.000003
             Slave_IO_Running: No
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 245
              Relay_Log_Space: 245
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1236
                Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 22
1 row in set (0.01 sec)

ERROR: No query specified

上面有报错,重启下slave, Slave_IO_Running: Yes , Slave_SQL_Running: Yes 表示成功

MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]> reset slave;
Query OK, 0 rows affected (0.12 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.08 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.22
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 245
               Relay_Log_File: mariadb-relay-bin.000005
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
4. 验证

在主库中插入数据,在备库中查看。

5. 热备份原理
5.1 mysql数据复制机制
  1. 主库把数据更改记录到二进制日志(bin-log),这些记录被称为二进制日志事件。
  2. 备库将主库上的日志同步到自己的relay-log中
  3. 备库读取relay-log的事件,将其放到备库数据之上
    如图所示
    image
5.2

MySQL复制大致分为基于语句的复制和基于行的复制。
基于语句的复制,前面所用的全量备份就是此类,所用mysqldump出sql语句,备库通过执行sql完成备份。它的优点是操作简单,这种复制方法,必须是串行的,否则容易出错。
基于行的复制,通过bin-log,正确的复制每一行。但是对于修改操作,行复制不是好办法。像是update这种操作,就要复制很多数据,而基于语句的复制,则需要很短的一句sql就可以完成。

其他

安装配置mysql/mariadb

[root@kafka3 opt]# yum install -y mariadb.x86_64
[root@kafka3 opt]# systemctl start mariadb.service
[root@kafka3 opt]# mysql_secure_installation

参考
[0] 高性能MySQL
[1] https://blog.csdn.net/u013256816/article/details/52536283

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值