How to change MySQL's data directory

  1. Have a backup.

  2. Stop MySQL:

    sudo service mysql stop
    
  3. Move (or copy) your mysql directory. If you want /mnt/mysql to be the new directory, do it like this:

    sudo mv /var/lib/mysql /mnt/
    
  4. Open your MySQL configuration (sudo vim /etc/mysql/my.cnf) and change the datadir value to your new path (e.g. /mnt/mysql)

  5. Modify your AppArmor configuration:

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

    Change/copy the lines granting access to /var/lib/mysql to your new path. Otherwise MySQL will not start.

  6. Load your new AppArmor configuration:

    sudo service apparmor reload
    
  7. Start MySQL:

    sudo service mysql start
    
Arne Hartherz