一、背景:

在业务生产环境维护过程中会出现k8s集群下mysql容器需要更换pvc存储的场景,更换pvc存储就必须得做数据迁移,不然会出现业务数据丢失的情况。实施的方案主要有两种:

方案一:
1、将需要迁移的mysql容器里的数据通过mysqldump的方式导出来;
2、然后更换pvc存储,然后通过source 数据文件.sql的方式导入到新的数据库中;
方案二:

1、先新建pvc存储,然后挂载到mysql容器中的另一个目录上;
2、然后在容器里将mysql的数据复制到新的pvc存储挂载的目录上,修改权限和属主;

3、替换旧的pvc存储,然后mysql容器;

二、实施mysql容器数据迁移:

这里主要选择是方案二的操作。

1、创建新的pvc存储:

2、在mysql容器上挂载新pvc存储:

 

这里是将新pvc存储挂载mysql容器的/mnt目录下,记得生效容器配置。 

3、登入到mysql容器里cp迁移数据:

 cd到mysql存放数据的目录,这里默认的是/var/lib/mysql的

root@mysql-ozthj-b7b7cb9cd-m8hr4:/# cd /var/lib/mysql
root@mysql-ozthj-b7b7cb9cd-m8hr4:/var/lib/mysql# ls
auto.cnf    ca.pem           client-key.pem  ib_logfile0  ibdata1  mysql               private_key.pem  server-cert.pem  sys
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile1  ibtmp1   performance_schema  public_key.pem   server-key.pem   xxl_job

然后通过cp命令将/var/lib/mysql目录下的所有数据复制到/mnt/目录下,并设置属主: 

root@mysql-ozthj-b7b7cb9cd-m8hr4:/var/lib/mysql# ls
auto.cnf    ca.pem           client-key.pem  ib_logfile0  ibdata1  mysql               private_key.pem  server-cert.pem  sys
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile1  ibtmp1   performance_schema  public_key.pem   server-key.pem   xxl_job
root@mysql-ozthj-b7b7cb9cd-m8hr4:/var/lib/mysql# cp -r * /mnt/
root@mysql-ozthj-b7b7cb9cd-m8hr4:/var/lib/mysql# cd /mnt
root@mysql-ozthj-b7b7cb9cd-m8hr4:/mnt# ls
auto.cnf    ca.pem           client-key.pem  ib_logfile0  ibdata1  lost+found  performance_schema  public_key.pem   server-key.pem  xxl_job
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile1  ibtmp1   mysql       private_key.pem     server-cert.pem  sys
root@mysql-ozthj-b7b7cb9cd-m8hr4:/mnt# chown -R mysql:mysql *
root@mysql-ozthj-b7b7cb9cd-m8hr4:/mnt# ls -l
total 188496
-rw-r----- 1 mysql mysql       56 Jan 31 03:07 auto.cnf
-rw------- 1 mysql mysql     1676 Jan 31 03:07 ca-key.pem
-rw-r--r-- 1 mysql mysql     1112 Jan 31 03:07 ca.pem
-rw-r--r-- 1 mysql mysql     1112 Jan 31 03:07 client-cert.pem
-rw------- 1 mysql mysql     1680 Jan 31 03:07 client-key.pem
-rw-r----- 1 mysql mysql      678 Jan 31 03:07 ib_buffer_pool
-rw-r----- 1 mysql mysql 50331648 Jan 31 03:07 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Jan 31 03:07 ib_logfile1
-rw-r----- 1 mysql mysql 79691776 Jan 31 03:07 ibdata1
-rw-r----- 1 mysql mysql 12582912 Jan 31 03:07 ibtmp1
drwx------ 2 mysql mysql    16384 Jan 31 03:02 lost+found
drwxr-x--- 2 mysql mysql     4096 Jan 31 03:07 mysql
drwxr-x--- 2 mysql mysql     4096 Jan 31 03:07 performance_schema
-rw------- 1 mysql mysql     1680 Jan 31 03:07 private_key.pem
-rw-r--r-- 1 mysql mysql      452 Jan 31 03:07 public_key.pem
-rw-r--r-- 1 mysql mysql     1112 Jan 31 03:07 server-cert.pem
-rw------- 1 mysql mysql     1676 Jan 31 03:07 server-key.pem
drwxr-x--- 2 mysql mysql    12288 Jan 31 03:07 sys
drwxr-x--- 2 mysql mysql     4096 Jan 31 03:07 xxl_job

4、然后替换pvc存储:

 5、mysql容器运行时可能有一个报错:

InnoDB: Your database may be corrupt or you may have copied the InnoDB tablespace but

解决方案:

需要删除两个文件即可:

root@mysql-ozthj-b7b7cb9cd-m8hr4:/var/lib/mysql# rm -rfv ib_logfile0
root@mysql-ozthj-b7b7cb9cd-m8hr4:/var/lib/mysql# rm -rfv ib_logfile1

然后重新启动一下mysql容器即可:

[mysql] [2024-01-31 11:14:42] 2024-01-31T03:14:42.386544Z 0 [Note] Plugin 'FEDERATED' is disabled.
[mysql] [2024-01-31 11:14:42] 2024-01-31T03:14:42.396895Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
[mysql] [2024-01-31 11:14:42] 2024-01-31T03:14:42.396920Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
[mysql] [2024-01-31 11:14:42] 2024-01-31T03:14:42.398866Z 0 [Warning] CA certificate ca.pem is self signed.
[mysql] [2024-01-31 11:14:42] 2024-01-31T03:14:42.398926Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
[mysql] [2024-01-31 11:14:42] 2024-01-31T03:14:42.399541Z 0 [Note] InnoDB: Buffer pool(s) load completed at 240131  3:14:42
[mysql] [2024-01-31 11:14:42] 2024-01-31T03:14:42.400190Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
[mysql] [2024-01-31 11:14:42] 2024-01-31T03:14:42.400242Z 0 [Note] IPv6 is available.
[mysql] [2024-01-31 11:14:42] 2024-01-31T03:14:42.400257Z 0 [Note]   - '::' resolves to '::';
[mysql] [2024-01-31 11:14:42] 2024-01-31T03:14:42.400277Z 0 [Note] Server socket created on IP: '::'.
[mysql] [2024-01-31 11:14:42] 2024-01-31T03:14:42.402599Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
[mysql] [2024-01-31 11:14:43] 2024-01-31T03:14:43.014325Z 0 [Note] Event Scheduler: Loaded 0 events
[mysql] [2024-01-31 11:14:43] 2024-01-31T03:14:43.014613Z 0 [Note] mysqld: ready for connections.
[mysql] [2024-01-31 11:14:43] Version: '5.7.32'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)

三、验证业务数据:

经过业务人员的验证及功能验证,数据是正确的,没有任何问题。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