Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i need to move the data to a different folder and drive on my cent os db server.

i added new hard drive (since we ran low on space),

i have a 1tb of hard drive and i have a new 2tb i want to put the data on on the same server (dedicated server hosted in a datacenter)

i would like to know what is the easy way to migrate the data without losing any data?

currently i have the data located in: /var/lib/mysql/ on cent os. mysql version is 5.1

share|improve this question

migrated from Jan 30 '12 at 5:43

This question came from our site for professional and enthusiast programmers.

does your mysql server can be shutdown during the mogration? – Book Of Zeus Jan 29 '12 at 18:29
prefereble no, but if there's no other way then yes – Vladimir Kakazsky Jan 29 '12 at 18:34
are you using innodb? – abdelsaid Jan 29 '12 at 18:43
@abdelsaid no, im using myisam – Vladimir Kakazsky Jan 29 '12 at 18:49
I tried to do the same in CENTOS 6.4 and this link worked perfectly! – user48668 Oct 4 '14 at 2:13
up vote 12 down vote accepted

Step 1

First setup your drive (assumed it's mounted and setup correctly in the fstab) and create new MySQL directory:

sudo mkdir /path/to/new/mysql

Make sure this folder has the right permissions:

chown -R mysql:mysql /data/mysql

Now here you can shutdown the service while copying data. Copy the data from the current data directory to new data directory, example:.

sudo cp -R /var/lib/mysql/* /path/to/new/mysql/

or you can copy individually each databases if you prefer or if you feel safe, you can move the folder using the mv command.

Step 2

You can change the location of your MySQL data in your my.cnf. If you don't know where this file is, you can run the command:

locate my.cnf

This will give you the path of where the my.cnf is located. Then edit the file:

sudo vi /etc/mysql/my.cnf (of course you have to replace with your path)

Now look for datadir = /var/lib/mysql and update to datadir = /path/to/new/mysql.

Then restart mysql

sudo /etc/init.d/mysqld restart

After few days/weeks, if you feel comfortable you can remove the old folder (/var/lib/mysql) or backup everything inside.

share|improve this answer
he doesn't want downtime or at least minimal. cp -R will take forever. and changing the directory might result in data loss in case of clients online. – abdelsaid Jan 29 '12 at 18:57
he said: prefereble no, but if there's no other way then yes. Also, if you have a lot of inserts in a tables you currently copying using MyISAM (table locking) you might have errors or data that are not copied over. – Book Of Zeus Jan 29 '12 at 19:00
there are other ways :) – abdelsaid Jan 29 '12 at 19:07
@abdelsaid can you tell me another way? – Pat R Ellery Nov 12 '13 at 13:11
I replaced full wamp\bin\mysql\mysql5.5.24 But it's showing databases, not tables – KarSho May 7 '14 at 12:28

If you don't want shutdown during migration, I would setup a master-master replication to chroot environment on 2tb with mysql (same version) in it, then when on sync turn the 1tb off and you got a working chroot environment.

If you don't want to use chroot environment, change the my.cnf (not the chroot one) to point to where you want to store your mysql files on 2tb disk and disable replication. Then turn off the chroot environment and move the data to what you have in my.cnf, the move here will not take even a second because it's on the same disk. Restart mysql.

This is little bit complex to do, you can play with this in local vm enironment first.

share|improve this answer
yes it complicated for me. I don't understand chroot and replication is complex for me. – Vladimir Kakazsky Jan 29 '12 at 19:09
then you will have downtime and what Book Of Zeus described might work. – abdelsaid Jan 29 '12 at 19:13
[root@v ~]# service mysqld stop
Stopping mysqld:                                           [  OK  ]

[root@v ~]# mkdir /home/info

[root@v ~]# cp -r /var/lib/mysql/ /home/info
[root@v ~]# chown -R mysql:mysql /home/info/mysql
[root@v ~]# chown -R mysql:mysql /home/info/mysql/*
[root@v ~]# chcon -R -t mysqld_db_t /home/info/mysql
[root@v ~]# vi /etc/my.cnf

# Disabling symbolic-links is recommended to prevent assorted security risks


[root@v ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@v ~]#
share|improve this answer
Would you mind to add an explanation about how this solves the issue? – dezso Jul 8 '15 at 8:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.