Centos7 MySql8 集群1主3从搭建

安装参考:Centos7 MySql8安装 

主从配置和遇到的坑以及爬坑方案:

一.主库配置:192.168.0.1
1.修改my.cnf
vi /etc/my.cnf
加入下面的配置:
#主从复制配置
#[必须]设置主服务器唯一ID,默认是1,一般取IP最后一段,但是要保证和slave的id不一样
server-id = 118
#[必须]启用二进制日志
log-bin=master-bin
log-bin-index=master-bin.index
#若涉及及同步函数或者存储过程需要配置,否则主备会产生异常不能同步  
log_bin_trust_function_creators=TRUE

重启MySQL:
systemctl restart mysqld

2.登录数据库,密码为root@2021
mysql -uroot -p
3.在主库的服务器上,给从库分配权限(主服务器新建用户并赋予“REPLICATION SLAVE”的权限)。
在Master库创建一个账号,具备Replication Slave权限,提供给Slave库访问二进制日志使用。
mysql> CREATE USER 'slaveuser'@'%' IDENTIFIED BY 'root@2021';
Query OK, 0 rows affected (0.03 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slaveuser'@'%';
Query OK, 0 rows affected (0.00 sec)
刷新权限:
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
查看Master状态:
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |      865 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

4.查看master状态,到这里master的配置已经完成。
mysql> show master status \G;
*************************** 1. row ***************************
             File: master-bin.000001
         Position: 865
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

ERROR:
No query specified

二.从库配置:192.168.0.2
1.修改my.cnf
vi /etc/my.cnf

#主从复制配置[从库]
#[必须]设置从服务器唯一ID,要保证ID于其他主从ID不一样
server_id =122
relay-log=master-bin

2.重启MySQL:
systemctl restart mysqld

3.登录mysql并配置从服务器,设置从服务器对主服务器的监听,密码为root@2021
mysql -uroot -p
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.1',MASTER_USER='slaveuser',MASTER_PASSWORD='root@2021',MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS=865,Master_Port=3306;

说明:
MASTER_HOST:master的ip或虚ip或域名
MASTER_USER:master上之前创建的登录账户名
MASTER_PASSWORD:创建的登录账户密码
MASTER_LOG_FILE:master的file
MASTER_LOG_POS:master的position
4.刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
5.启动从服务器复制功能
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.04 sec)

6.查看从服务器的状态
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.1
                  Master_User: slaveuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 1363
               Relay_Log_File: master-bin.000004
                Relay_Log_Pos: 325
        Relay_Master_Log_File: master-bin.000001
             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: 1363
              Relay_Log_Space: 1196
              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: 118
                  Master_UUID: cca01ee8-5b91-11e3-99c3-2c44fd932330
             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:
                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, 1 warning (0.01 sec)

ERROR:
No query specified

如果
Slave_IO_Running:Yes
Slave_SQL_Running: Yes
说明我们的从服务器配置成功,主从复制的配置都已完成。

三.从库配置:192.168.0.3
1.修改my.cnf
vi /etc/my.cnf

#主从复制配置[从库]
#[必须]设置从服务器唯一ID,要保证ID于其他主从ID不一样
server_id =123
relay-log=master-bin

2.重启MySQL:
systemctl restart mysqld

3.登录mysql并配置从服务器,设置从服务器对主服务器的监听,密码为root@2021
mysql -uroot -p
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.1',MASTER_USER='slaveuser',MASTER_PASSWORD='root@2021',MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS=865,Master_Port=3306;
Query OK, 0 rows affected, 2 warnings (0.29 sec)
创建用户:
mysql> CREATE USER 'slaveuser'@'%' IDENTIFIED WITH mysql_native_password BY 'root@2021';
Query OK, 0 rows affected (0.06 sec)
说明:
MASTER_HOST:master的ip或虚ip或域名
MASTER_USER:master上之前创建的登录账户名
MASTER_PASSWORD:创建的登录账户密码
MASTER_LOG_FILE:master的file
MASTER_LOG_POS:master的position
4.刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
5.启动从服务器复制功能
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.04 sec)

6.查看从服务器的状态
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.1
                  Master_User: slaveuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 1363
               Relay_Log_File: master-bin.000002
                Relay_Log_Pos: 823
        Relay_Master_Log_File: master-bin.000001
             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: 1363
              Relay_Log_Space: 1027
              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: 118
                  Master_UUID: cca01ee8-5b91-11e3-99c3-2c44fd932330
             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:
                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, 1 warning (0.01 sec)

ERROR:
No query specified

如果
Slave_IO_Running:Yes
Slave_SQL_Running: Yes
说明我们的从服务器配置成功,主从复制的配置都已完成。

###问题1,修改了默认端口问题:https://blog.csdn.net/wuhaotian1996/article/details/86714340
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.1',MASTER_USER='slaveuser',MASTER_PASSWORD='root@2021',MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS=865,Master_Port=3306;
解决:停止从库
mysql> STOP SLAVE;
Query OK, 0 rows affected, 1 warning (0.05 sec)
重新授权,成功
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.1',MASTER_USER='slaveuser',MASTER_PASSWORD='root@2021',MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS=865,Master_Port=3306;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
打开从库
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

###问题2,密码加密方式问题:https://www.cnblogs.com/ryxiong-blog/p/12516875.html
 Last_IO_Errno: 2061
 Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60 retries: 1 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
解决:
停止从库:
mysql> STOP SLAVE;
Query OK, 0 rows affected, 1 warning (0.05 sec)
###修改主库加密方式:###
mysql> ALTER USER 'slaveuser'@'%' IDENTIFIED WITH mysql_native_password BY 'root@2021';
Query OK, 0 rows affected (0.01 sec)
重启slave
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

###问题3,从库无法连接主库:
Last_SQL_Errno: 1396
Last_SQL_Error: Error 'Operation ALTER USER failed for 'slaveuser'@'%'' on query. Default database: ''. Query: 'ALTER USER 'slaveuser'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*B4904D15E9FA44C4732642F8913A2EEC0E80B3AC''
解决:https://blog.csdn.net/hehuyi_in/article/details/102917165
在从库创建用户并授权
mysql> CREATE USER 'slaveuser'@'%' IDENTIFIED WITH mysql_native_password BY 'root@2021';
Query OK, 0 rows affected (0.06 sec)
刷新权限:
mysql> flush privileges;
还需要重新启动一下同步,再进行检查
mysql> stop slave;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.1
                  Master_User: slaveuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 1363
               Relay_Log_File: master-bin.000004
                Relay_Log_Pos: 325
        Relay_Master_Log_File: master-bin.000001
             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: 1363
              Relay_Log_Space: 1196
              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: 118
                  Master_UUID: cca01ee8-5b91-11e3-99c3-2c44fd932330
             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:
                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, 1 warning (0.01 sec)

ERROR:
No query specified

完成:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

###问题4,无法连接主库:
Last_IO_Errno: 2003
Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60 retries: 3 message: Can't connect to MySQL server on '192.168.0.1' (111)
解决:
mysql> stop slave;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.1',MASTER_USER='slaveuser',MASTER_PASSWORD='root@2021',MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS=865,Master_Port=3306;
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.1
                  Master_User: slaveuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 1363
               Relay_Log_File: master-bin.000002
                Relay_Log_Pos: 823
        Relay_Master_Log_File: master-bin.000001
             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: 1363
              Relay_Log_Space: 1027
              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: 118
                  Master_UUID: cca01ee8-5b91-11e3-99c3-2c44fd932330
             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:
                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, 1 warning (0.01 sec)

ERROR:
No query specified

问题5:ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
解决:
1、 修改用户密码
mysql> alter user 'root'@'localhost' identified by 'root@2021';
或者       
mysql> set password=password("root@2021");
2、刷新权限
mysql> flush privileges;

问题6:1146
Last_SQL_Errno: 1146
Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: 
Worker 16 failed executing transaction 'cca01ee8-5b91-11e3-99c3-2c44fd932330:5901' at master log master-bin.000014, 
end_log_pos 1237614. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
解决:
mysql> reset slave all;
ERROR 3081 (HY000): This operation cannot be performed with running replication threads; run STOP SLAVE FOR CHANNEL 'master_3' first
mysql> stop slave;
mysql> CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000014', MASTER_LOG_POS=20057536;
ERROR 3079 (HY000): Multiple channels exist on the slave. Please provide channel name as an argument.
mysql> reset slave all;
mysql> stop slave;
mysql> change master to master_auto_position=0;
mysql> stop  slave;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.1',MASTER_USER='slaveuser',MASTER_PASSWORD='root@2021',MASTER_LOG_FILE='master-bin.000014', MASTER_LOG_POS=20062462,Master_Port=3306;
mysql> start slave;
mysql> show slave status\G;

