[로키 리눅스 or MYSQL] 록키 리눅스에서 MY_SQL 지정 디렉토리 공간으로 데이터 저장되게 하기

작성자: okkerberg
작성일: 2025.12.04
조회수: 96

아래 커맨드 입력
mysql -uroot -p -e "SHOW VARIABLES LIKE 'datadir';" 

 

예상 출력
+---------------+-------------------------+
| Variable_name | Value                   |
+---------------+-------------------------+
| datadir       | /var/lib/mysql/         |
+---------------+-------------------------+

 

설정 파일에서 직접 확인
grep -R "datadir" /etc/my.cnf /etc/my.cnf.d
결과
/etc/my.cnf.d/server.cnf: datadir=/var/lib/mysql
마리아 DB
/etc/my.cnf.d/mariadb-server.cnf
/etc/my.cnf.d/server.cnf
 

1.MY_SQL 정지
sudo systemctl stop mysqld
# 또는 MariaDB라면
sudo systemctl stop mariadb

 

sudo mkdir -p /home/mysql_data
sudo chown -R mysql:mysql /home/mysql_data
sudo chmod 750 /home/mysql_data

기존 데이터 전체 복사
sudo dnf install -y rsync
sudo rsync -avh /var/lib/mysql/ /home/mysql_data/
 

SELinux 적용
sudo semanage fcontext -a -t mysqld_db_t "/home/mysql_data(/.*)?"
sudo restorecon -Rv /home/mysql_data

MySQL 설정파일에서 datadir 변경
/etc/my.cnf.d/mariadb-server.cnf
또는
/etc/my.cnf.d/server.cnf

#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld/mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld]
datadir=/home/mysql_data
socket=/home/mysql_data/mysql.sock
#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
innodb_buffer_pool_size = 8G
tmp_table_size = 64M
max_heap_table_size = 64M
sort_buffer_size = 4M

ft_min_word_len=1
innodb_ft_min_token_size=1
#
# * Galera-related settings
#
[galera]
# Mandatory settings
#wsrep_on=ON
#wsrep_provider=
#wsrep_cluster_address=
#binlog_format=row
#default_storage_engine=InnoDB
#innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0

 

##아래는 수동으로 추가

[client]
socket=/home/mysql_data/mysql.sock

[mysql]
socket=/home/mysql_data/mysql.sock


# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

# This group is only read by MariaDB-10.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.5]

MySQL 재시작
sudo systemctl start mariadb
sudo systemctl status mariadb

 

목록으로