MySQL主从复制(Master-Slave)实践
MySQL数据库自身提供的主从复制功能可以方便的实现数据的多处自动备份,实现数据库的拓展。多数据备份不仅可以加强数据的安全性,通过实现读写分离还能进一步提升数据库的负载性能。
下图就描述了一个多个数据库间主从复制与读写分离的模型(来源网络):
MySql的主从复制
在一主多从的数据库体系中,多个从服务器采用异步的方式更新主数据库的变化,业务服务器在执行写或者相关修改数据库的操作是在主服务器上进行的,读操作则是在各从服务器上进行。如果配置了多个从服务器或者多个主服务器又涉及到相应的负载均衡问题,关于负载均衡具体的技术细节还没有研究过,今天就先简单的实现一主一从的主从复制功能。

Mysql主从复制的实现原理图大致如下(来源网络):
MySql的主从复制
MySQL之间数据复制的基础是二进制日志文件(binary log file)。一台MySQL数据库一旦启用二进制日志后,其作为master,它的数据库中所有操作都会以“事件”的方式记录在二进制日志中,其他数据库作为slave通过一个I/O线程与主服务器保持通信,并监控master的二进制日志文件的变化,如果发现master二进制日志文件发生变化,则会把变化复制到自己的中继日志中,然后slave的一个SQL线程会把相关的“事件”执行到自己的数据库中,以此实现从数据库和主数据库的一致性,也就实现了主从复制。

实现MySQL主从复制需要进行的配置:
主服务器:
开启二进制日志

log_bin=mysql-bin

配置唯一的server-id

server_id=#

获得master二进制日志文件名及位置
创建一个用于slave和master通信的用户账号

GRANT REPLCATION SLAVE ON *.* TO
'repluser'@'HOST' IDENTIFIED BY 'replpass';

从服务器:
配置唯一的server-id

server_id=#

使用master分配的用户账号读取master二进制日志
启用slave服务

具体实现过程如下:
实验环境:
两台cenos7.4 的服务器 (linux)
IP 地址分别为 192.168.137.130(主) 192.168.137.131(从)

1.两台机器分别安装 数据库

yum install mariadb mariadb-server -y

2.修改主服务器的配置

[[email protected] ~]# vi /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log_bin=mysql-bin
server_id=1


启动数据库

systemctl start mariadb

3.创建有复制权限的用户账号

mysql

MariaDB [(none)]> grant replication slave on *.* to [email protected]'192.168.137.%' identified by 'centos';
Query OK, 0 rows affected (0.00 sec)

4.查看master状态,记录二进制文件名

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      396 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

5.从服务器配置的修改

[[email protected] ~]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server_id=2




启动从数据库服务

[[email protected] ~]# systemctl start mariadb

6.使用有复制权限的用户账号连接至主服务器,并启动复制线程

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.137.130', MASTER_USER='cwf', MASTER_PASSWORD='centos', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=396;
Query OK, 0 rows affected (0.01 sec)


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

7.查看slave的状态

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.137.130
                  Master_User: cwf
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 396
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            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: 396
              Relay_Log_Space: 825
              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: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
1 row in set (0.00 sec)

当Slave_IO_Running和Slave_SQL_Running都为YES的时候就表示主从同步设置成功了。接下来就可以进行一些验证了。

验证
1.首先查看 主数据库 和从 数据库 的 数据

主
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
从

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

因为都是新安装的 所以数据都一样

2.给 主服务器的数据库创建 一个数据库 测试从服务器是否同步


主

MariaDB [(none)]> create database cwftest;
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cwftest            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)



下来 去看看从 是否同步 过来

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cwftest            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)


MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.137.130
                  Master_User: cwf
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 485
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 618
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            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: 485
              Relay_Log_Space: 914
              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: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR: No query specified

经过 测试 从服务器 可以同步过了。

结束了

假如 主服务器数据库 已经 有了 1T 的 数据了 我们还继续 从 336记录哪儿 开始 复制 ???

我们可以 把主服务器 的 数据备份 然后传 给 从服务器 直接导入 进去
然后 在做 主从 复制。

相关文章:

  • 2021-05-13
  • 2021-09-06
  • 2022-01-17
  • 2021-08-03
猜你喜欢
  • 2022-02-16
  • 2021-07-23
  • 2021-11-23
  • 2021-11-23
  • 2021-09-04
  • 2022-01-02
相关资源
相似解决方案