我正在尝试将 MySQL 的数据目录移动到 odroid(类似树莓派的微型计算机)上的外部驱动器。
我正在使用 Ubuntu 14.04。
我更改了包含所有配置文件的路径/var/lib/mysql
,并将所有 mysql 文件夹移动到新驱动器上。
该服务不再启动,我不知道如何解决这个问题。
以下是所有更改的路径:
root@odroid-server:/etc# grep -rnw '/etc' -e "/var/lib/mysql"
root@odroid-server:/etc# grep -rnw '/etc' -e "/media/md0/mysql"
/etc/apparmor.d/abstractions/mysql:12: /media/md0/mysql{,d}/mysql{,d}.sock rw,
/etc/apparmor.d/tunables/alias:16:# alias /media/md0/mysql/ -> /home/mysql/,
/etc/apparmor.d/usr.sbin.mysqld:49: /media/md0/mysql/ r,
/etc/apparmor.d/usr.sbin.mysqld:50: /media/md0/mysql/** rwk,
/etc/apparmor.d/usr.sbin.mysqld:53: /media/md0/mysql-files/ r,
/etc/apparmor.d/usr.sbin.mysqld:54: /media/md0/mysql-files/** rwk,
/etc/apparmor.d/usr.sbin.mysqld:57: /media/md0/mysql-keyring/ r,
/etc/apparmor.d/usr.sbin.mysqld:58: /media/md0/mysql-keyring/** rwk,
/etc/imscp/imscp.conf:37:DATABASE_DIR = /media/md0/mysql
/etc/mysql/mysql.conf.d/mysqld.cnf:36:datadir = /media/md0/mysql
/etc/mysql/mysql.conf.d/mysqld.cnf:92:# InnoDB is enabled by default with a 10MB datafile in /media/md0/mysql/.
/etc/mysql/mysql.conf.d/mysqld.cnf:98:# chroot = /media/md0/mysql/
然后启动服务:
root@odroid-server:/etc# service mysql start
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
我尝试该命令来获取更多详细信息,结果发现它仍然使用旧的 mysql datadir。
root@odroid-server:/etc# journalctl -xe
Nov 18 13:06:45 odroid-server systemd[1]: Starting MySQL Community Server...
-- Subject: Unit mysql.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mysql.service has begun starting up.
Nov 18 13:06:45 odroid-server mysql-systemd-start[24276]: MySQL data dir not found at /var/lib/mysql. Please create one.
Nov 18 13:06:45 odroid-server systemd[1]: mysql.service: Control process exited, code=exited status=1
Nov 18 13:06:45 odroid-server systemd[1]: Failed to start MySQL Community Server.
-- Subject: Unit mysql.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mysql.service has failed.
--
-- The result is failed.
Nov 18 13:06:45 odroid-server systemd[1]: mysql.service: Unit entered failed state.
Nov 18 13:06:45 odroid-server systemd[1]: mysql.service: Failed with result 'exit-code'.
Nov 18 13:06:45 odroid-server systemd[1]: mysql.service: Service hold-off time over, scheduling restart.
Nov 18 13:06:45 odroid-server systemd[1]: Stopped MySQL Community Server.
-- Subject: Unit mysql.service has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mysql.service has finished shutting down.
Nov 18 13:06:45 odroid-server systemd[1]: mysql.service: Start request repeated too quickly.
Nov 18 13:06:45 odroid-server systemd[1]: Failed to start MySQL Community Server.
-- Subject: Unit mysql.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mysql.service has failed.
--
-- The result is failed.
也许有一些命令可以将配置文件重新加载到内存中?
答案1
来自评论:
只需符号链接
/var/lib/mysql
到新目录即可。
创建符号链接是移动重要文件和目录的一种快速简便的方法。无需更改任何配置,而且需要跟踪的文档很少。
答案2
我刚刚找到了解决这个问题的方法(这件事发生在我身上好几年之后)。
问题很简单,就是 ubuntu 服务器附带了恶意软件装甲。我检查后才注意到dmesg
。为了摆脱它,我只需运行
sudo apt remove --purge apparmor -y
这使得 mysql 使用我的新 mysql datadir 启动(并且也修复了其他问题)。
答案3
SELINUX 也可能是你烦恼的原因。查看我的博客文章,了解有关如何修复的更多详细信息:https://sysengineers.wordpress.com/2021/03/22/mysqld-fails-to-start-after-moving-its-data-directory/
ps. 请注意,apparmor 和 SELinux 可提高 Linux 环境的整体安全性。删除它们可能会解决当前的问题,但会使您的安装更容易受到攻击。