由于/
分区只有50G,而我的数据比这个大,而且 里面还有500G的空间/home
,所以我把MySQL的所有数据都移到了 里面/home
,下面是我的操作步骤(在root
账户下):
关停 MySQL
# mysql -u root -p shutdown
创建
data
目录。# cd /home/ && mkdir data
/var/lib/mysql
将目录移动到/home/data
# mv /var/lib/mysql /home/data/
编辑
my.cnf
和/usr/lib64/mysql/mysql_config
改成
socket=/var/lib/mysql/mysql.sock
socket=/home/data/mysql/mysql.sock
datadir=/home/data/mysql
仅my.cnf
在下[mysqldb]
节添加ln-s
首先我创建了一个名为的目录
mysql
。/var/lib/
并chown mysql:mysql /var/lib/mysql
创建链接:ln -s /home/data/mysql/mysql.sock /var/lib/mysql/mysql.sock
更改所有者
/home/data/mysql
chown -R mysql:mysql /home/data/mysql/
启动 MySQL 服务
# systemctl start mariadb.service
但是,它启动失败了。
[root@localhost]~# systemctl start mariadb.service
Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details.
详细信息如下:
[root@localhost]~# systemctl status mariadb.service
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Thu 2016-04-21 19:51:13 HKT; 39s ago
Process: 7144 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=1/FAILURE)
Process: 7143 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=0/SUCCESS)
Process: 7090 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 7143 (code=exited, status=0/SUCCESS)
Apr 21 19:51:12 localhost.localdomain systemd[1]: Starting MariaDB database server...
Apr 21 19:51:12 localhost.localdomain mysqld_safe[7143]: 160421 19:51:12 mysqld_safe Logging to '/home/data/mysql/localhost.localdomain.err'.
Apr 21 19:51:12 localhost.localdomain mysqld_safe[7143]: 160421 19:51:12 mysqld_safe Starting mysqld daemon with databases from /home/data/mysql
Apr 21 19:51:12 localhost.localdomain mysqld_safe[7143]: 160421 19:51:12 mysqld_safe mysqld from pid file /home/data/mysql/localhost.localdomain.pid ended
Apr 21 19:51:13 localhost.localdomain systemd[1]: mariadb.service: control process exited, code=exited status=1
Apr 21 19:51:13 localhost.localdomain systemd[1]: Failed to start MariaDB database server.
Apr 21 19:51:13 localhost.localdomain systemd[1]: Unit mariadb.service entered failed state.
Apr 21 19:51:13 localhost.localdomain systemd[1]: mariadb.service failed.
[root@localhost]~# journalctl -xe
-- The start-up result is done.
Apr 21 19:50:01 localhost.localdomain systemd[1]: Starting Session 7 of user root.
-- Subject: Unit session-7.scope has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-7.scope has begun starting up.
Apr 21 19:50:01 localhost.localdomain CROND[6971]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Apr 21 19:51:12 localhost.localdomain polkitd[900]: Registered Authentication Agent for unix-process:7084:268964 (system bus name :1.80 [/usr/bin/pkttyagent
Apr 21 19:51:12 localhost.localdomain systemd[1]: Starting MariaDB database server...
-- Subject: Unit mariadb.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mariadb.service has begun starting up.
Apr 21 19:51:12 localhost.localdomain mysqld_safe[7143]: 160421 19:51:12 mysqld_safe Logging to '/home/data/mysql/localhost.localdomain.err'.
Apr 21 19:51:12 localhost.localdomain mysqld_safe[7143]: 160421 19:51:12 mysqld_safe Starting mysqld daemon with databases from /home/data/mysql
Apr 21 19:51:12 localhost.localdomain mysqld_safe[7143]: 160421 19:51:12 mysqld_safe mysqld from pid file /home/data/mysql/localhost.localdomain.pid ended
Apr 21 19:51:13 localhost.localdomain systemd[1]: mariadb.service: control process exited, code=exited status=1
Apr 21 19:51:13 localhost.localdomain systemd[1]: Failed to start MariaDB database server.
-- Subject: Unit mariadb.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mariadb.service has failed.
--
-- The result is failed.
Apr 21 19:51:13 localhost.localdomain systemd[1]: Unit mariadb.service entered failed state.
Apr 21 19:51:13 localhost.localdomain systemd[1]: mariadb.service failed.
Apr 21 19:51:13 localhost.localdomain polkitd[900]: Unregistered Authentication Agent for unix-process:7084:268964 (system bus name :1.80, object path /org/f
lines 3132-3161/3161 (END)
我的操作系统是CentOS 7
,MySQL是MariaDB
。
如何修复?谢谢!
答案1
尝试启动时,请观察 /var/log/messages。您可能会发现 SELinux 妨碍了您:
tail -f /var/log/messages
尝试将 MariaDB 留在预期位置 /var/lib/mysql。选项包括:
1)如果性能很重要:缩小 /home 上的文件系统和分区(或逻辑卷),然后创建一个新分区并将其挂载在 /var/lib/mysql
2)如果性能不太重要:在 /home/mysql.img 上创建一个大文件,并通过 losetup 将该文件用作 /dev/loop0 并挂载在 /var/lib/mysql 上。
3)如果性能确实不重要:对一个大的稀疏文件执行losetup,并将“丢弃”传递给你的ext4文件系统以保持文件稀疏。
答案2
当我尝试将 MariaDB 数据库文件移动到 ISCSI 共享时,我遇到了完全相同的问题。当我的共享已安装并正确格式化后,SELinux 将目录标记为system_u:object_r:unlabeled_t
。您可以通过运行以下命令来检查目录的标签...ls -Zd
SELinux 不喜欢意外。因此,要在更改数据目录后运行 MariaDB,您必须重新标记新目录。使用以下命令:semanage fcontext -a -e /var/lib/mysql/ <your new directory>
。此命令将旧的 mysql 目录标记映射到新的标记。通过在新标记的目录上运行来重新检查ls -Zd
。
最后,使用以下命令将更改传播到整个新修改的目录中:restorecon -R <your new directory>
。
所有这些都假设您复制了所有的 mysql 文件而没有进行修改,并且您有原始文件的备份,以便需要恢复。
请参阅以下文章以获取有关此解决方案的参考:
https://www.unixmen.com/selinux-and-non-default-home-directory-locations/