简体   繁体   English

如何在docker容器中迁移MySQL数据目录?

[英]How to migrate MySQL data directory in docker container?

I have a docker container running MySQL-5.5 with host data volume. 我有一个运行带有主机数据量的MySQL-5.5的docker容器。 I am upgrading my container to MySQL-5.6. 我正在将我的容器升级到MySQL-5.6。 I am starting a new container with the same host volume. 我正在启动一个具有相同主机卷的新容器。 MySQL was crashing in the container due to the crash of mysql.user table. 由于mysql.user表崩溃,MySQL在容器中崩溃。

[ERROR] Fatal error: Can't open and lock privilege tables: 
Incorrect key file for table 'user'; try to repair it
160523 12:04:13 mysqld_safe mysqld from pid file  /var/run/mysqld/mysqld.pid ended

I tried the following to fix that: 我尝试了以下方法来解决这个问题:

root@container# mysqld --skip-grant-tables;
root@container# mysql -uroot -ppassword
mysql> repair table user USE_FRM;
+------------+--------+----------+--------------------------------------------------+
| Table      | Op     | Msg_type | Msg_text                                         |
+------------+--------+----------+--------------------------------------------------+
| mysql.user | repair | info     | Key 1 - Found wrong stored record at 0           |
| mysql.user | repair | info     | Found block that points outside data file at 184 |
| mysql.user | repair | info     | Found block that points outside data file at 292 |
| mysql.user | repair | info     | Found block that points outside data file at 296 |
| mysql.user | repair | info     | Found block that points outside data file at 300 |
| mysql.user | repair | info     | Found block that points outside data file at 304 |
| mysql.user | repair | info     | Found block that points outside data file at 308 |
| mysql.user | repair | info     | Found block that points outside data file at 312 |
| mysql.user | repair | info     | Found block that points outside data file at 316 |
| mysql.user | repair | info     | Found block that points outside data file at 340 |
| mysql.user | repair | info     | Found block that points outside data file at 392 |
| mysql.user | repair | info     | Found block that points outside data file at 396 |
| mysql.user | repair | info     | Found block that points outside data file at 400 |
| mysql.user | repair | info     | Found block that points outside data file at 404 |
| mysql.user | repair | info     | Found block that points outside data file at 408 |
| mysql.user | repair | info     | Found block that points outside data file at 412 |
| mysql.user | repair | info     | Found block that points outside data file at 416 |
| mysql.user | repair | info     | Found block that points outside data file at 420 |
| mysql.user | repair | info     | Found block that points outside data file at 448 |
| mysql.user | repair | info     | Found block that points outside data file at 452 |
| mysql.user | repair | info     | Found block that points outside data file at 456 |
| mysql.user | repair | info     | Found block that points outside data file at 460 |
| mysql.user | repair | info     | Found block that points outside data file at 464 |
| mysql.user | repair | info     | Found block that points outside data file at 468 |
| mysql.user | repair | info     | Found block that points outside data file at 472 |
| mysql.user | repair | info     | Found block that points outside data file at 500 |
| mysql.user | repair | info     | Found block that points outside data file at 504 |
| mysql.user | repair | info     | Found block that points outside data file at 508 |
| mysql.user | repair | info     | Found block that points outside data file at 512 |
| mysql.user | repair | info     | Found block that points outside data file at 516 |
| mysql.user | repair | info     | Found block that points outside data file at 520 |
| mysql.user | repair | info     | Found block that points outside data file at 524 |
| mysql.user | repair | info     | Found block that points outside data file at 528 |
| mysql.user | repair | info     | Found block that points outside data file at 556 |
| mysql.user | repair | info     | Found block that points outside data file at 560 |
| mysql.user | repair | info     | Found block that points outside data file at 564 |
| mysql.user | repair | info     | Found block that points outside data file at 568 |
| mysql.user | repair | info     | Found block that points outside data file at 572 |
| mysql.user | repair | info     | Found block that points outside data file at 576 |
| mysql.user | repair | info     | Found block that points outside data file at 580 |
| mysql.user | repair | info     | Found block that points outside data file at 604 |
| mysql.user | repair | info     | Found block that points outside data file at 608 |
| mysql.user | repair | info     | Found block that points outside data file at 612 |
| mysql.user | repair | info     | Found block that points outside data file at 616 |
| mysql.user | repair | info     | Found block that points outside data file at 620 |
| mysql.user | repair | info     | Found block that points outside data file at 624 |
| mysql.user | repair | info     | Found block that points outside data file at 628 |
| mysql.user | repair | info     | Found block that points outside data file at 632 |
| mysql.user | repair | status   | OK                                               |
+------------+--------+----------+--------------------------------------------------+
49 rows in set (0.00 sec)

Error: I cannot log in into MySQL server using my username/password. 错误:我无法使用用户名/密码登录MySQL服务器。 Any idea on how should I upgrade my MySQL data? 关于如何升级我的MySQL数据的任何想法?

root@container# mysql -uroot -ppassword
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with; or \g.
Your MySQL connection id is 2
Server version: 5.6.30-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit;
Bye
root@container# ps -ef|grep mysql              
root      1509 31492  0 12:15 ?        00:00:00 grep --color=auto mysql
mysql    31281  4823  0 12:06 ?        00:00:01 mysqld --skip-grant-tables --user=mysql
root@container# kill -9 31281
root@container# ps -ef|grep mysql
root      1698 31492  0 12:16 ?        00:00:00 grep --color=auto mysql
root@container# mysqld_safe &
[1] 1700
root@container# 160523 12:16:34 mysqld_safe Can't log to error log and syslog at the same time.  Remove all --log-error configuration     options for --syslog to take effect.
160523 12:16:34 mysqld_safe Logging to '/var/log/mysql/error.log'.
160523 12:16:34 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
^C
root@container# mysql -uroot -ppassword
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
root@container# 

Reference: 参考:

MySQL Fatal error: Can't open and lock privilege tables: Incorrect file format 'user' https://bugs.mysql.com/bug.php?id=68385 MySQL致命错误:无法打开和锁定权限表:文件格式不正确'用户' https://bugs.mysql.com/bug.php?id=68385

You could start MySQL using the 5.5 image and run mysqldump against it 您可以使用5.5映像启动MySQL并对其运行mysqldump

docker run --rm --link mysqld mysql:5.5 \
       mysqldump -h mysqld --all-databases > /your/host/machine/

And then start a new container using the 5.6 image and initialize it using the SQL dump 然后使用5.6映像启动一个新容器,并使用SQL转储对其进行初始化

docker run -v /data/your_dump.sql:/docker-entrypoint-initdb.d/dump.sql mysql:5.6

I believe you need to modify your dockerfile to run mysql-upgrade before starting mysql. 我相信在启动mysql之前你需要修改你的dockerfile来运行mysql-upgrade Although you might need to repair it as well as described in the references you provided. 虽然您可能需要修复它,以及您提供的参考中所述。 You should only need to run it once, then you can remove it from dockerfile. 您只需要运行一次,然后就可以从dockerfile中删除它。 (I assume database is actually stored on the host file system and mounted in the docker.) (我假设数据库实际上存储在主机文件系统中并挂载在docker中。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM