如需转载,标记出处

下载docker镜像

sudo docker image pull mysql:9.1.0

运行 Docker 容器

docker run --name mysql-slave -e MYSQL_ROOT_PASSWORD=mysql -d --network=host -v /home/admin123/mysql_slave/data:/data/mysql -v /home/admin123/mysql_slave/my.cnf:/etc/mysql/my.cnf mysql:9.1.0

使用 docker run 启动一个新的 Docker 容器。为容器指定名称为 mysql-slave,设置 MySQL 的根密码为 mysql,容器将后台运行,使用宿主机的网络模式,并将宿主机的 /home/admin123/mysql_slave/data 目录挂载到容器内的 /data/mysql 目录,同时将宿主机的 MySQL 配置文件 my.cnf 挂载到容器的 /etc/mysql/my.cnf 目录。使用 MySQL 9.1.0 官方 Docker 镜像。

使用 mysqldump 导出数据库

mysqldump --uroot --all-databases --lock-all-tables > ~/master_db.sql

使用 mysqldump 工具导出所有数据库,使用 root 用户进行导出,并锁定所有表以确保备份过程中数据不发生变化。导出的数据会保存到 ~/master_db.sql 文件中。

将备份文件导入到 MySQL 从库

mysql -u root -p -h 127.0.0.1 --port=8306 < ~/master_db.sql

使用 MySQL 客户端工具连接到 MySQL 服务,指定主机为 127.0.0.1,端口为 8306,并将备份文件 ~/master_db.sql 导入到 MySQL 数据库。

在 MySQL 8.0 版本以下配置从库复制权限

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'slave';

使用 GRANT REPLICATION SLAVE 命令授予 slave 用户从库复制的权限,允许该用户从任意 IP 地址连接并设置密码为 slave

MySQL 8.0+ 版本中 GRANT 语法变了,不能再直接用 IDENTIFIED BY 来创建用户并授权。要分开写:

  1. 先创建用户:

    CREATE USER 'slave'@'%' IDENTIFIED BY 'slave';
  2. 再授权:

    GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';
  3. 最后刷新权限:

FLUSH PRIVILEGES;

现在在主服务器上,执行 SHOW MASTER STATUS,查看当前的二进制日志状态

mysql> show master status;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'master status' at line 1

这条命令报错

mysql> select version();
+-----------+
| version() |
+-----------+
| 9.1.0     |
+-----------+
1 row in set (0.00 sec)

我的mysql版本是9.1.0,

SHOW MASTER STATUS 命令已被移除, 取而代之的是 SHOW BINARY LOG STATUS。

mysql> show binary log status;
+---------------+----------+--------------+------------------+-------------------+
​
| File                                                         | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
| ------------------------------------------------------------ | -------- | ------------ | ---------------- | ----------------- |
|                                                              |          |              |                  |                   |
| +---------------+----------+--------------+------------------+-------------------+ |          |              |                  |                   |
| binlog.000002                                                | 158      |              |                  |                   |
| -------------                                                | ----     | ----         | ----             | ----              |
|                                                              |          |              |                  |                   |
| +---------------+----------+--------------+------------------+-------------------+ |          |              |                  |                   |
| 1 row in set (0.00 sec)                                      |          |              |                  |                   |

成功了

同样的还有其他新版本的问题

#从机连接主机

mysql> change master to master_host='127.0.0.1',master_user='slave',master_password='slave',master_log_file='binlog.000002',master_log_pos= 158;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'master to master_host='127.0.0.1',master_user='slave',master_password='slave',ma' at line 1

由于版本问题可以替换成

mysql> CHANGE REPLICATION SOURCE TO
    ->   SOURCE_HOST='127.0.0.1',
    ->   SOURCE_USER='slave',
    ->   SOURCE_PASSWORD='slave',
    ->   SOURCE_LOG_FILE='binlog.000002',
    ->   SOURCE_LOG_POS=158;
Query OK, 0 rows affected, 1 warning (0.02 sec)

#开启从机服务

start slave;

#展示从机服务状态

mysql> show slave status \G ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'slave status' at line 1

MySQL 9.1.0,而这个版本已经彻底淘汰了 SHOW SLAVE STATUS 这种老式写法,改用了新的术语:

SHOW REPLICA STATUS\G

有些同学在这一步就已经可以正常运行了,当然具体问题具体分析,比如我

