Monthly Archive for January, 2013

Move MySQL datadir

Moving my MySQL data files to a separate new partition in Linode.

This page is just really for my own notes… BACKUP YOUR DB AND SETTINGS FILES FIRST!
(Ubuntu 11.10)
Original database directory: /var/lib/mysql/
New directory: /srv/mysql/

  1. Log in to your Linode Manager
  2. Create the new disk image
    • Label: /srv
    • Type: ext3
    • Size: size needed for database (10000MB)
  3. Click Create Image and wait for it to finish in the queue
  4. Click edit for your ‘Configuration Profile’
  5. Under ‘Block Device Assignment’ assign the next available /dev/xvdX device to the newly created disk image
    • /dev/xvdc = /srv (The label you chose earlier)
  6. Click Save Changes
  7. You will need to reboot your server for it to see the new disk image so click “Reboot”
  8. Once it boots, log into it with a terminal
  9. Add the new disk image to /etc/fstab so it will auto mount at boot
    /dev/xvdc /srv ext3 defaults 0 2
  10. Mount it
    mount /srv
  11. Shutdown the mysql dameon.
    • service mysql stop
  12. Edit the MySQL config file: /etc/mysql/my.cnf
  13. Change the datadir to the new location inside the [mysqld] section
    • datadir = /var/lib/mysql
    • to
    • datadir = /srv/mysql
  14. If the server is using AppArmor then add in the following lines to /etc/apparmor.d/usr.sbin.mysqld
    /srv/mysql/ r,
    /srv/mysql/** rwk,
  15. Restart AppArmor
    service apparmor restart
  16. Move the datadir to its new location
    mv /var/lib/mysql/ /srv/
  17. Make a symlink just in case
    ln -s /srv/mysql /var/lib/mysql
  18. Now startup mysql
    service mysql start

old stats:

$df -h

Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda             9.7G  4.9G 4.4G   54% /
devtmpfs              249M  4.0K 249M    1% /dev
none                   50M  164K  50M    1% /run
none                  5.0M     0 5.0M    0% /run/lock
none                  249M     0 249M    0% /run/shm

 

$ cat /fstab

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>   <options>       <dump>  <pass>
dev             /dev            devtmpfs rw              0       0
proc            /proc           proc     defaults        0       0
/dev/xvda       /               ext3     noatime,errors=remount-ro 0       1
/dev/xvdb       none            swap     sw              0       0

new stats:

$ df -h

Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda             5.8G  1.4G  4.2G  26% /
devtmpfs              248M  4.0K  248M   1% /dev
none                   50M  168K   50M   1% /run
none                  5.0M     0  5.0M   0% /run/lock
none                  249M     0  249M   0% /run/shm
/dev/xvdc             9.7G  3.7G  5.9G  39% /srv

 

$ cat /etc/fstab

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>   <options>       <dump>  <pass>
dev             /dev            devtmpfs rw              0       0
proc            /proc           proc     defaults        0       0
/dev/xvda       /               ext3     noatime,errors=remount-ro 0       1
/dev/xvdb       none            swap     sw              0       0
/dev/xvdc       /srv            ext3     defaults        0       2