Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have MySQL installed on CentOS (detail versions given below). My OS is a VM machine and not a physical machine, the directory /data/mysql is a local disk (virtual). I suspect I might need to do some extra steps in a virtualized environment. This link has some information on SELinux & virtualization but not sure how to include it for MySQL.

MySQL works fine with the data directory in /var/lib/mysql but the moment I move it to another directory on different disk it stops working. Whereas different directory on the same disk works. If I disable SELinux, MySQL works with different disk. SELinux is restricting different drive. I tried the solution given in the following two links

In particular the following two commands:

$ semanage fcontext --add --type mysqld_db_t '/data/mysql(/.*)?'
$ restorecon -r /data/mysql

When I run:

$ ls -dZ /data/mysql

I get the following:

drwxr-xr-x. mysql mysql unconfined_u:object_r:mysqld_db_t:SystemLow 

Still I keep getting:

/usr/sbin/mysqld: Can't change dir to '/data/mysql/' (Errcode: 13 - Permission denied)

Is there anything else I need to do?

  • MySQL : mysql Ver 14.14 Distrib 5.6.14, for Linux (x86_64) using EditLine wrapper
  • OS : CentOS release 6.4 (Final)
share|improve this question
Does modifying (with a text editor) the selinux contexts file help? (/etc/selinux/targeted/contexts/files/file_contexts) – Christopher Nov 25 '13 at 22:52
/etc/selinux/targeted/contexts/files/file_contexts file doesn't have entry for path. I am pretty new to SELinux. Not sure what to edit there. – user2619299 Nov 26 '13 at 0:51
I checked /etc/selinux/targeted/contexts/files/file_contexts.local and there were several entries which were added during trying out different command. I removed everything using semanage fcontext -D and re-ran semanage fcontext --add --type mysqld_db_t '/data/mysql(/.*)?' and it worked. – user2619299 Nov 26 '13 at 16:19
I also needed to change the container of that folder to var_lib_t type. Not sure why this matters. – user2619299 Nov 26 '13 at 19:18
up vote 0 down vote accepted

I don't see anything directly wrong with what you're doing in terms of the commands you're running or how you're approaching the addition of /data/mysql with respect to SELinux.

Just to make sure things are working properly I would check if present and/or manually add this line to /etc/selinux/targeted/contexts/files/file_contexts.local.

/data/mysql(/.*)?   system_u:object_r:mysqld_db_t:s0

When I repeated your steps above I had that line added to my file_contexts.local file as expected.

I would double check your steps you're following against the steps discussed here in this Redhat specific document titled: 10.4.1. MySQL Changing Database Location.

share|improve this answer
Thanks for the info about file_contexts.local. My file_contexts.local had lot of interfering entries. I cleaned all of them using semanage fcontext -D and readded semanage fcontext --add --type mysqld_db_t '/data/mysql(/.*)?' – user2619299 Nov 26 '13 at 16:21
@user2619299 - excellent, glad this resolved your problem. Thanks for the Q. – slm Nov 26 '13 at 16:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.