MySQL主从复制

MySQL集群概念

数据库集群,顾名思义,就是利用至少两台或者多台数据库服务器,构成一个虚拟单一数据库逻辑映像,像单数据库系统那样,向客户端提供透明的数据服务
采用MySQL集群,需要部署主从关系,主节点用来写数据,从节点用来读数据,所以主从节点一般为一对多的关系,使用MySQL集群,用户可以从多个从节点读取数据,大大缓解了读取数据的压力,使用MySQL集群,在主节点写入的数据会自动同步到从节点从而降低了数据丢失的可能性

设置节点id

本次实验采用两台不同的服务器,一个主节点,一个从节点

  1. 将两台MySQL服务器停止
systemctl stop mysqld.service
  1. 配置my.cnf文件,确定主从节点
  • 主节点
log-bin=mysql-bin
#server-id是唯一的,每个节点的server-id必须不同,这里以每个节点的端口号为例
server-id=1
  • 从节点
log-bin=mysql-bin
#server-id是唯一的,每个节点的server-id必须不同,这里以每个节点的端口号为例
server-id=2
  1. 将两台MySQL服务启动
systemctl start mysqld.service

配置主节点

连接主节点MySQL,查看主节点二进制日志指针位置

root@iZwz9b81pjmymimkyvx8hcZ:~# mysql -uroot -p123456

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

当主节点修改数据或修改配置时,指针的位置都会发生变化
从节点会根据这个指针的位置来判断主节点的数据库状态是否发生变化,从而将自身的数据库与主节点的数据库进行同步

在主节点中赋予从节点同步的权限,同步需要创建用户名和密码

grant replication slave on *.* to '用户名'@'%' identified by '密码'

这里我创建的用户名和密码都是(yefeng)

grant replication slave on *.* to 'yefeng'@'%' identified by 'yefeng';

再次查看主节点二进制文件指针位置

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

因为我们修改了数据库的配置所以这里指针的位置发生了变化
所以我们要初始化指针位置

reset master;
mysql> reset master;
Query OK, 0 rows affected (0.01 sec)

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

配置从节点

在从节点中添加主节点的主机名,端口号,同步用户名和密码,二进制日志文件名,指针起始位置。
SQL语句

#主节点主机名,端口号
change master to master_host='主节点地址',master_port='端口号',
#同步用户名,密码
master_user='yefeng',master_password='yefeng',
#主节点二进制日志文件名,指针起始位置
master_log_file='mysql-bin.000001',master_log_pos=154;


mysql> change master to master_host='192.115.1.104',master_port=3306,
    -> master_user='yefeng',master_password='yefeng',
    -> master_log_file='mysql-bin.000001',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

启动从节点

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

查看节点状态

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.115.1.104
                  Master_User: yefeng
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: iZuf6j594ao1xc9idlv8pwZ-relay-bin.000004
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000002
             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: 154
              Relay_Log_Space: 758
              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
                  Master_UUID: 806aaa66-2b15-11ef-81c0-00163e02ae8d
             Master_Info_File: /www/server/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

其中注意 Slave_IO_Running和 Slave_SQL_Running 必须是 Yes

               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.115.1.104
                  Master_User: yefeng
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: iZuf6j594ao1xc9idlv8pwZ-relay-bin.000004
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes 
            Slave_SQL_Running: Yes

测试从节点自动同步主节点数据

image

posted @ 2024-06-16 00:30  ゐ叶う枫ゆ  阅读(32)  评论(0编辑  收藏  举报