Moving a MySQL data directory involves more than copying files to another disk. Service configuration, ownership, parent-directory permissions and SELinux labels all affect access. This workflow uses a CentOS 6.5 and MySQL 5.6 maintenance scenario; adapt service commands to the actual installation.

Confirm runtime configuration and recovery

SHOW VARIABLES LIKE 'datadir';
SHOW VARIABLES LIKE 'log_error';

Confirm that the connection reaches the intended instance and identify the configuration files it actually reads. Prepare a recoverable backup and maintenance window. Record whether data, logs and binlogs live in separate locations. Copying selected database directories may omit files the instance needs.

Stop writes and copy offline

Stop application writes, stop MySQL through its service manager, and confirm that its process has exited. Older systems commonly use service mysqld stop; systemd installations require the actual unit name. Do not mix the two blindly.

For a destination such as /data/mysql, first verify capacity and mount status. Use a copy method that preserves permissions and links, such as rsync if installed:

rsync -aH /var/lib/mysql/ /data/mysql/

Understand the trailing slashes on both paths. Keep the database stopped throughout copying. Check files, ownership and permissions afterward, then change datadir in the actual configuration. Retain the old directory until validation so a failed copy or configuration error remains recoverable.

Permission denied involves more than ownership

For errors such as Can't change dir or Can't create test file, check the mount, the mysql user’s traversal permission on every parent directory, file ownership and modes, and SELinux denials.

On SELinux systems, assign the appropriate database file context to the new path and apply it with restorecon. Use the distribution’s policy for exact types and commands. Disabling SELinux or making the directory writable by everyone should not be a default migration step.

Validate business behavior after startup

Start the correct service and inspect its error log. Query datadir again, and verify existing data and application reads and writes. Check binlogs, replication, backup jobs and automatic mounting after reboot, rather than accepting one successful startup as sufficient.

Rollback also requires stopping writes. If the new directory has already accepted business changes, simply starting the old copy would lose them. Remove old data only after defining the end of validation and a cleanup time.

References


blog comments powered by Disqus
Fork me on GitHub