I am trying to move the data directory of my MySQL database to a second disk array that I have as a mount point /array2/
.
The problem I am having is I have tried everything and after I modify the location of datadir in my.cnf mysql will not start up again.
All I get is:
start: Job failed to start
Forgot about app armour.
For anyone that is interested I did the following to move the folder.
Stop the mysql server:
Create the new directory:
Copy over ONLY the database folders:
Backup the
my.cnf
file:Edit the
my.cnf
file:Change all mentions of the old datadir and socket to your new location
Mine became:
Update the directory permissions:
Rename the old directory:
Create a symlink, just in case:
Let AppArmor know about the new datadir:
Reload the apparmor profiles
Then start mysql:
I found that AppArmor was the culprit by examining the
syslog
, and was able to successfully change themysql
data location by following this process.Please note that, in files edited below, lines starting with
+
were added, and lines starting with-
were removed. You should not actually type/paste the+
signs when adding lines to these files.I cloned the
mysql
directory to the new location:Then I edited the
datadir
line in/etc/mysql/my.cnf
:Then I edited
/etc/apparmor.d/usr.sbin.mysqld
:Then I restarted
mysql
.Be aware.
If you have InnoDB tables you MUST copy over the
ibdata*
andib_logfile*
files or you will not be able to use the the tables. You will get:errors.
Run this copy command to copy over the
ibdata*
andib_logfile*
files.I prefer to use mount with bind option so I avoid any further changes in Apparmor and Mysql configuration.
For example:
Suppose I want to move everything in
/var/www
. Lets say this dir is my dev environment and it is mounted in a different partitionFirst we need to stop mysql:
We move files (preserving permission)
This will generate a directory
/var/www/mysql/
with all the content.We remove everything in the old directory:
We mount the new directory with
bind
option in the old one.edit
/etc/fstab
and add this line:This will mount the
/var/www/mysql
in our empty dir/var/lib/mysql
The
bind
option here do the magic, it will populate/var/lib/mysql
with the content of/var/www/mysql
so for mysql and apparmor it will be the like nothing has changed.Now we do the mount:
and restart mysql.
I just tried another way, that some may find useful:
copy with permissions intact:
back up (in case something goes wrong):
create a new empty directory in place of old:
bind mount the new location to the old:
Hope that helps someone, because symlinking didn't work for me, and this was the simplest way
If you move your datadir, you not only need to give the new datadir permissions, but you need to insure all parent directories have permission.
I moved my datadir to a hard drive, mounted in Ubuntu as:
and my datadir was Databases.
I had to set permissions to 771 to each of the media, user and Data directories:
If this does not work, another way you can get mysql to work is to change user in /etc/mysql/my.cnf to root; though there are no doubt some issues with doing that from a security perspective.
You realize if you follow the commands verbatim from the accepted answer it will fail?
cp -R /var/lib/mysql /array2/mysql
Will copy /var/lib/mysql to /array2/mysql/mysql.
When you set your configuration file you better be putting in /array2/mysql/mysql as the directory or else your mysqld will fail to start.
The better copy commands would be:
cp -R /var/lib/mysql /array2/ cp -R /var/lib/mysql/users /array2/mysql
Just my 2 cents worth.