• mysql 8.0.35 搭建主从


     

    参考文档:
    Replication Not Working in MySQL 8.0. The I/O Thread Cannot Connect, Fails With Error 2061 (Doc ID 2423671.1)

    -- 安装mysql8.0.35 ,解压tar包后,直接初始化(略)

    1. mysql> select version();
    2. +-----------+
    3. | version() |
    4. +-----------+
    5. | 8.0.35 |
    6. +-----------+
    7. 1 row in set (0.00 sec)
    8. mysql>


    -- 创建复制账号,所有的库上。可以使用两种方式创建账号,

    1. --create user 'rep1'@'192.168.2.%' identified by 'mysql'; <<<<< 这种方式创建的账号,会报Error_code: MY-002061
    2. CREATE USER 'rep1'@'192.168.2.%' IDENTIFIED WITH 'mysql_native_password' BY 'mysql'; <<<<< 这种方式创建的账号,不会报Error_code: MY-002061
    3. grant replication slave on *.* to 'rep1'@'192.168.2.%';
    4. flush privileges;

    -- conf文件 这里的conf文件,仅仅为搭建主从的最小最少参数

    1. [mysqld]
    2. server_id=3306
    3. admin_port=33062
    4. mysqlx_port=33060
    5. socket=/mysql/mysql3306/mysql3306.sock
    6. mysqlx_socket=/mysql/mysql3306/mysql3306x.sock
    7. log-error=/mysql/mysql3306/data/error.log
    8. port=3306
    9. character_set_server=utf8mb4
    10. basedir=/mysql/mysql3306
    11. datadir=/mysql/mysql3306/data
    12. log_bin=binlog
    13. log_bin_index=/mysql/mysql3306/data/binlog.index
    14. gtid_mode=on
    15. enforce_gtid_consistency=true
    16. binlog-ignore-db = mysql
    17. binlog-ignore-db = information_schema
    18. binlog-ignore-db = performance_schema
    19. binlog-ignore-db = sys
    1. [mysqld]
    2. server_id=3307
    3. admin_port=33072
    4. mysqlx_port=33070
    5. socket=/mysql/mysql3307/mysql3307.sock
    6. mysqlx_socket=/mysql/mysql3307/mysql3307x.sock
    7. log-error=/mysql/mysql3307/data/error.log
    8. log_bin=binlog
    9. log_bin_index=/mysql/mysql3307/data/binlog.index
    10. port=3307
    11. character_set_server=utf8mb4
    12. basedir=/mysql/mysql3307
    13. datadir=/mysql/mysql3307/data
    14. socket=/tmp/mysql.sock
    15. gtid_mode=on
    16. enforce-gtid-consistency=true
    17. relay-log-index=slave-relay-bin.index
    18. relay-log=slave-relay-bin
    19. binlog-ignore-db = mysql
    20. binlog-ignore-db = information_schema
    21. binlog-ignore-db = performance_schema
    22. binlog-ignore-db = sys

    -- 开启半同步 (8.0和5.7的半同步插件,不一样。安装5.7的插件也可以,log中有提示,即将被淘汰)

    1. --INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
    2. --SET GLOBAL rpl_semi_sync_master_enabled=ON;
    3. install plugin rpl_semi_sync_source soname 'semisync_source.so';
    4. set global rpl_semi_sync_source_enabled=1;

    -- 备份主库

    mysqldump --source-data=2 --single-transaction -uroot -h127.0.0.1 -p -P3306 --databases test test_db  >mysqldump_`date +%Y%m%d`.sql

    --开启主从同步
    -- CHANGE MASTER TO MASTER_LOG_FILE='on.000008', MASTER_LOG_POS=1268;

    1. change master to master_host='192.168.2.154',
    2. master_port=3306,
    3. master_user='rep1',
    4. master_password='mysql',
    5. master_log_file='on.000008',
    6. master_log_pos=1268;
    7. start slave;
    8. show slave status \G;

    -- 出现的错误

    1. mysql> show slave status\G;
    2. *************************** 1. row ***************************
    3. Slave_IO_State: Connecting to source
    4. Master_Host: 192.168.2.154
    5. Master_User: rep1
    6. Master_Port: 3306
    7. Connect_Retry: 60
    8. Master_Log_File: on.000008
    9. Read_Master_Log_Pos: 1268
    10. Relay_Log_File: slave-relay-bin.000001
    11. Relay_Log_Pos: 4
    12. Relay_Master_Log_File: on.000008
    13. Slave_IO_Running: Connecting
    14. Slave_SQL_Running: Yes
    15. Replicate_Do_DB:

    -- log中的错误 

    1. 2023-11-07T02:51:59.855154Z 8 [System] [MY-010597] [Repl] 'CHANGE REPLICATION SOURCE TO FOR CHANNEL '' executed'. Previous state source_host='', source_port= 3306, source_log_file='', source_log_pos= 4, source_bind=''. New state source_host='192.168.2.154', source_port= 3306, source_log_file='on.000008', source_log_pos= 1268, source_bind=''.
    2. 2023-11-07T02:52:30.166311Z 9 [Warning] [MY-010897] [Repl] Storing MySQL user name or password information in the connection metadata repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START REPLICA; see the 'START REPLICA Syntax' in the MySQL Manual for more information.
    3. 2023-11-07T02:52:45.230203Z 9 [ERROR] [MY-010584] [Repl] Replica I/O for channel '': Error connecting to source 'rep1@192.168.2.154:3306'. This was attempt 1/86400, with a delay of 60 seconds between attempts. Message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. Error_code: MY-002061
    4. 2023-11-07T02:53:45.231503Z 9 [ERROR] [MY-010584] [Repl] Replica I/O for channel '': Error connecting to source 'rep1@192.168.2.154:3306'. This was attempt 2/86400, with a delay of 60 seconds between attempts. Message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. Error_code: MY-002061

    -- 查看当前的默认认证插件,为caching_sha2_password

    1. +-------------------------------+-----------------------+
    2. | Variable_name | Value |
    3. +-------------------------------+-----------------------+
    4. | default_authentication_plugin | caching_sha2_password |
    5. +-------------------------------+-----------------------+
    6. 1 row in set (0.03 sec)
    7. mysql>

    --参数中修改为mysql_native_password,不行      

    default_authentication_plugin=mysql_native_password

    方法1 
    --drop 掉用户,重新创建,加上WITH 'mysql_native_password'即可

    1. drop user 'rep1'@'192.168.2.%'
    2. CREATE USER 'rep1'@'192.168.2.%' IDENTIFIED WITH 'mysql_native_password' BY 'mysql';
    3. mysql> select host,user,plugin from mysql.user;
    4. +-------------+------------------+-----------------------+
    5. | host | user | plugin |
    6. +-------------+------------------+-----------------------+
    7. | 192.168.2.% | rep1 | mysql_native_password |
    8. | 192.168.2.% | root | caching_sha2_password |
    9. | localhost | mysql.infoschema | caching_sha2_password |
    10. | localhost | mysql.session | caching_sha2_password |
    11. | localhost | mysql.sys | caching_sha2_password |
    12. | localhost | root | caching_sha2_password |
    13. +-------------+------------------+-----------------------+
    14. 6 rows in set (0.00 sec)
    15. mysql>

    -- 方法2 
    创建用户,需要修改一些SSL及public key

    1. CREATE USER 'rep1'@'192.168.2.%' IDENTIFIED BY 'mysql';
    2. mysql> select host,user,plugin from mysql.user;
    3. +-------------+------------------+-----------------------+
    4. | host | user | plugin |
    5. +-------------+------------------+-----------------------+
    6. | 192.168.2.% | rep1 | caching_sha2_password |
    7. | 192.168.2.% | root | caching_sha2_password |
    8. | localhost | mysql.infoschema | caching_sha2_password |
    9. | localhost | mysql.session | caching_sha2_password |
    10. | localhost | mysql.sys | caching_sha2_password |
    11. | localhost | root | caching_sha2_password |
    12. +-------------+------------------+-----------------------+
    13. 6 rows in set (0.00 sec)
    14. mysql>

    启用SSL

    1. stop replica;
    2. CHANGE REPLICATION SOURCE TO SOURCE_SSL=1;
    3. START REPLICA;

    将public_key.pem从主端复制到从端

    1. STOP REPLICA;
    2. CHANGE REPLICATION SOURCE TO SOURCE_PUBLIC_KEY_PATH='/path/public_key.pem';
    3. START REPLICA;
    4. SHOW REPLICA STATUS\G

    -- 查看从库复制状态,可以看到Source_SSL_Allowed、Source_public_key_path等

    1. mysql> show replica status \G;
    2. *************************** 1. row ***************************
    3. Replica_IO_State: Waiting for source to send event
    4. Source_Host: 192.168.2.154
    5. Source_User: rep1
    6. Source_Port: 3306
    7. Connect_Retry: 60
    8. Source_Log_File: binlog.000023
    9. Read_Source_Log_Pos: 197
    10. Relay_Log_File: slave-relay-bin.000006
    11. Relay_Log_Pos: 367
    12. Relay_Source_Log_File: binlog.000023
    13. Replica_IO_Running: Yes
    14. Replica_SQL_Running: Yes
    15. Replicate_Do_DB:
    16. Until_Condition: None
    17. Until_Log_File:
    18. Until_Log_Pos: 0
    19. Source_SSL_Allowed: Yes
    20. Source_SSL_CA_File:
    21. Source_TLS_Version:
    22. Source_public_key_path: /mysql/mysql3307/data/public_key.pem
    23. Get_Source_public_key: 0
    24. Network_Namespace:
    25. 1 row in set (0.00 sec)

    END 

  • 相关阅读:
    Unity中集合的随机数(指定长度—List、Dictionary)
    ansible 使用roles简单部署LAMP平台
    java毕业设计大众点评管理系统Mybatis+系统+数据库+调试部署
    力扣459. 重复的子字符串(kmp算法)
    JAVA计算机毕业设计书籍影视评论系统(附源码、数据库)
    【JAVA学习笔记】67 - 坦克大战1.5 - 1.6,防止重叠,记录成绩,选择是否开新游戏或上局游戏,播放游戏音乐
    想要精通算法和SQL的成长之路 - 打家劫舍系列问题
    5. 最长回文子串
    【计组】指令和运算1--《深入浅出计算机组成原理》(二)
    RabbitMQ —— 理解及应用场景
  • 原文地址:https://blog.csdn.net/xxzhaobb/article/details/134287121