Using symbolic link on directory with contents

7,378

First, move or copy /var/lib/mysql to /srv/mysql.

$ mv -i /var/lib/mysql /srv
$ cp -ir /var/lib/mysql /srv

You may want to use a bind mount instead of a symbolic link. Bind mounts won't break in special roots.

$ mkdir /var/lib/mysql
$ mount --bind /srv/mysql /var/lib/mysql

if you decide to use a symbolic link, remove the old /var/lib/mysql directory and run the following.

$ ln -s /srv/mysql /var/lib/mysql
Share:
7,378

Related videos on Youtube

Mike
Author by

Mike

Updated on November 24, 2022

Comments

  • Mike
    Mike 12 months

    I'm working on a Debian machine with a bunch of different filesystems and I'm trying to manipulate a large amount of data in MySQL. I've run out of room in my home directory, but there's a ton of empty space in other systems (particularly one that's located in /srv). I want to make it so that /var/lib/mysql (the directory that stores MySQL data) seems to still be in its rightful location, but all of its data is stored in /srv/mysql. How can I do this?

  • Admin
    Admin over 10 years
    When I try that ln -s command I get: "ln: failed to create symbolic link `/var/lib/mysql/mysql': File exists". It may have something to do with the fact that /var/lib/mysql/mysql is a directory, but that seems like an error that shouldn't be happening regardless.
  • Admin
    Admin over 10 years
    You have to remove the old directory first. Make sure its contents have been copied to the new location (/srv/mysql) and run rm -r /var/lib/mysql.
  • Admin
    Admin over 10 years
    It seems like it worked, but MySQL no longer recognizes any of the databases and gives me an Error 1006 errno 2 when I try to make one.
  • Admin
    Admin over 10 years
    Try using a bind mount instead. Delete the symlink (rm /var/lib/mysql), make a new directory there (mkdir /var/lib/mysql), and bind mount the new directory on the old one (mount --bind /srv/mysql /var/lib/mysql).
  • Admin
    Admin over 10 years
    You'll have to add the mount to your /etc/fstab file for it to mount on boot. This line should work: /srv/mysql /var/lib/mysql bind defaults,bind 0 0. You could also change your mysql configuration to point to the new directory. Though, that may cause problems if you're using administration utilities that expect to find your mysql data at /var/lib/mysql.