MySQL Data Storage Location on CentOS: Default Paths and Partition Configuration Analysis


2 views

On CentOS systems, MySQL/MariaDB typically stores its data files in /var/lib/mysql by default. However, this isn't an absolute rule - the actual location depends on several configuration factors:

# To check current datadir location:
mysql -e "SHOW VARIABLES LIKE 'datadir';"
# OR check my.cnf:
grep "datadir" /etc/my.cnf

In your case where only these partitions exist:

/dev/sda3              /
tmpfs                  /dev/shm
/dev/sda1              /boot
/usr/tmpDSK            /tmp

This means MySQL data would be stored under the root partition (/) unless specifically configured otherwise. The common paths would be:

  • /var/lib/mysql (default)
  • /usr/local/mysql/data (alternative common location)
  • Or a custom path defined in my.cnf

If you suspect data is being temporarily stored, check these indicators:

# Check if MySQL is using tmpfs
df -h | grep mysql
# Verify storage engine temp files
ls -la /tmp/ib* 2>/dev/null

For production servers, consider these recommendations:

# Example fstab entry for dedicated MySQL partition:
/dev/sdb1  /mysql_data  ext4  defaults,noatime  0 2
# Then in my.cnf:
[mysqld]
datadir=/mysql_data

cPanel servers often modify default paths. Check these cPanel-specific locations:

# cPanel MySQL config usually here:
/var/cpanel/conf/mysql/config
# To rebuild MySQL with custom datadir in cPanel:
/scripts/mysqlup --force --datadir=/new/path

If you need to move MySQL data to a new location:

# 1. Stop MySQL
systemctl stop mysqld

# 2. Copy data (preserving permissions)
rsync -avz /var/lib/mysql/ /new/mysql/location/

# 3. Update SELinux context
semanage fcontext -a -t mysqld_db_t "/new/mysql/location(/.*)?"
restorecon -Rv /new/mysql/location

# 4. Update my.cnf and restart
sed -i 's|datadir=.*|datadir=/new/mysql/location|g' /etc/my.cnf
systemctl start mysqld

Remember to verify disk space before and after any migration:

df -h / /new/mysql/location

On CentOS systems, MySQL (or its fork MariaDB) typically stores database files in /var/lib/mysql by default. This location is defined in the MySQL server configuration file (/etc/my.cnf or /etc/my.cnf.d/server.cnf) under the datadir parameter.

The partition structure you've provided shows:

/dev/sda3              /
tmpfs                  /dev/shm
/dev/sda1              /boot
/usr/tmpDSK            /tmp

This indicates your system has a single root partition (/) containing all directories not explicitly mounted elsewhere. Since /var isn't a separate mount point, it exists within the root partition.

To confirm where MySQL is storing data, check these methods:

# Method 1: Check MySQL configuration
mysqladmin variables | grep datadir

# Method 2: Check running process
ps aux | grep mysql | grep -oP "datadir=\\K[^ ]+"

# Method 3: Check configuration files
grep -r "datadir" /etc/my.cnf /etc/my.cnf.d/

When /var isn't a separate partition:

  • MySQL uses /var/lib/mysql on the root partition (most common default)
  • The datadir might be configured in an alternative location
  • In cPanel environments, the location might be /home/mysql

cPanel often modifies default MySQL locations. Check these cPanel-specific locations:

# Check cPanel's MySQL configuration
/usr/local/cpanel/bin/mysqladmin variables | grep datadir

# Alternative cPanel check
cat /var/cpanel/cpanel.config | grep MYSQL_DATADIR

If you need to relocate MySQL data, follow these steps:

# 1. Stop MySQL service
systemctl stop mysqld

# 2. Copy existing data
rsync -av /var/lib/mysql/ /new/location/

# 3. Update permissions
chown -R mysql:mysql /new/location

# 4. Update my.cnf
echo "datadir=/new/location" >> /etc/my.cnf

# 5. Configure SELinux (if enabled)
semanage fcontext -a -t mysqld_db_t "/new/location(/.*)?"
restorecon -Rv /new/location

# 6. Start MySQL
systemctl start mysqld

To monitor MySQL's disk usage in your current setup:

# Check overall usage
du -sh $(mysql -NBe "SELECT @@datadir")

# Check per-database usage
mysql -e "SELECT table_schema 'Database', 
ROUND(SUM(data_length+index_length)/1024/1024,2) 'Size (MB)' 
FROM information_schema.tables 
GROUP BY table_schema;"