Since /
partition is only 50G, and my data is larger than that. And there are 500G space in /home
. So, I move all the data of MySQL into /home
.
The following are my steps(under root
account):
shout down MySQL
# mysql -u root -p shutdown
make
data
directory.# cd /home/ && mkdir data
move
/var/lib/mysql
directory to/home/data
# mv /var/lib/mysql /home/data/
edit
my.cnf
and/usr/lib64/mysql/mysql_config
change
socket=/var/lib/mysql/mysql.sock
tosocket=/home/data/mysql/mysql.sock
add
datadir=/home/data/mysql
only inmy.cnf
under[mysqldb]
sectionln -s
first I made a directory named
mysql
in/var/lib/
. Andchown mysql:mysql /var/lib/mysql
make link:ln -s /home/data/mysql/mysql.sock /var/lib/mysql/mysql.sock
change owner of
/home/data/mysql
chown -R mysql:mysql /home/data/mysql/
start MySQL service
# systemctl start mariadb.service
But, it start failed.
[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.
And the detailed message is:
[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)
My OS is CentOS 7
, MySQL is MariaDB
.
How to fix it? Thanks!
Watch your /var/log/messages while you try to start it. You might find SELinux getting in the way:
Try to leave MariaDB at the expected location /var/lib/mysql. Options include:
1) If performance matters: shrink the filesystem and partition (or logical volume) on /home, then create a new partition and mount it at /var/lib/mysql
2) If performance less critical: Create a big file on /home/mysql.img and use that file via losetup as /dev/loop0 and mount at /var/lib/mysql.
3) If performance really doesn't matter: Do the losetup on a big sparse file, and pass "discard" to your ext4 filesystem to keep the file sparse.
I had exactly the same problem when trying to move MariaDB database files to an ISCSI share. When my share was mounted and properly formatted, SELinux tags the directory as
system_u:object_r:unlabeled_t
. You can check the label of your directory by running the following command...ls -Zd
SELinux does not like surprises. So to get MariaDB running after changing the data directory, you have to relabel your new directory. Use the following command:
semanage fcontext -a -e /var/lib/mysql/ <your new directory>
. This command maps the old mysql directory tagging to the new one. Recheck by runningls -Zd
on the newly tagged directory.Finally, propagate the changes throughout the newly modified directory with the following command:
restorecon -R <your new directory>
.All this assumes you copied all the mysql files over with out modification and that you have a backup of the original should you need to revert.
Look at the following articles for references to this solution:
https://www.digitalocean.com/community/tutorials/how-to-change-a-mariadb-data-directory-to-a-new-location-on-centos-7
https://www.unixmen.com/selinux-and-non-default-home-directory-locations/