본문으로 바로가기

[Mysql/MariaDB] DB 데이터 경로 변경 (datadir)

category DB/MySQL-MariaDB 2022. 2. 19. 13:38

0. 환경

  • CentOS Linux release 7.9.2009
  • MariaDB - 10.4.24

 

1. 설정

1. datadir 확인

# mysql -u root -p

MariaDB [(none)]> show variables where Variable_name = 'datadir';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| datadir       | /var/lib/mysql/ |
+---------------+-----------------+

 

2. DB 정지

# systemctl stop mariadb

 

3. 새 디렉터리 생성

# mkdir /mysql_data

 

4. 기존 data 경로 복사

// 파일 복사
# rsync -av /var/lib/mysql /mysql_data

// 소유자 변경
# chown -R mysql.mysql /mysql_data

 

5. DB 설정 파일 변경

★ [mysqld], [client]를 꼭 작성합니다.

# vi /etc/my.cnf

[mysqld]
datadir=/mysql_data/mysql/
socket=/mysql_data/mysql/mysql.sock

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

 

6. SELinux 설정

  • SELinux 설정하지 않고 실행한 경우 실행이 되지 않고 다음과 같은 설정을 제안합니다.(SELinux 설정)
  • 또는 SELinux를 끄는 방법도 있습니다.
*****  Plugin catchall_labels (83.8 confidence) suggests   *******************

If you want to allow mysqld_safe_helper to have write access on the mysql_data directory
Then you need to change the label on mysql_data
Do
# semanage fcontext -a -t FILE_TYPE 'mysql_data'
where FILE_TYPE is one of the following: cluster_var_run_t, faillog_t, krb5_host_rcache_t, mysqld_db_t, mysqld_log_t, mysqld_tmp_t, mysqld_
Then execute:
restorecon -v 'mysql_data'


*****  Plugin catchall (17.1 confidence) suggests   **************************

If you believe that mysqld_safe_helper should be allowed write access on the mysql_data directory by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# ausearch -c 'mysqld_safe_hel' --raw | audit2allow -M my-mysqldsafehel
# semodule -i my-mysqldsafehel.pp

 

1안) SELinux 끄기

# vi /etc/sysconfig/selinux

SELINUX=disabled

# reboot

 

2안) SELinux 설정(권장)

# semanage fcontext -a -t mysqld_db_t "/mysql_data/mysql(/.*)?"

# restorecon -R /mysql_data/mysql

 

2. 확인

  • DB 실행을 해보고 각 변수를 확인해봅니다.
  • 설정이 알맞게 변경된 경우 기존 datadir는 삭제해도 됩니다.
# systemctl start mariadb

# mysql -u root -p

MariaDB [(none)]> show variables like 'datadir';
+---------------+--------------------+
| Variable_name | Value              |
+---------------+--------------------+
| datadir       | /mysql_data/mysql/ |
+---------------+--------------------+

MariaDB [(none)]> show variables like 'socket';
+---------------+------------------------------+
| Variable_name | Value                        |
+---------------+------------------------------+
| socket        | /mysql_data/mysql/mysql.sock |
+---------------+------------------------------+