mariadb-2-在centos中修改mariadb資料的儲存路徑

皮皮冰燃 發表於 2020-11-18
CentOS

(1)mariadb的預設儲存目錄
Mariadb 預設的資料儲存目錄為 /var/lib/mysql。
MariaDB [(none)]> create database mytest;
MariaDB [mytest]> create table student(name varchar(10),age int);
MariaDB [mytest]> insert into student(name,age) values (‘lucy’,11);
在這裡插入圖片描述
在這裡插入圖片描述新建的資料庫,在目錄中以資料夾的形式存在。
在這裡插入圖片描述
#mkdir /mydata建立需要遷移到的新目錄
#chmod 777 /mydata加上讀寫許可權
#systemctl stop mariadb.service停止Mariadb服務

(2)修改配置檔案/etc/my.cnf
修改datadir=/var/lib/mysql
修改log-error=/var/log/mariadb/mariadb.log

# include *.cnf from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

#datadir=/var/lib/mysql
datadir=/mydata/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
# 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 mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
log-error=/mydata/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

注意:不要修改socket和pid-file路徑,否者容易出錯。
(3)複製檔案
將/var/lib/mysql下面的檔案複製到/mydata/mysql,
將/var/log/mariadb/複製到/mydata/log/mariadb
#mkdir /mydata
#cp -a /var/lib/mysql/ /mydata/
#mkdir /mydata/log/
#cp -a /var/log/mariadb/ /data/log/
#chown -R mysql:mysql /mydata/

(4)注意:如果開啟了SELinux和防火牆,需要關閉SELinux和firewall
或者設定/data/、/data/log/的security context,開啟3306埠

修改security context(如果開啟了selinux,需要,否則跳過)
檢視/var/lib/mysql 的security context
#ls -Z /var/lib/mysql/
-rw-rw----. mysql mysql system_u:object_r:mysqld_db_t:s0 aria_log.00000001
-rw-rw----. mysql mysql system_u:object_r:mysqld_db_t:s0 aria_log_control
-rw-rw----. mysql mysql system_u:object_r:mysqld_db_t:s0 ibdata1
-rw-rw----. mysql mysql system_u:object_r:mysqld_db_t:s0 ib_logfile0
-rw-rw----. mysql mysql system_u:object_r:mysqld_db_t:s0 ib_logfile1
drwx------. mysql mysql system_u:object_r:mysqld_db_t:s0 mysql
drwx------. mysql mysql system_u:object_r:mysqld_db_t:s0 performance_schema
修改、恢復/data/mysql,/data/log的security context
#semanage fcontext -a -t mysqld_db_t “/data/mysql(/.)?"
#restorecon -R -v /data/mysql
#semanage fcontext -a -t mysqld_db_t "/data/log(/.
)?”
#restorecon -R -v /data/log
(5)重新啟動 Mariadb 服務
#systemctl start mariadb.service

在這裡插入圖片描述可以看到新的資料庫已經出現在遷移後的目錄裡了。
在這裡插入圖片描述