【MySQL】主从配置

 

 

参考博客:

https://www.cnblogs.com/lelehellow/p/9633315.html

 

正好借着3台机器,可以做一个 1主 2从的分配

192.168.101.12 centos7-02
192.168.101.12 centos7-03
192.168.101.12 centos7-04

3台机器都已经安装好RPM版的8.0.27

 

主库需要做的事情是

1、开启Binlog日志

1.5、设置服务ID

2、分配从库的监听账号

3、拿到binlog文件名

首先找到配置文件:

[root@centos7-03 ~]# find / -name my.cnf
/etc/my.cnf

配置这两个参数项:

[mysqld]
# 开启二进制日志 log-bin=mysql-bin # 设置server-id server-id=1

保存后重启mysqld服务,生效配置

systemctl restart mysqld

 

然后登陆上主库,分配从库的账号及其权限

-- 创建两个从库的监听账号
CREATE USER 'SLAVE-01'@'centos7-03' IDENTIFIED BY '123456';
CREATE USER 'SLAVE-02'@'centos7-04' IDENTIFIED BY '123456';

-- 分配分配【从库副本权限】给监听账号
GRANT REPLICATION SLAVE ON *.* TO 'SLAVE-01'@'centos7-03';
GRANT REPLICATION SLAVE ON *.* TO 'SLAVE-02'@'centos7-04';

-- 刷新权限
FLUSH PRIVILEGES;

 

获取主库的bin日志文件

SHOW MASTER STATUS;

展示信息:

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      156 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.02 sec)

 

然后从库要和主库一样设置服务ID,为了区分MySQL

# centos7-03 机器 设置
vim /etc/my.cnf
server-id=2

# centos7-04 机器 设置
vim /etc/my.cnf
server-id=3 

# 然后都重启服务
systemctl restart mysqld

 

登陆上从库的MySQL,执行绑定SQL:

-- centos7-03的mysql执行
CHANGE MASTER TO
   -- 主库连接信息
    MASTER_PORT= 3306, -- 主库端口
    MASTER_HOST='centos7-02', -- 主库地址
    MASTER_USER='SLAVE-01', -- 主库提供的同步账号
    MASTER_PASSWORD='123456', -- 主库提供的同步账号密码
        
   -- 主库的 SHOW MASTER STATUS 展示的信息
    MASTER_LOG_FILE='mysql-bin.000001', -- 主库的日志文件名称
    MASTER_LOG_POS=156; -- 主库的日志文件最后位置6


-- centos7-04的mysql执行
CHANGE MASTER TO
   -- 主库连接信息
    MASTER_PORT= 3306, -- 主库端口
    MASTER_HOST='centos7-02', -- 主库地址
    MASTER_USER='SLAVE-02', -- 主库提供的同步账号
    MASTER_PASSWORD='123456', -- 主库提供的同步账号密码
        
   -- 主库的 SHOW MASTER STATUS 展示的信息
    MASTER_LOG_FILE='mysql-bin.000001', -- 主库的日志文件名称
    MASTER_LOG_POS=156; -- 主库的日志文件最后位置6

然后开启从库同步:

START SLAVE;

 

关闭从库同步:

STOP SLAVE;

 

查看从库同步状态:

SHOW SLAVE STATUS;
注意两个参数 Slave_IO_Running & Slave_SQL_Running 值都为Yes的时候,才算主从配置成功
mysql> SHOW SLAVE STATUS;
+----------------------+-------------+-------------+-------------+---------------+------------------+---------------------+-----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------+-----------------------------+------------------+-------------+-------------------------+-----------+---------------------+----------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+
| Slave_IO_State       | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File  | Read_Master_Log_Pos | Relay_Log_File              | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error                                                                                                                                                                                        | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File        | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                  | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version | Master_public_key_path | Get_master_public_key | Network_Namespace |
+----------------------+-------------+-------------+-------------+---------------+------------------+---------------------+-----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------+-----------------------------+------------------+-------------+-------------------------+-----------+---------------------+----------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+
| Connecting to source | centos7-02  | SLAVE-02    |        3306 |            60 | mysql-bin.000001 |                 156 | centos7-04-relay-bin.000001 |             4 | mysql-bin.000001      | Connecting       | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 156 |             156 | None            |                |             0 | No                 |                    |                    |                 |                   |                | NULL                  | No                            |          2061 | error connecting to master 'SLAVE-02@centos7-02:3306' - retry-time: 60 retries: 31 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. |              0 |                |                             |                0 |             | mysql.slave_master_info |         0 | NULL                | Replica has read all relay log; waiting for more updates |              86400 |             | 220201 18:09:21         |                          |                |                    |                    |                   |             0 |                      |              |                    |                        |                     0 |                   |
+----------------------+-------------+-------------+-------------+---------------+------------------+---------------------+-----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------+-----------------------------+------------------+-------------+-------------------------+-----------+---------------------+----------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+
1 row in set (0.04 sec)

 

报错原因在 【Last_IO_Error】

error connecting to master 'SLAVE-02@centos7-02:3306' - retry-time: 60 retries: 31 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.

连接要求密码配有加密插件

回到主库更改账号的密码插件项:

ALTER USER 'SLAVE-01'@'centos7-03' IDENTIFIED WITH mysql_native_password BY '123456';
ALTER USER 'SLAVE-02'@'centos7-04' IDENTIFIED WITH mysql_native_password BY '123456';
FLUSH PRIVILEGES;
UNLOCK TABLES;

 

停止从库绑定:

STOP SLAVE;

重置从库

RESET SLAVE;

再检查主库binlog的pos值

SHOW MASTER STATUS;

然后重新配置SLAVE,再启动即可

 

上一篇:2022.1.23


下一篇:mybatisplus手动切换数据源