问题7:1051
Master_Log_File: master-bin.000014
Read_Master_Log_Pos: 18969343
Relay_Log_File: master-bin-master_1.000038
Relay_Log_Pos: 942126
Relay_Master_Log_File: master-bin.000014
Slave_IO_Running: Yes
Slave_SQL_Running: No
Last_SQL_Errno: 1051
Last_SQL_Error: Error 'Unknown table 'ad_base.t,ad_base.test1'' on query. Default database: 'ad_base'. Query: 'DROP TABLE `t`,`test1` /* generated by server */'

解决:https://www.codenong.com/cs109650301/
原因:sql_thread执行relaylog时,发现从库并没有test.t2表。
场景:主库已经把库表删除了,从库已经早一步删除了对应的库表。可以跳过这个事物,保持主从复制正常和主从数据一致
操作:http://blog.chinaunix.net/uid-30234421-id-5761157.html
stop  slave;
reset slave;
start slave;
show slave status\G;

问题8:13114
Last_IO_Errno: 13114
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Cannot replicate anonymo
us transaction when @@GLOBAL.GTID_MODE = ON, at file ./master-bin.000004, position 156.; the first event '' at 4, the last
event read from './master-bin.000004' at 235, the last byte read from './master-bin.000004' at 235.'
解决:
https://blog.csdn.net/qq_42395490/article/details/107451996
https://blog.csdn.net/eagle89/article/details/104950222
stop slave;
CHANGE MASTER TO MASTER_HOST='192.168.0.1',MASTER_USER='slaveuser',MASTER_PASSWORD='root@2021',MASTER_LOG_FILE='master-bin.000014', MASTER_LOG_POS=20986469,Master_Port=3306;

stop  slave;
CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000014', MASTER_LOG_POS=20986469;
start slave;
show slave status\G;

问题9:ERROR 3079 (HY000): Multiple channels exist on the slave. Please provide channel name as an argument.
reset slave all;
stop slave;
change master to master_auto_position=0;
stop  slave;
CHANGE MASTER TO MASTER_HOST='192.168.0.1',MASTER_USER='slaveuser',MASTER_PASSWORD='root@2021',MASTER_LOG_FILE='master-bin.000014', MASTER_LOG_POS=20986469,Master_Port=3306;
start slave;
show slave status\G;

问题10:
Last_SQL_Errno: 1032
Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: 
Worker 16 failed executing transaction 'cca01ee8-5b91-11e3-99c3-2c44fd932330:39043' at master log master-bin.000014, end_log_pos 20042727. 
See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.
解决:
select * from performance_schema.replication_applier_status_by_worker\G;
从数据恢复一致后需要在slave上跳过报错的事务
在从库中执行
Stop slave;
Set @@SESSION.GTID_NEXT='cca01ee8-5b91-11e3-99c3-2c44fd932330:39584'
Begin;
Commit;
Set @@SESSION.GTID_NEXT = AUTOMATIC;
Start slave;
Show slave status\G;
再次查看从库状态,恢复正常即解决

问题11:
Last_SQL_Errno: 1062
Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: 
Worker 16 failed executing transaction 'cca01ee8-5b91-11e3-99c3-2c44fd932330:39242' at master log master-bin.000014, end_log_pos 20151378. 
See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any.

问题12:
Last_SQL_Errno: 1032
Last_SQL_Error: Could not execute Delete_rows event on table test01.b1; Can't find record in 'b1', Error_code: 1032; 
handler error HA_ERR_KEY_NOT_FOUND; the event's master log master-bin.000014, end_log_pos 20675809
解决:(万能解决方法,但是不推荐)
stop  slave;
reset slave;
start slave;
show slave status\G;
stop slave;
CHANGE MASTER TO MASTER_HOST='192.168.0.1',MASTER_USER='slaveuser',MASTER_PASSWORD='root@2021',MASTER_LOG_FILE='master-bin.000014', MASTER_LOG_POS=20986469,Master_Port=3306;
stop  slave;
CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000014', MASTER_LOG_POS=20986469;
start slave;
show slave status\G;

Last_SQL_Errno: 1062
Last_SQL_Error: Could not execute Write_rows event on table test01.b1; Duplicate entry '0' for key 'b1.PRIMARY', Error_code: 1062; 
handler error HA_ERR_FOUND_DUPP_KEY; the event's master log master-bin.000014, end_log_pos 20151378

 

おすすめ

転載: blog.csdn.net/JavaAlpha/article/details/112480559