How to change default MySQL directory

Originally it will be located at /var/lib/mysql

Say you want to change it to /home/myApps/mysql

First if the mysql is running stop it: sudo /etc/init.d/mysql stop
and stop the server too: sudo/etc/init.d/apache2 stop

Go to /etc/mysql/my.cnf and edit:
datadir = /var/lib/mysql to datadir = /home/myApps/mysql

Copy files from existing path to the new path
cp -R /var/lib/mysql* /home/myApp/mysql

Grant ownership to new directory for mysql

sudo chown -R mysql:mysql /home/myApp/mysql

then start up your server and mysql – voila.