挂载磁盘到/var/lib/mysql并迁移数据。

挂载磁盘到/var/lib/mysql并迁移数据。

本月初,我接手了一个托管在小型 Azure VM 上的 MySQL Server 5.6,该 VM 配有一个 30GB 的硬盘。数据库占用了 22GB;磁盘使用率为 89%。

使用符号链接将 /var/lib/mysql 迁移到已挂载的磁盘

lsblk && df
sudo mkdir /media/sdc
fdisk /dev/sdc
     n p 1 2048 209715199 w
sudo mkfs.ext3 /dev/sdc1
sudo mount /dev/sdc1 /media/sdc
sudo systemctl stop mysqld
sudo cp -dpR /var/lib/mysql /media/sdc/mysql
sudo mv /var/lib/mysql /var/lib/mysql.old
sudo ln -s /media/sdc/mysql /var/lib/mysql
sudo systemctl start mysqld

我安装了一个外部驱动器,将数据库文件迁移到外部驱动器;然后,将原始 mysql 目录更改为指向新位置的符号链接。最初我以为这可能是 mysql 未遍历符号链接的引用问题。所以我改变了策略,将外部驱动器直接安装到 /var/lib/mysql。

挂载磁盘到/var/lib/mysql并迁移数据。

#Remove the migrated files (I still have mysql.old)
sudo rm /var/lib/mysql
sudo rm -R /media/sdc/mysql

#Mount the drive to /var/lib/mysql directly.
sudo mount /dev/sdc1 /var/lib/mysql
sudo cp -dpR /var/lib/mysql.old /var/lib/mysql
sudo systemctl restart mysql

这也不起作用。在这两种情况下,mysql 都因相同的原因拒绝启动:

sudo systemctl restart mysql
Job for mysqld.service failed because a timeout was exceeded. See "systemctl status mysqld.service" and "journalctl -xe" for details

systemctl status mysqld.service

systemctl status mysqld.service
● mysqld.service - MySQL Community Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: activating (start-post) since Tue 2017-07-18 20:58:12 UTC; 4min 37s ago
Process: 32798 ExecStart=/usr/bin/mysqld_safe (code=exited, status=0/SUCCESS)
Process: 32787 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 32798 (code=exited, status=0/SUCCESS); : 32799 (mysql-systemd-s)
CGroup: /system.slice/mysqld.service
└─control
├─32799 /bin/bash /usr/bin/mysql-systemd-start post
└─33965 sleep 1

journalctl -xe

~
~
...skipping...
~
~

另外,我还尝试了各种方法来启动服务器,但都无济于事(结果相同):

sudo systemctl start mysqld
sudo systemctl start mysql.service
sudo systemctl start mysql
service mysql restart

ps aux | grep mysql

admin_u+ 58206  0.0  0.0 112648   976 pts/0    R+   14:19   0:00 grep --color=auto mysql 

我该如何解决这个问题并启动 mysql?

我是不是忽略了什么?我认为也可能是 datadir 设置,如下所示这个问题;但是,我采用的两种方法都没有修改 datadir 路径......

答案1

Yoonix 建议我查看 MySQL 日志。

mysqld.log

...
017-07-19 13:45:13 0 [Note] Binlog end
170719 13:45:13 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
170719 13:55:13 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
2017-07-19 13:55:13 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
/usr/sbin/mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)
2017-07-19 13:55:13 0 [ERROR] Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
2017-07-19 13:55:13 0 [ERROR] Aborting

解决方案:

在安装驱动器并将文件移至外部驱动器时,我也移动了 mysql 文件。将其移回后,我无法将其重命名为/mysql-files。我将其重命名为/mysqlfiles。重命名目录可修复该问题。

相关内容