How to do it...

  1. Check the current data directory. By default, the data directory is /var/lib/mysql:
mysql> show variables like '%datadir%';+---------------+-----------------+| Variable_name | Value           |+---------------+-----------------+| datadir       | /var/lib/mysql/ |+---------------+-----------------+1 row in set (0.04 sec)
  1. Stop mysql and make sure it has stopped successfully:
shell> sudo systemctl stop mysql
  1. Check the status:
shell> sudo systemctl status mysql

It should show Stopped MySQL Community Server.

  1. Create the directory at the new location and change the ownership to mysql:
shell> sudo mkdir -pv /datashell> sudo chown -R mysql:mysql /data/
  1. Move the files to the new data directory:
shell> sudo rsync -av /var/lib/mysql /data

Get MySQL 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.