Change MySQL Data Directory

Comments Off on Change MySQL Data Directory

Change MySQL Data Directory

In Redhat/CentOS/Fedora MySQL default data directory remains in /var/lib/mysql. Sometimes you need to change MySQL data directory to a new location for many reasons. To change MySQL data directory is an easy task. Here I am showing step by step procedure to change MySQL data directory from its default location /var/lib/mysql to /data/mysql-data

Change data directory from existing installation

  1. Stop the MySQL
    # /etc/init.d/mysqld stop
  1. Change the data directory
    # cp –rap /var/lib/mysql /data/mysql-data
    #  chown mysql.mysql /data/mysql-data
    
  1. Edit MySQL configuration file ( /etc/my.cnf)
    datadir=/data/mysql-data
  1. Start the MySQL
    # /etc/init.d/mysqld start

 

Create fresh data directory for new installation

When you have no existing data or it is a fresh MySQL, you can change MySQL data directory as follows

  1. Stop the MySQL
    # /etc/init.d/mysqld stop
  1. Create the data directory
    # mysql_install_db --datadir=/data/mysql-data --user=mysql
  1. Edit MySQL configuration file ( /etc/my.cnf)
    datadir=/data/mysql-data
  1. Start the MySQL
    # /etc/init.d/mysqld start

Here, all the commands showed are based on Redhat/CentOS/Fedora. However, the procedure is more or less same for all Linux distributions.