When working with MySQL data directories mounted on separate partitions, many DBAs encounter this frustrating scenario:
ls -ld /var/lib/mysql
drwxr-x--- 2 root root 4096 Jan 15 10:00 /var/lib/mysql
Despite setting chown -R mysql:mysql /var/lib/mysql
, the ownership reverts to root after every remount. This occurs because standard mount operations don't preserve directory permissions.
The solution lies in the filesystem mount options. For ext3/ext4 filesystems, we have several approaches:
# Method 1: Using uid/gid parameters
/dev/mapper/db-db /var/lib/mysql ext3 relatime,uid=107,gid=107 0 2
# Method 2: Using context (for SELinux systems)
/dev/mapper/db-db /var/lib/mysql ext3 relatime,context="system_u:object_r:mysqld_db_t:s0" 0 2
First identify your mysql user/group IDs:
id mysql
uid=107(mysql) gid=107(mysql) groups=107(mysql)
For systems using systemd, create a mount unit for more control:
# /etc/systemd/system/var-lib-mysql.mount
[Unit]
Description=MySQL Data Directory Mount
[Mount]
What=/dev/mapper/db-db
Where=/var/lib/mysql
Type=ext3
Options=uid=107,gid=107,relatime
[Install]
WantedBy=multi-user.target
Then enable it with:
systemctl enable --now var-lib-mysql.mount
Another reliable method uses bind mounts with proper permissions:
# /etc/fstab
/data/mysql /var/lib/mysql none bind,uid=107,gid=107 0 0
First ensure your actual data directory has correct permissions:
chown -R mysql:mysql /data/mysql
chmod 750 /data/mysql
After implementing any solution, verify with:
mount | grep mysql
ls -ld /var/lib/mysql
ls -ln /var/lib/mysql
Common issues to check:
- SELinux contexts if enabled (
ls -Z /var/lib/mysql
) - Parent directory permissions
- AppArmor/other MAC systems blocking access
When moving MySQL's data directory to a separate partition, many administrators encounter permission issues. In this case, we mounted a separate partition at /var/lib/mysql
with this fstab entry:
/dev/mapper/db-db /var/lib/mysql ext3 relatime 0 2
Despite setting the correct ownership (mysql:mysql
), the permissions revert to root:root
after every mount operation, preventing MySQL from creating new databases.
The filesystem's ownership is determined at mount time, and standard mount operations default to root ownership. The chown
command only affects the directory's metadata, not the underlying mounted filesystem.
For ext3/ext4 filesystems, we can use the uid
and gid
mount options. First, find your mysql user's UID and GID:
id mysql
Then modify your fstab entry (example assumes UID/GID of 27):
/dev/mapper/db-db /var/lib/mysql ext3 relatime,uid=27,gid=27 0 2
If the above doesn't work (or for non-ext filesystems), consider a bind mount approach:
# Create a directory with correct permissions
mkdir /mnt/mysql_data
chown mysql:mysql /mnt/mysql_data
# Original fstab entry
/dev/mapper/db-db /mnt/mysql_data ext3 relatime 0 2
# Bind mount in fstab
/mnt/mysql_data /var/lib/mysql none bind 0 0
After making changes, test with:
mount -o remount /var/lib/mysql
ls -ld /var/lib/mysql
The output should show mysql:mysql ownership. For immediate effect without reboot:
umount /var/lib/mysql
mount /var/lib/mysql
- Always back up your MySQL data before making filesystem changes
- Consider using
nofail
option if this is a non-critical mount - For production systems, test changes in a staging environment first