The default data directory location is C:\Program Files\MySQL\MySQL Server 8.0\data , or C:\ProgramData\Mysql on Windows 7 and Windows Server 2008. The C:\ProgramData directory is hidden by default. You need to change your folder options to see the directory and contents.
Stop MySQL using the following command:
sudo /etc/init.d/mysql stop
Copy the existing data directory (default located in /var/lib/mysql
) using the following command:
sudo cp -R -p /var/lib/mysql /newpath
edit the MySQL configuration file with the following command:
sudo gedit /etc/mysql/my.cnf # or perhaps /etc/mysql/mysql.conf.d/mysqld.cnf
Look for the entry for datadir
, and change the path (which should be /var/lib/mysql
) to the new data directory.
In the terminal, enter the command:
sudo gedit /etc/apparmor.d/usr.sbin.mysqld
Look for lines beginning with /var/lib/mysql
. Change /var/lib/mysql
in the lines with the new path.
Save and close the file.
Restart the AppArmor profiles with the command:
sudo /etc/init.d/apparmor reload
Restart MySQL with the command:
sudo /etc/init.d/mysql restart
Now login to MySQL and you can access the same databases you had before.
you would have to copy the current data to the new directory and to change your my.cnf
your MySQL.
[mysqld]
datadir=/your/new/dir/
tmpdir=/your/new/temp/
You have to copy the database when the server is not running.
Quick and easy to do:
# Create new directory for MySQL data
mkdir /new/dir/for/mysql
# Set ownership of new directory to match existing one
chown --reference=/var/lib/mysql /new/dir/for/mysql
# Set permissions on new directory to match existing one
chmod --reference=/var/lib/mysql /new/dir/for/mysql
# Stop MySQL before copying over files
service mysql stop
# Copy all files in default directory, to new one, retaining perms (-p)
cp -rp /var/lib/mysql/* /new/dir/for/mysql/
Edit the /etc/my.cnf
file, and under [mysqld]
add this line:
datadir=/new/dir/for/mysql/
If you are using CageFS (with or without CloudLinux) and want to change the MySQL directory, you MUST add the new directory to this file:
/etc/cagefs/cagefs.mp
And then run this command:
cagefsctl --remount-all
First you should stop the mysql server. e.g.
# /etc/init.d/mysql stop
After that you should copy the old data directory (e.g. /var/lib/mysql) incl. privileges to your new directory via
# cp -R -p /var/lib/mysql /new/data/dir
now you can change in /etc/mysql/my.cnf
the data new and restart the server
# /etc/init.d/mysql restart
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With