Archive

Archive for May, 2016

Move MySQL data directory from one folder to another

May 15th, 2016 Comments off

Moving your MySQL data from one folder to another sounds like a big job, but in reality it is not really.
Before we get started you need to bear a couple of things in mind

  • Be sure to read this entire post before you do anything.
  • Especially read the part about the ‘socket’.
  • Be sure to understand everything that is in the post
  • Some versions of unix/linux use different terminology, (for example to stop/start a service), be sure that you use the correct command.
  • Be sure you are logged in as root.
  • Remember that there will be down time!
    The length of time depends a lot on the size of the data and where you are transferring to/from.
  • Be prepared to roll back the changes if things don’t work!
    • Stop the service.
    • Copy the original my.cnf file, (see below to locate it)
    • Start the service.
  • This is all done at your own risk! If you are not careful you could really trash your data!
  • Read the errors you might get, learn about your system, normally the error should help you solve most issues.
  • Try it on your test/dev server before you do anything, (you have a test server don’t you!!)

Locate the datadir.
This is where MySQL currently has all the data saved, everything is saved in one place.

Run the script, (with phpMyAdmin or something), below and look for the entry that says “datadir”.

# locate the current data directory
SHOW VARIABLES WHERE Variable_Name LIKE “%dir”

Locate the my.cnf.
This is the file that contains the MySQL configuration, this is a very important point and you need to do it carefully.

There could be more than one file so you will need to check all of them!!!

# locate my.cnf, (the very fist line been returned from the cmd below).
# more than one could be given
mysqld –help –verbose

This will return a whole lot of information, but the very first line will contain the locations.

  • Not all the files will exist
  • Maybe _none_ of the files will exist
  • If none exist, create a file called “my.cnf” in the first folder in the list.
  • If any of the files exist, look in any of them for the entry “datadir=…”
    (of course making sure it corresponds to the datadir you located in the previous step.

Stop the MySQL service.
This is a typical case where you need to choose the right command for your repo… do your homework first…

service mysql stop
# or maybe
# /etc/init.d/mysql stop

Copy the data from the old folder to the new folder

# copy the data over, (change the directories).
# rm -rf /home/mysql/*
cp -R -p /var/lib/mysql /home/mysql

You must learn what this means, “cp -R -p” means, copy the data from the source folder to the destination folder, (“/var/lib/mysql” and “/home/mysql” in my case).

But what this also does is keep the permissions of the folder, normally a user “mysql” is created and has full permissions to that folder.

For things to keep working, you must make sure that the permissions remain.

Spend a bit of time making sure that …

  • All the files were copied
  • All the permissions have been kept.

Edit the datadir.
In the my.cnf that you edited/created earlier change, (or add), the datadir.

[mysqld]
datadir=/home/mysql
#innodb_data_home_dir
#innodb_log_group_home_dir=/home/mysql/

  • There could be other values, only edit/add the datadir=, don’t change anything else!
  • In your first step, where you located the directory, look for …
    • innodb_data_home_dir=, if the directory is the same/similar to the one you just moved, then either add an entry or edit the one that is there with the same folder.
    • innodb_log_group_home_dir=, same as above.

Start the MySQL service.

service mysql start
# or maybe
# /etc/init.d/mysql start

Immediately make sure that the server is up and running

mysqladmin status
# or using phpmyadmin# or using whatever tool you fancy…

If it is running ok, make sure that the directories are indeed valid, (same step as earlier).

# locate the current data directory
SHOW VARIABLES WHERE Variable_Name LIKE “%dir”

If the server is not started you might get a ‘socket’ error, in that case, look for where MySQL thinks it is…

SHOW VARIABLES WHERE Variable_name = “socket”

It might point to the old directory, in that case you need to edit the config one more time.

But first stop the service…

service mysql stop
# or maybe
# /etc/init.d/mysql stop

Add two socket section, (one for the client and one for mysql itself)

[client]
# …
socket=/home/mysql/mysql.sock

[mysqld]
# …
socket=/home/mysql/mysql.sock

Of course, in my example, I am using my directory and I am only showing what needs to be edited/added.

Restart the service…

service mysql start
# or maybe
# /etc/init.d/mysql start

And make sure that it is all good!

Delete the old data

If everything is working I would suggest that you back up the old data somewhere else.

To make really sure that all us good, simply rename the old folder, (don’t move it or anything, just rename), and make sure that things are working).

Categories: development Tags: , , , ,