How to Fix MySQL Permission Errors When Changing Data Directory in Ubuntu Server 10.04


4 views

html

When attempting to relocate MySQL's data directory in Ubuntu Server 10.04, many developers encounter the frustrating "Error 13: Permission denied" message. This typically manifests when MySQL cannot access critical files like plugin.frm or ibdata1, indicating the service account lacks proper filesystem permissions.

First verify current MySQL configuration:

mysql --version
sudo cat /etc/mysql/my.cnf | grep datadir
ls -ld /var/lib/mysql

Key directories should show mysql:mysql ownership:

drwx------ 4 mysql mysql 4096 Aug 10 19:32 /var/lib/mysql

1. Stop MySQL service:

sudo service mysql stop

2. Copy existing data with proper permissions:

sudo cp -Rp /var/lib/mysql /new/path/data_directory

3. Update AppArmor configuration (critical for Ubuntu):

sudo nano /etc/apparmor.d/usr.sbin.mysqld

Add these lines under the existing directory rules:

/new/path/data_directory/ r,
/new/path/data_directory/** rwk,

4. Reload AppArmor:

sudo /etc/init.d/apparmor reload

Modify MySQL configuration file:

sudo nano /etc/mysql/my.cnf

Update the datadir parameter:

[mysqld]
datadir = /new/path/data_directory

Even on Ubuntu, check for residual SELinux contexts:

sudo apt-get install selinux-utils
sudo matchpathcon /new/path/data_directory

Set correct ownership recursively:

sudo chown -R mysql:mysql /new/path/data_directory
sudo chmod -R 750 /new/path/data_directory

Start MySQL and verify operation:

sudo service mysql start
mysql -u root -p -e "SHOW VARIABLES LIKE 'datadir'"

If issues persist, check error logs:

sudo tail -50 /var/log/mysql/error.log

For minimal downtime, consider symlinking instead of direct copy:

sudo service mysql stop
sudo mv /var/lib/mysql /new/path/data_directory
sudo ln -s /new/path/data_directory /var/lib/mysql
sudo service mysql start

When attempting to relocate MySQL's data directory in Ubuntu Server 10.04, many administrators encounter the frustrating Errno 13 permission denied error. The key indicators in the error log are:

InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.

Before proceeding with the solution, verify these critical points:

# Check current MySQL data directory
mysqladmin variables | grep datadir

# Verify directory ownership
ls -ld /path/to/new/datadir

# Confirm mysql user exists
grep mysql /etc/passwd

Here's the complete procedure to properly migrate your MySQL data directory:

# 1. Stop MySQL service
sudo service mysql stop

# 2. Create new directory (if needed)
sudo mkdir -p /new/data/path

# 3. Set proper ownership (critical step)
sudo chown -R mysql:mysql /new/data/path

# 4. Copy existing data (preserve permissions)
sudo cp -R -p /var/lib/mysql/* /new/data/path/

# 5. Update MySQL configuration
sudo nano /etc/mysql/my.cnf
# Locate [mysqld] section and modify:
datadir = /new/data/path

# 6. Update apparmor configuration (specific to Ubuntu)
sudo nano /etc/apparmor.d/usr.sbin.mysqld
# Add these lines:
/new/data/path/ r,
/new/data/path/** rwk,

# 7. Restart apparmor
sudo /etc/init.d/apparmor reload

# 8. Start MySQL
sudo service mysql start

After completing the migration, verify the changes:

# Check if MySQL is running
sudo service mysql status

# Verify new data directory
mysql -e "SELECT @@datadir;"

# Check for remaining permission issues
sudo tail -f /var/log/mysql/error.log

If you still encounter issues, try these additional steps:

# Repair tables (if needed)
sudo mysql_upgrade -u root -p

# Set SELinux context (if using SELinux)
sudo chcon -R -t mysqld_db_t /new/data/path

For some environments, using symbolic links might be preferable:

sudo service mysql stop
sudo mv /var/lib/mysql /new/data/path
sudo ln -s /new/data/path/mysql /var/lib/mysql
sudo chown -R mysql:mysql /new/data/path
sudo service mysql start