Can't change mysql data folder CentOS 6

Gigitsu asked:

I’ve a server with CentOS 6.3 and Mysql installed on.
I’ve created two partition, one for system and the bigger one for data mounted in /data
Right after installed Mysql I’ve created the dir /data/mysql then as root I changed the selinux context of this folder typing chcon -R -t mysqld_db_t /data/mysql and modified the Mysql configuration file (my.cnf) to this:

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
symbolic-link=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

but now i can’t start mysqld service! It give me Can’t change dir to /data/mysql (errcode 13)

This is the output of the ls -Z /data | grep mysql:

drwxr-xr-x. mysql mysql system_u:object_r:mysqld_db_t:s0 mysql

The strange thing is that every time I try to start the mysqld service and it fails the context of myslq dir become system_u:object_r:default_t:s0 and two dirs (mysql and test) are created in it.

Anyway if I disable selinux evreything works great….

My answer:


You need to use semanage fcontext to set the default file context for your non-standard location for the MySQL databases.

semanage fcontext -a -t mysqld_db_t "/data/mysql(/.*)?"

Once done, use restorecon to fix any mislabeled files/directories:

restorecon -r -v /data/mysql

Now you should have no further SELinux issues.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.