How to move mysql datadir

This example will show you how to move MySQL datadir to another dir (in my case to “/mysqldata”) step-by-step (OS: Linux Mint):

1. create new dir (/mysqldata) and set permissions to it

sudo mkdir /mysqldata
sudo chown mysql:mysql /mysqldata/

2. stop mysql

sudo /etc/init.d/mysql stop

3. copy mysql datadir with all their permissions

sudo cp -R -p /var/lib/mysql/* /mysqldata/

4. edit mysql configuration file (/etc/my.cnf)

sudo vi /etc/my.cnf

and change there datadir variable (under [mysqld] section)

datadir = /mysqldata

5. start mysql

sudo /etc/init.d/mysql start

6. check if it works

mysql -u root -p -e "select @@datadir\G"
*************************** 1. row ***************************
@@datadir: /mysqldata/

it works!