Changing MySQL data directory in CentOS is a very 6 simple steps.

You need root access to your CentOS. Login and do followings

  1. stop the mysql
    1
    2
    3
    #
    # /etc/init.d/mysqld stop
    #
  2. copy all the current data to your destination directory (you need to decide where you want to move your mysql data)
    1
    2
    3
    #
    # cp /var/lib/mysql/ /mysqldata/mysql/ -R
    #
  3. give your new directory proper rights
    1
    2
    3
    #
    # chown mysql.mysql /mysqldata/mysql/ -R
    #
  4. Add new directory path to you my.cnf
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    # nano /etc/my.cnf
    #
    #
    [mysqld]
    #datadir=/var/lib/mysql
    datadir=/mysqldata/mysql/
    socket=/var/lib/mysql/mysql.sock
    user=mysql
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
     
    [mysqld_safe]
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid

    see the highlighted lines we changed the “datadir” to our new Path.

  5. Optional you should remove or atleast rename the actual MySQL directory. Just to make sure that you are using new data directory
    1
    2
    3
    #
    # mv /var/lib/mysql /var/lib/mysql_ORIGNAL
    #
  6. Start Mysql
    1
    2
    3
    #
    # /etc/init.d/mysqld start
    #