Move MySQL datadir to new partition

  Centos, Linux

I had to move a mysql datadir to a new partition as it was stored in /var/lib/mysql but df -h showed the main partition being /data and the / partition was now full up. This was a Centos machine.

I stopped mysql –

/etc/init.d/mysqld stop

I copied the mysql databases over to the new directory I created –

mkdir /data/mysql && cp -R /var/lib/mysql/* /data/mysql/

Changed the owndership –

chown -R mysql:mysql /data/mysql

I ensured the permissions were set properly and matched on both old and new directories by checking them with ls -l on the folder and inside them. They are usually 700 I believe on mysql.


I then edited the startup script –

vi /etc/init.d/mysqld

I changed the datadir line to the new directory.

I then changed the /etc/my.cnf file to use the new directory (it was hard set here too, so you should check that).

I tried starting mysql and checking it was using the new datadir –

/etc/init.d/mysqld start

ps ax | grep sql

I then stopped mysql and removed the old databases in /var/lib/mysql (to free up the space)

I started mysql again – /etc/init.d/mysql but when looking at the logs I saw it was crashed, so I ran mysqlcheck -A (but with -u myuser -p -A). Once this finished I restarted mysqld and it started without any errors in the logs.

I also needed to change the socket file directory, which I defined in the my.cnf file accordingly –