You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改RHEL 7.5 64位系统中MySQL的yum安装目标路径至自定义挂载点

Can I Change MySQL's Installation/Storage Path to /dbs on RHEL 7.5?

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:

  1. 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
    
  2. Back up the default data directory just in case something goes wrong:
    sudo cp -R /var/lib/mysql /var/lib/mysql_backup
    
  3. Move the default MySQL data directory to your /dbs mount point, then fix permissions (MySQL requires the mysql:mysql user/group to own this directory):
    sudo mv /var/lib/mysql /dbs/
    sudo chown -R mysql:mysql /dbs/mysql
    sudo chmod -R 750 /dbs/mysql
    
  4. Update MySQL's configuration to point to the new directory:
    • Open the main config file (usually /etc/my.cnf or /etc/my.cnf.d/mysql-server.cnf on RHEL 7.5) and modify the datadir and socket lines 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
      
  5. 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:

  1. Create the target directory in /dbs and set the correct permissions upfront:
    sudo mkdir -p /dbs/mysql
    sudo chown -R mysql:mysql /dbs/mysql
    sudo chmod -R 750 /dbs/mysql
    
  2. Create or edit /etc/my.cnf to define the new data directory and socket path:
    [mysqld]
    datadir=/dbs/mysql
    socket=/dbs/mysql/mysql.sock
    [client]
    socket=/dbs/mysql/mysql.sock
    
  3. Install MySQL via yum—this time, the initialization script will use your pre-defined datadir instead of the default:
    sudo yum install mysql-server mysql
    
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:22:27