MySQL主从

主从作用和形式

1、作用

实时灾备,用于故障切换
读写分离,提供查询服务
备份,避免影响业务

2、形式

一主一从
主主复制
一主多从—扩展系统读取的性能,因为读是在从库读取的
多主一从—5.7开始支持
联级复制

主从复制步骤

主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
从库生成两个线程,一个I/O线程,一个SQL线程
I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的

主从复制配置

1、时钟同步(两台都需要)

[root@mysql ~]# yum -y install chrony
[root@mysql ~]# systemctl restart chronyd
[root@mysql ~]# systemctl enable chronyd
[root@mysql ~]# timedatectl 
               Local time: Tue 2024-08-06 09:04:43 CST
           Universal time: Tue 2024-08-06 01:04:43 UTC
                 RTC time: Tue 2024-08-06 01:04:43
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

2、查看两个数据库是不是相同

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

3、进入数据库给数据库加上读锁

[root@mysql ~]# mysql -uroot -p
Enter password: 

mysql> flush tables with  read  lock;
Query OK, 0 rows affected (0.00 sec)

4、做完全备份

[root@mysql ~]# mysqldump -uroot -p123456 --all-databases  > /opt/all-123.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

[root@mysql ~]# ll /opt/all-123.sql 
-rw-r--r-- 1 root root 874962 Aug  6 09:17 /opt/all-123.sql

5、将完全备份发个从库

 [root@mysql ~]# scp /opt/all-123.sql root@192.168.100.40:/opt/
The authenticity of host '192.168.100.40 (192.168.100.40)' can't be established.
ED25519 key fingerprint is SHA256:Ci2qzv2Hvt2jld5Q8LBu35qRbAnKzC3EaGZRV6Htsw0.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.100.40' (ED25519) to the list of known hosts.
root@192.168.100.40's password: 
all-123.sql                                                                                       100%  854KB  94.1MB/s   00:00    
[root@mysql ~]#

[root@mysql ~]# ll /opt/
total 860
-rw-r--r-- 1 root  root  874962 Aug  6 09:19 all-123.sql
drwxr-xr-x 5 mysql mysql   4096 Aug  6 08:33 data
drwxr-xr-x 2 root  root      56 Aug  1 02:06 software
[root@mysql ~]# 

6、从库恢复

[root@mysql ~]# mysql -uroot -p123456 < /opt/all-123.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@mysql ~]# 

7、创建账号并设置权限

(master)
mysql>  create  user  'repl'@'192.168.100.40' identified  by  '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication  slave  on  *.* to  'repl'@'192.168.100.40';
Query OK, 0 rows affected (0.00 sec)

mysql> flush  privileges;
Query OK, 0 rows affected (0.00 sec)

8、编写主从库的配置文件

主库
[root@mysql ~]# vim /etc/my.cnf 
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
log-bin = mysql-bin
server-id = 1
symbolic-links = 0
log-error = /var/log/mysqld.log

//重启服务
[root@mysql ~]# systemctl restart mysqld
[root@mysql ~]# ss -anlt
State          Recv-Q         Send-Q                 Local Address:Port                   Peer Address:Port         Process         
LISTEN         0              128                          0.0.0.0:22                          0.0.0.0:*                            
LISTEN         0              80                                 *:3306                              *:*                            
LISTEN         0              128                             [::]:22                             [::]:*  

//查看主库状态
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)



从库
[root@mysql ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id = 2
relay-log = mysql-relay-bin
symbolic-links = 0
log-error = /var/log/mysqld.log

[root@mysql ~]# systemctl restart mysqld
[root@mysql ~]# ss -anlt
State          Recv-Q         Send-Q                 Local Address:Port                   Peer Address:Port         Process         
LISTEN         0              128                          0.0.0.0:22                          0.0.0.0:*                            
LISTEN         0              80                                 *:3306                              *:*                            
LISTEN         0              128                             [::]:22                             [::]:*    

9、配置并启动主从复制

mysql> change  master to  master_host='192.168.100.10',
    -> master_user='repl',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 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.168.100.10
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000007
                Relay_Log_Pos: 367
        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: 154
              Relay_Log_Space: 740
              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: 64fd89fc-4f28-11ef-8583-000c290143a8
             Master_Info_File: /opt/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)

ERROR: 
No query specified

mysql> 

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值