Bug #65413 /usr/libexec/mysqld: Can't change dir (Errcode: 13)
Submitted: 24 May 2012 15:16 Modified: 30 May 2012 11:55
Reporter: Anton Logvinenko Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Installing Severity:S1 (Critical)
Version:5.1.61 OS:Linux (CentOS release 6.2 (Final))
Assigned to: CPU Architecture:Any
Tags: CentOS, linux, mysqld

[24 May 2012 15:16] Anton Logvinenko
Description:
OS: CentOS release 6.2 (Final)

I tried to change default mysql database directory  from /var/lib/mysql to /media/sda3/mysql/ and i have errors after starting mysql daemon:

tailf /var/log/mysqld.log
120524 18:33:58 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
120524 18:39:21 mysqld_safe Starting mysqld daemon with databases from /media/sda3/mysql
120524 18:39:21 [Warning] Can't create test file /media/sda3/mysql/localhost.lower-test
120524 18:39:21 [Warning] Can't create test file /media/sda3/mysql/localhost.lower-test
/usr/libexec/mysqld: Can't change dir to '/media/sda3/mysql/' (Errcode: 13)
120524 18:39:21 [ERROR] Aborting

120524 18:39:21 [Note] /usr/libexec/mysqld: Shutdown complete

120524 18:39:21 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

How to repeat:
1. change default mysql db dir in my.cnf

[mysqld]
datadir=/media/sda3/mysql
socket=/media/sda3/mysql/mysql.sock
[mysqld_safe]
datadir=/media/sda3/mysql

2. copy database files to /media/sda3/mysql
cp /var/lib/mysql /media/sda3/

3. change permissions to files
chown mysql:mysql /var/lib/mysql/ -R

4. change SElinux permissions

chcon -R system_u:object_r:mysqld_db_t:s0 /media/sda3/mysql/

check it 
ls -Z /media/sda3/
drwxr-xr-x. mysql mysql system_u:object_r:mysqld_db_t:s0 mysql

5. /etc/init.d/mysqld start

Starting mysqld:                                           [FAILED]
[24 May 2012 15:46] Valeriy Kravchuk
If after this step:

2. copy database files to /media/sda3/mysql
cp /var/lib/mysql /media/sda3/

you really did this:

3. change permissions to files
chown mysql:mysql /var/lib/mysql/ -R

then owner of /media/sda3/mysql is probably still NOT mysql:mysql and error 13 is expected... Please, double check.
[24 May 2012 15:56] Anton Logvinenko
sorry, it's copy-paste mistake
actually i did this:

chown mysql:mysql /media/sda3/mysql -R

by the way, when i do this:

su mysql
cd /media/sda3/mysql
mkdir somedir

somedir successfully created
[25 May 2012 5:54] Valeriy Kravchuk
Looks like it may have something to do with SELinux then. See bug #41422 as yet another example. Please, try to start with SELinux disabled and check if this changes anything.
[25 May 2012 14:20] Anton Logvinenko
i think i found the solution of this problem

i create default mysql dir /var/lib/mysql
then give to dir right permissions

some changes to my.cnf
[mysqld]
datadir=/media/sda3/mysql
socket=/media/sda3/mysql/mysql.sock
log-bin=/media/sda3/mysql/mysql-bin

/etc/init.d/mysqd start
Starting mysqld: [  OK  ]

but what in the default mysql dir ?

ls /var/lib/mysql
mysql-bin.000001  mysql-bin.index

why? i think the problem with the Binary Log path
[30 May 2012 11:49] Sveta Smirnova
Thank you for the feedback.

> but what in the default mysql dir ?
>
> ls /var/lib/mysql
> mysql-bin.000001  mysql-bin.index

These can be files created earlier than you changed datadir.

To ensure please send full configuration file and output of `ls -l /media/sda3/mysql`
[30 May 2012 11:55] Anton Logvinenko
The problem resolved. I found duplicate records of log-bin path in config with different paths.

Thanks a lot!