Monday, November 9, 2009

How to move mysql datadir to new location

By default the mysql install the datadir at /var/lib/mysql. Unfortunately, people usually did partition their hard disk, or buy a new hard drive later. In such a case, you would like to move the *huge* datadir to another disk to both boost the speed by parallel in I/O and utilize space smartly. Here's what you should do:


  1. Stop your mysql service.
    sudo /etc/init.d/mysql stop

  2. Copying all files from old location to new location and preserver the ownership and timestamp information.
    sudo cp -r -p /var/lib/mysql /newlocation/

  3. Update your mysql configuration file:
    sudo vim /etc/mysql/my.cnf
    datadir     = /newlocation/mysql #old: /var/lib/mysql

  4. Update the second configuration file for apparmor:
      /newlocation/mysql/ r, #/var/lib/mysql/ r,
    /newlocation/mysql/** rwk, #/var/lib/mysql/** rwk,

  5. (Optional) Rename the old folder to prevent confusing.
    sudo mv /var/lib/mysql /var/lib/mysql.bak

  6. Restart your apparmor & mysql service.
    sudo /etc/init.d/apparmor reload
    sudo /etc/init.d/mysql start

  7. Delete the old location
    sudo rm -rf /var/lib/mysql.bak


No comments:

Post a Comment