Get the App
SLTechnology News&Howtos  ›  Database  › 

How to change the location of MySQL database directory under Linux (CentOS) system

Shulou Source: shulou.com Published: 2022-05-31 23:38:38 09月12日 Update

This article is about how to change the location of the MySQL database directory under Linux (CentOS) system. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

How to change the location of MySQL database directory under CentOS system

1. First of all, we need to close MySQL with the following command:

Service mysqld stop

2. Then transfer the data. For security, we use the replicated command cp to find the original directory of mysql.

Cd / var/libls

After running this command, you will see the directory of mysql, and then execute the cp command

Cp-a mysql / home/mysql_data/

So the database is copied to / home/mysql_data.

Note: (- a) this parameter must be taken, otherwise the permissions copied in the past will be incorrect. )

If the database is relatively large, the time will be relatively long, and it may time out. How to set up the method that ssh does not time out, please find the relevant information by yourself.

3. Then we modify the configuration file. There are a total of three. I will explain them one by one below:

Modify the first file: back up before modification

Cp / etc/my.cnf / etc/my.cnfbakvi / etc/my.cnf

Change the directory of datadir to / home/mysql_data/mysql after opening it.

Change socket to / home/mysql_data/mysql/mysql.sock. For security reasons, you can delete the original comment, then add a new line and change it to the current directory.

Modify the second file: back up before modification

Cp / etc/init.d/mysqld / etc/init.d/mysqldbakvi / etc/init.d/mysqld

Note: the exact location is / etc/rc.d/init.d/mysqld, because there is a / etc/init.d to / etc/rc.d/init.d mapping here.

So it's easy to use the above command.

Change the path to the right of the equal sign in the datadir=/var/lib/mysql line to your current actual storage path: / home/mysql_data/mysql

Modify the third file: back up before modification

Cp / usr/bin/mysqld_safe / usr/bin/mysqld_safebakvi / usr/bin/mysqld_safe

Here, also change the directory of datadir to / home/mysql_data/mysql

4. Below, you need to establish a link to mysql.sock:

Ln-s / home/mysql_data/mysql/mysql.sock / var/lib/mysql/mysql.sock

Now that all the changes have been completed, start mysql

Service mysqld start

Or restart linux

Reboot

Check to see if Mysql is working properly, and if it starts up properly, there will be basically no problem.

Thank you for reading! On "how to change the MySQL database directory location under the Linux (CentOS) system" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Tags: Directory data command location database file system backup first security one line three content more article path run no good practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Docker MySQL vpn NVidia