How to change the MySQL database file directory in Ubuntu

How to change the MySQL database file directory in Ubuntu

Preface

The company's Ubuntu server places the directories of various systems on different logical partitions. For example, the default directory /var/lib/mysql for storing MySQL database files is located in a separate partition, and the system directory /var/ is located in a separate partition. However, the size of this partition is fixed and is not enough to store the entire database file. However, the size of another system directory /data reaches T level, which greatly meets the needs, so there is a need to change the database file directory.

The following is the process I tried by referring to some articles on the Internet:

1. Stop the database service:

Use /etc/init.d/mysql stop or stop mysql

2. Create a directory for the database file (such as /mysqldb) at the target location (/data) and copy (if you no longer use the default location, you can directly mv the original database file to the new directory) the original database file directory to this directory:

cd /data

mkdir mysqldb

cp -r /var/lib/mysql /data/mysqldb/

After a period of waiting, there is a copy of the original default mysql database file directory "/mysql" under /data/mysqldb/

3. Modify the my.cnf file

# vim /etc/mysql/my.cnf

Change datadir = /var/lib/mysql datadir = /data/mysqldb/mysql

In addition, since the current my.cnf has socket = /var/run/mysqld/mysqld.sock (not sock = /var/lib/mysql/mysql.sock as mentioned on the Internet), that is, the location of the socket is not where the database file is located, you do not need to do what other web pages say, and use the following command to make a mysql.sock link:

ln -s /data/mysqldb/mysql/mysql.sock /var/lib/mysql/mysql.sock (need to copy a copy from /home/data/mysql)

4. Modify the database permissions:

# chown -R mysql:mysql /data/mysqldb/mysql/ ← Change the ownership of the database file directory to mysql

# chmod 700 /data/mysqldb/mysql/whois/ ← Change the whois attribute of the database directory to 700

# chmod 660 /data/mysqldb/mysql/whois/* ← Change the attribute of the data table in the database to 660

5. Modify the file usr.sbin.mysqld

# vim /etc/apparmor.d/usr.sbin.mysqld

Bundle

/var/lib/mysql r,

/var/lib/mysql/** rwk,

Change to

/data/mysqldb/mysql/ r,

/data/mysqldb/mysql/** rwk,

Note: Without this step, the database service will not be able to restart. It seems to be restarting, but it is stuck and unresponsive.

6. Start mysql server

/etc/init.d/apparmor restart

/etc/init.d/mysql restart (or use restart mysql)

Done!

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • How to change the database data storage directory in MySQL
  • How to implement Mysql switching data storage directory
  • Steps to change the mysql database directory in Linux
  • Detailed explanation of mysql5.5 database data directory migration method
  • centos mysql modify database directory
  • How to modify the storage directory of Mysql database?
  • How to modify the mysql data directory under Win2008 r2
  • How to relocate the MySQL data directory
  • Tutorial on creating a temporary directory for MySQL in memory

<<:  How to install MySQL and MariaDB in Docker

>>:  Vue+spring boot realizes the verification code function

Recommend

MYSQL METADATA LOCK (MDL LOCK) MDL lock problem analysis

1. Introduction MDL lock in MYSQL has always been...

Solution to the problem of failure to insert emoji expressions into MySQL

Preface I always thought that UTF-8 was a univers...

In-depth understanding of the matching logic of Server and Location in Nginx

Server matching logic When Nginx decides which se...

JavaScript to implement retractable secondary menu

The specific code for implementing the retractabl...

Diagram of the process of implementing direction proxy through nginx

This article mainly introduces the process of imp...

MySQL 8.0.11 Installation Guide for Mac

MAC installs mysql8.0, the specific contents are ...

Getting Started with Front-End Vue Unit Testing

Table of contents 1. Why do we need unit testing?...

Detailed explanation of important cascading concepts in CSS

Recently, I encountered a problem in the process ...

Sample code for implementing radar chart with vue+antv

1. Download Dependency npm install @antv/data-set...

Encapsulate the navigation bar component with Vue

Preface: Fully encapsulating a functional module ...

Docker+selenium method to realize automatic health reporting

This article takes the health reporting system of...

CSS flexible layout FLEX, media query and mobile click event implementation

flex layout Definition: The element of Flex layou...