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

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

How to change the MySQL database directory location under CentOS system

1. First we need to shut down MySQL. The command is as follows:

service mysqld stop

2. Then transfer the data. For safety reasons, we use the copy command cp to find the original directory of mysql first.

cd /var/lib
ls

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

cp -a mysql /home/mysql_data/

This will copy the database to /home/mysql_data

Note: (The -a parameter must be included, otherwise the permissions copied will be incorrect.)

If the database is large, it will take a long time and may time out. Please find relevant information on how to set ssh to not time out.

3. Then we modify the configuration files. There are three in total. Let me explain them one by one:

Modify the first file: Back up before modifying

cp /etc/my.cnf /etc/my.cnfbak
vi /etc/my.cnf

After opening, change the directory of datadir to /home/mysql_data/mysql .

Change socket to /home/mysql_data/mysql/mysql.sock . To be safe, you can comment out the original one and then add a new line to change it to the current directory.

Modify the second file: Back up before modifying

cp /etc/init.d/mysqld /etc/init.d/mysqldbak
vi /etc/init.d/mysqld

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

So just use the above command, it is also simple.

In the line datadir=/var/lib/mysql , change the path on the right side of the equal sign to your actual storage path: /home/mysql_data/mysql

Modify the third file: Back up before modifying

cp /usr/bin/mysqld_safe /usr/bin/mysqld_safebak
vi /usr/bin/mysqld_safe

Here we also modify the datadir directory to /home/mysql_data/mysql

4. Next, you need to create a mysql.sock link:

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

At this point all modifications are complete, start mysql

service mysqld start

Or restart linux

reboot

Check if Mysql is running normally. If it can start normally, there is basically no problem.

For more basic tutorials on MySQL, please click on the following related articles

You may also be interested in:
  • How to switch directories efficiently in Linux
  • How to determine whether files and directories exist in Linux shell
  • How to extract file names and directory names from Linux shell
  • Find the running nginx directory in Linux system
  • Batch extract all directories and file names under a folder in Linux
  • Detailed explanation of how to find files filtered by time in a directory in Linux
  • How to enter directory/folder in Linux without using CD command
  • Linux directory switching implementation code example

<<:  Use nginx + secondary domain name + https support

>>:  Summary of several common ways to abbreviate javascript code

Recommend

Pure CSS custom multi-line ellipsis problem (from principle to implementation)

How to display text overflow? What are your needs...

Detailed process of installing Jenkins-2.249.3-1.1 with Docker

Table of contents 1. Install Docker 2. Pull the J...

Detailed explanation of the watch listener example in vue3.0

Table of contents Preface The difference between ...

Detailed explanation of common commands in Docker repository

Log in docker login Complete the registration and...

Detailed explanation of the use of Join in Mysql

In the previous chapters, we have learned how to ...

Take you to understand the event scheduler EVENT in MySQL

The event scheduler in MySQL, EVENT, is also call...

Summarize the problems encountered in using Vue Element UI

Table of contents 1. DateTimePicker date selectio...

Summary of methods for querying MySQL user permissions

Introduce two methods to view MySQL user permissi...

Example of Vue routing listening to dynamically load the same page

Table of contents Scenario Analysis Development S...

Summary of the use of CSS scope (style splitting)

1. Use of CSS scope (style division) In Vue, make...

How to implement property hijacking with JavaScript defineProperty

Table of contents Preface Descriptors Detailed ex...

Writing and understanding of arrow functions and this in JS

Table of contents Preface 1. How to write functio...

Are you still Select *?

There are many reasons why an application is as s...