Change MySQL data directory

Ubuntu

Stop the service:

$ service MySQL stop

Create a new destination folder:

$ mkdir /new_path/mysql_data
$ cp -R -p /var/lib/MySQL /new_path/mysql_data

MySQL users should have sufficient permissions for the new folder /new_path/mysql_data.

Optionally modify the path of the old MySQL data store folder:

$ mv /var/lib/MySQL /var/lib/mysql_old

Edit the datadir property in the /etc/mysql/my.cnf configuration file:

$ vim /etc/mysql/my.cnf

Other possible config paths:

/etc/mysql/mariadb.conf.d/50-server.cnf

/etc/MySQL/MySQL.conf.d/mysqld.cnf

 

Replace the old data store folder path with the new one:

data-dir = /var/lib/MySQL

to

data-dir = /new_path/mysql_data

Enable AppArmor. Edit the file /etc/AppArmor.d/usr.in.mysqld:

$ vim /etc/apparmor.d/usr.sbin.mysqld

Replace the old data store folder path with the new one:

/usr/lib/MySQL/plugin/ r,
/usr/lib/MySQL/plugin/*.so* mr,

...

/var/lib/MySQL/ r,
/var/lib/MySQL/** rwk,

to

/new_path/mysql_data/plugin/ r,
/new_path/mysql_data/plugin/*.so* mr,

...

/new_path/mysql_data/ r,
/new_path/mysql_data/** rwk,

Start MySQL service:

$ service MySQL start

CentOS and RHEL

Stop the service:

$ service mysqld stop

Create a new destination folder:

$ mkdir /new_path/mysql_data
$ cp -R -p /var/lib/MySQL /new_path/mysql_data

The MySQL user must have sufficient permissions for the new folder /new_path/mysql_data.

Optionally modify the path of the old MySQL data store folder:

$ mv /var/lib/MySQL /var/lib/mysql_old

Edit the datadir property in the /etc/mysql/my.cnf configuration file:

$ vim /etc/mysql/my.cnf

Replace the old data store folder path with the new one:

data-dir = /var/lib/MySQL

to

data-dir = /new_path/mysql_data

Enable AppArmor.

$ yum install policycoreutils-python
$ getenforce
$ manage context -a -t mysqld_db_t "/new_path/mysql_data(/.*)?"
$ restorecon -Rv /new_path/mysql_data

Start MySQL service:

$ service mysqld start

Windows

Stop the service:

C:\> net stop mysql56

Create a new destination folder:

C:\> robocopy "C:\ProgramData\MySQL\MySQL Server 8\data" c:\new_path\mysql_data

Optionally modify the path of the old MySQL data store folder:

C:\> ren "C:\ProgramData\MySQL\MySQL Server 8\data" "C:\ProgramData\MySQL\MySQL Server 8\data_old"

Edit the datadir property in the C:\ProgramData\MySQL\MySQL Server 8\my.ini configuration file:

Replace the old data store folder path with the new one:

datadir = C:\ProgramData\MySQL\MySQL Server 8\data

to

data-dir = c:\new_path\mysql_data

Start MySQL service:

C:\> net start mysql56

Table of contents [ Hide Show ]