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

How to get the dynamic number of remaining words in textarea

I encountered a case at work that I had never wri...

Sharing some details about MySQL indexes

A few days ago, a colleague asked me a question a...

HTML Tutorial: Collection of commonly used HTML tags (5)

These introduced HTML tags do not necessarily ful...

Interpretation of syslogd and syslog.conf files under Linux

1: Introduction to syslog.conf For different type...

Summary of MySQL view principles and usage examples

This article summarizes the principles and usage ...

Web design tips on form input boxes

1. Dashed box when cancel button is pressed <br...

Simple example of HTML checkbox and radio style beautification

Simple example of HTML checkbox and radio style b...

How to prevent Flash from covering HTML div elements

Today when I was writing a flash advertising code,...

Web design tips on form input boxes

This article lists some tips and codes about form...

MySQL 5.7.15 installation and configuration method graphic tutorial (windows)

Because I need to install MySQL, I record the ins...

Reflection and Proxy in Front-end JavaScript

Table of contents 1. What is reflection? 2. Refle...

Set an icon for the website to be displayed on the far left of the browser tab

What is the purpose of this sentence? Copy code Th...

Getting Started Tutorial on GDB in Linux

Preface gdb is a very useful debugging tool under...