如何修改RHEL 7.5 64位系统中MySQL的yum安装目标路径至自定义挂载点
Absolutely, you can get MySQL's data stored in your /dbs mount point instead of the default /var/lib/mysql—though using yum install directly won't let you redefine the installation paths for pre-built RPM packages (those are fixed at compile time), we can easily work around this by either migrating the data directory after installation, or pre-configuring it before setting up the service. Here's how to do both approaches:
Option 1: Migrate the Data Directory After Installing MySQL
This is the most straightforward method if you've already run (or plan to run) the yum install command first:
- Install MySQL but stop the service immediately (we don't want it writing data to the default directory yet):
sudo yum install mysql-server mysql sudo systemctl stop mysqld - Back up the default data directory just in case something goes wrong:
sudo cp -R /var/lib/mysql /var/lib/mysql_backup - Move the default MySQL data directory to your
/dbsmount point, then fix permissions (MySQL requires themysql:mysqluser/group to own this directory):sudo mv /var/lib/mysql /dbs/ sudo chown -R mysql:mysql /dbs/mysql sudo chmod -R 750 /dbs/mysql - Update MySQL's configuration to point to the new directory:
- Open the main config file (usually
/etc/my.cnfor/etc/my.cnf.d/mysql-server.cnfon RHEL 7.5) and modify thedatadirandsocketlines under the[mysqld]section:[mysqld] datadir=/dbs/mysql socket=/dbs/mysql/mysql.sock # Optional: Move logs to /dbs too to keep everything in one place log-error=/dbs/mysql/mysqld.log - Also update the client config (e.g.,
/etc/my.cnf.d/client.cnf) to match the new socket path, otherwise your MySQL client won't connect:[client] socket=/dbs/mysql/mysql.sock
- Open the main config file (usually
- Start the MySQL service, enable it to run on boot, and verify the new data directory:
sudo systemctl start mysqld sudo systemctl enable mysqld # Check if the datadir is correctly set mysql -e "SELECT @@datadir;"
Option 2: Pre-Configure the Data Directory Before Installation
If you want to avoid moving files post-install, you can set up the config first:
- Create the target directory in
/dbsand set the correct permissions upfront:sudo mkdir -p /dbs/mysql sudo chown -R mysql:mysql /dbs/mysql sudo chmod -R 750 /dbs/mysql - Create or edit
/etc/my.cnfto define the new data directory and socket path:[mysqld] datadir=/dbs/mysql socket=/dbs/mysql/mysql.sock [client] socket=/dbs/mysql/mysql.sock - Install MySQL via yum—this time, the initialization script will use your pre-defined
datadirinstead of the default:sudo yum install mysql-server mysql - Start and verify the service as before:
sudo systemctl start mysqld sudo systemctl enable mysqld mysql -e "SELECT @@datadir;"
A Quick Note on "Installation Paths"
If you were hoping to move the actual program files (like /usr/bin/mysql or /usr/lib64/mysql) to /dbs, that's not feasible with yum. RPM packages have fixed installation paths baked in, and changing that would require compiling MySQL from source with a custom --prefix flag—which is overkill for most use cases. The above methods focus on moving the data directory, which is almost always what users need when specifying a custom mount point (since that's where all the database storage lives).
内容的提问来源于stack exchange,提问作者Lawrence

