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
- Stop the MySQL
# /etc/init.d/mysqld stop
- Change the data directory
# cp –rap /var/lib/mysql /data/mysql-data # chown mysql.mysql /data/mysql-data
- Edit MySQL configuration file ( /etc/my.cnf)
datadir=/data/mysql-data
- 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
- Stop the MySQL
# /etc/init.d/mysqld stop
- Create the data directory
# mysql_install_db --datadir=/data/mysql-data --user=mysql
- Edit MySQL configuration file ( /etc/my.cnf)
datadir=/data/mysql-data
- 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.