在检查从数据库的状态时,我遇到的报错:

  Last_IO_Error: Error connecting to source 'slave@127.0.0.1:3306'. This was attempt 10/10, with a delay of 60 seconds between attempts. Message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection

报错分析:

MySQL 8.0 及更高版本引入了 caching_sha2_password 插件,它是默认的身份验证插件。caching_sha2_password 插件要求加密的连接才能进行身份验证(即要求 SSL 或加密连接)

  1. 从数据库连接问题: 从服务器尝试使用 slave@127.0.0.1:3306 连接主服务器,但是由于身份验证方式是 caching_sha2_password,且没有启用安全连接(SSL/TLS),因此认证失败。

说了一堆,就是让两个数据库的配置加密连接上保持一致

我的主数据库配置如下

mysql> SELECT user, host, plugin FROM mysql.user WHERE user = 'slave';
ERROR 2013 (HY000): Lost connection to MySQL server during query
No connection. Trying to reconnect...
Connection id:    8
Current database: NONE 
​
+-------+------+-----------------------+
| user  | host | plugin                |
+-------+------+-----------------------+
| slave | %    | caching_sha2_password |
+-------+------+-----------------------+
1 row in set (0.01 sec)
​
///////////////////////////////////////////////

我的从数据库显示为空

mysql> SELECT user, host, plugin FROM mysql.user WHERE user = 'slave';
Empty set (0.00 sec)

步骤一:编辑主库配置文件 my.cnf

找到主库的配置文件 /etc/my.cnf/etc/mysql/my.cnf,在 [mysqld] 段中加入以下配置:

​
caching_sha2_password_auto_generate_rsa_keys = ON

这条配置会让 MySQL 自动生成服务器的 RSA 密钥文件(通常保存在 /var/lib/mysql 或数据目录中),用于非 SSL 安全连接认证。


步骤二:重启 MySQL 主服务器

​
sudo systemctl restart mysql

步骤三:从库重新配置连接

检查主库的配置文件找到

bind-address=127.0.0.1

如果是其他的ip地址按照你的来写

在主库中再次查二进制日志文件的位置

SHOW BINARY LOG STATUS

(如果文件位置发生改动记得替换下面的SOURCE_LOG_FILE='mysql-bin.000007',SOURCE_LOG_POS=158)

你现在可以在从库中重新执行:

STOP REPLICA;
​
CHANGE REPLICATION SOURCE TO
  SOURCE_HOST='127.0.0.1',
     SOURCE_USER='slave',
      SOURCE_PASSWORD='slave',
      SOURCE_LOG_FILE='mysql-bin.000007',
   SOURCE_LOG_POS=158,
   GET_SOURCE_PUBLIC_KEY = 1;
​
START REPLICA;

GET_SOURCE_PUBLIC_KEY = 1 这一项非常关键,表示从库自动请求主库的 RSA 公钥进行身份验证,这正好适配 caching_sha2_password 的要求。


步骤四:查看状态

SHOW REPLICA STATUS\G

最终成功的结果是这样的

mysql> SHOW REPLICA STATUS\G
*************************** 1. row ***************************
             Replica_IO_State: Waiting for source to send event
                  Source_Host: 127.0.0.1
                  Source_User: slave
                  Source_Port: 3306
                Connect_Retry: 60
              Source_Log_File: mysql-bin.000007
          Read_Source_Log_Pos: 158
               Relay_Log_File: admin-virtual-machine-relay-bin.000002
                Relay_Log_Pos: 328
        Relay_Source_Log_File: mysql-bin.000007
           Replica_IO_Running: Yes
          Replica_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_Source_Log_Pos: 158
              Relay_Log_Space: 555
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Source_SSL_Allowed: No
           Source_SSL_CA_File: 
           Source_SSL_CA_Path: 
              Source_SSL_Cert: 
            Source_SSL_Cipher: 
               Source_SSL_Key: 
        Seconds_Behind_Source: 0
Source_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Source_Server_Id: 1
                  Source_UUID: df8e46c9-d705-11ef-9ad0-000c290e957a
             Source_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
    Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Source_Retry_Count: 10
                  Source_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Source_SSL_Crl: 
           Source_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Source_TLS_Version: 
       Source_public_key_path: 
        Get_Source_public_key: 1
            Network_Namespace: 
1 row in set (0.00 sec)
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