• Linux——mysql主从复制配置


    主从复制配置

    主服务器配置:

    追加信息
    [root@localhost ~]# vim /etc/my.cnf
    	[mysqld]
    	server-id=11111
    	log-bin=mysql-bin
    重启服务:
    [root@localhost ~]# systemctl restart mysqld.service 
    进入mysql进行用户创建并给予相应权限:
    mysql> create user 'qyx'@'%' identified with mysql_native_password by '054422';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> grant replication slave on *.* to 'qyx'@'%';
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    MySQL8中的默认认证插件是caching_sha2_password,该认证插件会导致从库连接不上主库,所以主库的用户创建时必须制定插件为mysql_native_password

    从服务器上配置:

    对从服务器进行配置,server-id必须和主服务器不同

    追加信息
    [root@localhost ~]# vim /etc/my.cnf
    	[mysqld]
    	server-id=22222       
    	log-bin=mysql-bin
    重启服务:
    [root@localhost ~]# systemctl restart mysqld.service 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    主服务器上:

    查询mysql主服务器信息:
    mysql> show master status;
    +------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                                                                   |
    +------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------+
    | mysql-bin.000008 |      195 |              |                  | 0a60a26a-15fe-11ed-8d62-000c29335a5f:1-5,
    853809b3-0322-11ed-96ea-000c29335a5f:1-19 |
    +------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    从服务器上配置:

    配置主服务器的信息,master_log_pos为上面查询到的主服务器的position,master_log_file为上方查到的日志名(以防主从不同步)

    mysql> stop slave;
    
    
    mysql> change master to master_auto_position=0;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> change master to master_host='192.168.15.128',
        -> master_port=3306,
        -> master_user='qyx',
        -> master_password='054422',
        -> master_log_pos=195,
        -> master_log_file='myql-bin.000008';
    Query OK, 0 rows affected, 2 warnings (0.01 sec)
    mysql> start slave;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    从服务器上:

    mysql> show slave status\G;                                                   
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.15.128
                      Master_User: qyx
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000008
              Read_Master_Log_Pos: 195
                   Relay_Log_File: localhost-relay-bin.000002
                    Relay_Log_Pos: 322
            Relay_Master_Log_File: mysql-bin.000008
                 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: 195
                  Relay_Log_Space: 534
                  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: 11111
                      Master_UUID: 853809b3-0322-11ed-96ea-000c29335a5f
                 Master_Info_File: mysql.slave_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: 64fdcca9-16be-11ed-b56a-000c299be67d:1-2,
    853809b3-0322-11ed-96ea-000c29335a5f:1-16
                    Auto_Position: 0
             Replicate_Rewrite_DB: 
                     Channel_Name: 
               Master_TLS_Version: 
           Master_public_key_path: 
            Get_master_public_key: 0
                Network_Namespace: 
    1 row in set (0.00 sec)
    
    ERROR: 
    No query specified
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    出现以下信息才成功:

    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes

    ​ Last_IO_Errno: 0

    ​ Last_SQL_Errno: 0

    若错误提示有连个mysql具有相同的uuid。

    则删除从服务器上的/etc/my.cnf文件再重新启动mysqld服务

    查看从服务器上的通用日志,若没有打开,则打开

    mysql> show variables like '%general%';
    +------------------+------------------------------+
    | Variable_name    | Value                        |
    +------------------+------------------------------+
    | general_log      | OFF                          |
    | general_log_file | /var/lib/mysql/localhost.log |
    +------------------+------------------------------+
    2 rows in set (0.00 sec)
    
    mysql> set @@global.general_log=on;
    Query OK, 0 rows affected (0.01 sec)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    从服务器上追踪通用日志:

    [root@localhost my.cnf.d]# tail -f /var/lib/mysql/localhost.log
    /usr/libexec/mysqld, Version: 8.0.17 (Source distribution). started with:
    Tcp port: 3306  Unix socket: /var/lib/mysql/mysql.sock
    Time                 Id Command    Argument
    2022-08-08T03:19:52.963828Z	    8 Query	show variables like '%general%'
    2022-08-08T03:45:55.907963Z	    8 Quit	
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    主服务器上创建数据库

    mysql> create database nh;
    Query OK, 1 row affected (0.02 sec)
    
    • 1
    • 2

    从服务器上出现追踪信息:

    [root@localhost my.cnf.d]# tail -f /var/lib/mysql/localhost.log
    /usr/libexec/mysqld, Version: 8.0.17 (Source distribution). started with:
    Tcp port: 3306  Unix socket: /var/lib/mysql/mysql.sock
    Time                 Id Command    Argument
    2022-08-08T03:19:52.963828Z	    8 Query	show variables like '%general%'
    2022-08-08T03:45:55.907963Z	    8 Quit	
    2022-08-08T03:48:50.370896Z	   18 Query	create database nh
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    进入从服务器进行认证,数据库复制成功:

    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | db3                |
    | example            |
    | he                 |
    | information_schema |
    | myemployees        |
    | mysql              |
    | nextcloud          |
    | nh                 |
    | performance_schema |
    | sys                |
    +--------------------+
    10 rows in set (0.03 sec)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    Python数据分析之numpy(保姆级教程)
    淘宝Tmall,1688,拼多多API商品详情接口
    英语读书笔记-Book Lovers Day 09
    编译使用Aws-cpp-sdk API
    C语言—程序环境和预处理
    60行PHP代码实现值班信息查询带万年历
    数据结构体--5.0图
    mybatis-plus使用总结
    技术人应该广度还是深度学习?
    搞脑筋的日历积木
  • 原文地址:https://blog.csdn.net/qq_53715074/article/details/126224980