db/mysql

Centos7 Mariadb datadir change

C/H 2018. 3. 7. 15:30

MariaDB 설치

yum install mariadb-server mariadb -y
systemctl status mariadb
systemctl enable mariadb

PHP db 관련 기능 설치

sudo yum install php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached

datadir 위치변경

datadir 위치확인

select @@datadir;
/var/lib/mysql

mysql datadir 변경

sudo vi /etc/my.cnf
[mysqld]
# datadir=/var/lib/mysql
datadir=/home/mysql_data
# socket=/var/lib/mysql/mysql.sock
socket=/home/mysql_data/mysql.sock
    
[client]
socket=/home/mysql_data/mysql.sock

DIR 변경

cp -rfp /var/lib/mysql/* /home/mysql/
mv /var/lib/mysql /var/lib/mysql.back
ln -sf /home/mysql /var/lib/mysql

SELinux 권한설정

semanage fcontext -a -t mysqld_db_t "/home/mysql(/.*)?"
restorecon -R /home/mysql
systemctl start mysql

mysql Protect 변경

sudo vi /usr/lib/systemd/system/mariadb.service
# Prevent accessing /home, /root and /run/user
#ProtectHome=true
ProtectHome=false

php.ini msyql.sock 변경

pdo_mysql.default_socket=/home/mysql_data/mysql.sock
mysql.default_socket =/home/mysql_data/mysql.sock
mysqli.default_socket =/home/mysql_data/mysql.sock

자동시작 등록

mysql_install_db --datadir=/home/mysql

systemctl start mysql|mariadb 에러

이래저래 /ect/init.d/mysql start --datadir=/home/mysql 은 정상적으로 작동하지만 systemctl start mysql|mariadb 가 되지 않는다면 systemctl 서비스를 변경하라.

cd /usr/lib/systemd/system
mv mariadb.service mariadb.service.back

vi mariadb.service
[Unit]
Description = MySQL Server
After = network.target

[Service]
Type = forking
ExecStart = /etc/rc.d/init.d/mysql start
ExecStop = /etc/rc.d/init.d/mysql stop

[Install]
WantedBy = multi-user.target


systemctl daemon-reload
systemctl start mysql|mariadb
systemctl stop


반응형