Change MySQL data directory

Ubuntu

Stop the service:

$ service MySQL stop

Create a new folder destination:

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

MySQL users should have enough credentials granted to the new folder /new_path/mysql_data.

Optionally modify the path of older MySQL data store folder location:

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

Edit the datadir property into 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

 

Change the older data store folder path with the newer:

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

Change the older data store folder path with the newer:

/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 folder destination:

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

The MySQL user must have enough credentials granted to the new folder /new_path/mysql_data.

Optionally modify the path of older MySQL data store folder location:

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

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

$ vim /etc/mysql/my.cnf

Change the older data store folder path with the newer:

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 folder destination:

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

Optionally modify the path of older MySQL data store folder location:

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

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

Change the older data store folder path with the newer:

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 ]