This is a good reference:
https://www.tecmint.com/change-default-mysql-mariadb-data-directory-in-linux/
[root@database-rhel84 lib]# cd /data
[root@database-rhel84 data]# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 8075472 0 8075472 0% /dev
tmpfs 8105800 0 8105800 0% /dev/shm
tmpfs 8105800 9492 8096308 1% /run
tmpfs 8105800 0 8105800 0% /sys/fs/cgroup
/dev/mapper/rhel_database--rhel84-root 31441920 5157720 26284200 17% /
/dev/mapper/rhel_database--rhel84-data 104806400 763756 104042644 1% /data
/dev/sda2 1038336 242324 796012 24% /boot
/dev/sr0 9892300 9892300 0 100% /cdrom
/dev/mapper/rhel_database--rhel84-home 11020288 123784 10896504 2% /home
/dev/sda1 613184 5892 607292 1% /boot/efi
tmpfs 1621160 1180 1619980 1% /run/user/42
tmpfs 1621160 0 1621160 0% /run/user/0
[root@database-rhel84 data]# mkdir mysql
[root@database-rhel84 data]# chown mysql:mysql mysql
[root@database-rhel84 data]# ls -l
total 0
drwxr-xr-x. 2 mysql mysql 6 Nov 12 22:37 mysql
[root@database-rhel84 data]# ls -l /var/lib/mssql
ls: cannot access '/var/lib/mssql': No such file or directory
[root@database-rhel84 data]# ls -l /var/lib/mysql
total 110628
-rw-rw----. 1 mysql mysql 24576 Nov 12 20:56 aria_log.00000001
-rw-rw----. 1 mysql mysql 52 Nov 12 20:56 aria_log_control
-rw-rw----. 1 mysql mysql 972 Nov 12 20:56 ib_buffer_pool
-rw-rw----. 1 mysql mysql 12582912 Nov 12 20:56 ibdata1
-rw-rw----. 1 mysql mysql 50331648 Nov 12 20:56 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 Nov 12 20:56 ib_logfile1
drwx------. 2 mysql mysql 4096 Nov 12 20:56 mysql
drwx------. 2 mysql mysql 20 Nov 12 20:56 performance_schema
drwx------. 2 mysql mysql 20 Nov 12 20:56 test
[root@database-rhel84 data]# date
Fri Nov 12 22:38:01 +08 2021
[root@database-rhel84 data]# systemctl stop mariadb
[root@database-rhel84 data]# systemctl is-active mariadb
inactive
[root@database-rhel84 data]# service mysqld stop
Redirecting to /bin/systemctl stop mysqld.service
Failed to stop mysqld.service: Unit mysqld.service not loaded.
[root@database-rhel84 data]# cp -R -p /var/lib/mysql/* /data/mysql
[root@database-rhel84 data]# pwd
/data
[root@database-rhel84 data]# mv /var/lib/mysql /var/lib/mysql.old
[root@database-rhel84 data]# vi /etc/my.cnf
[root@database-rhel84 data]# cd /etc/my.cnf.d/
[root@database-rhel84 my.cnf.d]# ls -l
total 16
-rw-r--r--. 1 root root 295 Nov 6 03:03 client.cnf
-rw-r--r--. 1 root root 763 Nov 6 03:03 enable_encryption.preset
-rw-r--r--. 1 root root 232 Nov 6 03:03 mysql-clients.cnf
-rw-r--r--. 1 root root 1080 Nov 6 03:03 server.cnf
[root@database-rhel84 my.cnf.d]# vi server.cnf
[root@database-rhel84 my.cnf.d]# cp -p server.cnf server.cnf.orig
[root@database-rhel84 my.cnf.d]# vi server.cnf
[root@database-rhel84 my.cnf.d]# ls -l /data
total 0
drwxr-xr-x. 5 mysql mysql 181 Nov 12 22:40 mysql
[root@database-rhel84 my.cnf.d]# mvdatadir=/mnt/mysql-data
[root@database-rhel84 my.cnf.d]# socket=/mnt/mysql-data/mysql.sock ^C
[root@database-rhel84 my.cnf.d]# mv /data/mysql/ /data/mysql-data
[root@database-rhel84 my.cnf.d]# ls -l
total 20
-rw-r--r--. 1 root root 295 Nov 6 03:03 client.cnf
-rw-r--r--. 1 root root 763 Nov 6 03:03 enable_encryption.preset
-rw-r--r--. 1 root root 232 Nov 6 03:03 mysql-clients.cnf
-rw-r--r--. 1 root root 1140 Nov 12 22:43 server.cnf
-rw-r--r--. 1 root root 1080 Nov 6 03:03 server.cnf.orig
[root@database-rhel84 my.cnf.d]# pwd
/etc/my.cnf.d
[root@database-rhel84 my.cnf.d]# ls -l /data
total 0
drwxr-xr-x. 5 mysql mysql 181 Nov 12 22:40 mysql-data
[root@database-rhel84 my.cnf.d]# ls -l
total 20
-rw-r--r--. 1 root root 295 Nov 6 03:03 client.cnf
-rw-r--r--. 1 root root 763 Nov 6 03:03 enable_encryption.preset
-rw-r--r--. 1 root root 232 Nov 6 03:03 mysql-clients.cnf
-rw-r--r--. 1 root root 1140 Nov 12 22:43 server.cnf
-rw-r--r--. 1 root root 1080 Nov 6 03:03 server.cnf.orig
[root@database-rhel84 my.cnf.d]# cp -p client.cnf client.cnf.orig
[root@database-rhel84 my.cnf.d]# vi client.cnf
[root@database-rhel84 my.cnf.d]#
[root@database-rhel84 my.cnf.d]#
[root@database-rhel84 my.cnf.d]# semanage fcontext -a -t mysqld_db_t "/mnt/mysql-data(/.*)?"
[root@database-rhel84 my.cnf.d]# restorecon -R /mnt/mysql-data
restorecon: lstat(/mnt/mysql-data) failed: No such file or directory
[root@database-rhel84 my.cnf.d]# semanage fcontext -a -t mysqld_db_t "/data/mysql-data(/.*)?"
[root@database-rhel84 my.cnf.d]# restorecon -R /data/mysql-data
[root@database-rhel84 my.cnf.d]# systemctl start mariadb
[root@database-rhel84 my.cnf.d]# systemctl is-active mariadb
active
[root@database-rhel84 my.cnf.d]# systemctl status mariadb
● mariadb.service - MariaDB 10.4.22 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/mariadb.service.d
└─migrated-from-my.cnf-settings.conf
Active: active (running) since Fri 2021-11-12 22:46:51 +08; 18s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Process: 37931 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 37882 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ] && systemctl se>
Process: 37880 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Main PID: 37899 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 30 (limit: 100943)
Memory: 73.6M
CGroup: /system.slice/mariadb.service
└─37899 /usr/sbin/mysqld
Nov 12 22:46:51 database-rhel84 mysqld[37899]: 2021-11-12 22:46:51 0 [Note] InnoDB: 10.4.22 started; log sequence number 60967; transaction id 20
Nov 12 22:46:51 database-rhel84 mysqld[37899]: 2021-11-12 22:46:51 0 [Note] InnoDB: Loading buffer pool(s) from /data/mysql-data/ib_buffer_pool
Nov 12 22:46:51 database-rhel84 mysqld[37899]: 2021-11-12 22:46:51 0 [Note] Plugin 'FEEDBACK' is disabled.
Nov 12 22:46:51 database-rhel84 mysqld[37899]: 2021-11-12 22:46:51 0 [Note] InnoDB: Buffer pool(s) load completed at 211112 22:46:51
Nov 12 22:46:51 database-rhel84 mysqld[37899]: 2021-11-12 22:46:51 0 [Note] Server socket created on IP: '::'.
Nov 12 22:46:51 database-rhel84 mysqld[37899]: 2021-11-12 22:46:51 0 [Note] Reading of all Master_info entries succeeded
Nov 12 22:46:51 database-rhel84 mysqld[37899]: 2021-11-12 22:46:51 0 [Note] Added new Master_info '' to hash table
Nov 12 22:46:51 database-rhel84 mysqld[37899]: 2021-11-12 22:46:51 0 [Note] /usr/sbin/mysqld: ready for connections.
Nov 12 22:46:51 database-rhel84 mysqld[37899]: Version: '10.4.22-MariaDB' socket: '/data/mysql-data/mysql.sock' port: 3306 MariaDB Server
Nov 12 22:46:51 database-rhel84 systemd[1]: Started MariaDB 10.4.22 database server.
[root@database-rhel84 my.cnf.d]# mysql -u root -p -e "SELECT @@datadir;"
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/mnt/mysql-data/mysql.sock' (2)
[root@database-rhel84 my.cnf.d]# systemctl stop mariadb
[root@database-rhel84 my.cnf.d]# vi client.cnf
[root@database-rhel84 my.cnf.d]# vi server.cnf
[root@database-rhel84 my.cnf.d]# systemctl start mariadb
[root@database-rhel84 my.cnf.d]# systemctl status mariadb
● mariadb.service - MariaDB 10.4.22 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/mariadb.service.d
└─migrated-from-my.cnf-settings.conf
Active: active (running) since Fri 2021-11-12 22:48:08 +08; 3s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Process: 38034 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 37987 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ] && systemctl se>
Process: 37985 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Main PID: 38003 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 30 (limit: 100943)
Memory: 71.7M
CGroup: /system.slice/mariadb.service
└─38003 /usr/sbin/mysqld
Nov 12 22:48:07 database-rhel84 mysqld[38003]: 2021-11-12 22:48:07 0 [Note] InnoDB: 10.4.22 started; log sequence number 60976; transaction id 20
Nov 12 22:48:07 database-rhel84 mysqld[38003]: 2021-11-12 22:48:07 0 [Note] InnoDB: Loading buffer pool(s) from /data/mysql-data/ib_buffer_pool
Nov 12 22:48:07 database-rhel84 mysqld[38003]: 2021-11-12 22:48:07 0 [Note] InnoDB: Buffer pool(s) load completed at 211112 22:48:07
Nov 12 22:48:07 database-rhel84 mysqld[38003]: 2021-11-12 22:48:07 0 [Note] Plugin 'FEEDBACK' is disabled.
Nov 12 22:48:07 database-rhel84 mysqld[38003]: 2021-11-12 22:48:07 0 [Note] Server socket created on IP: '::'.
Nov 12 22:48:07 database-rhel84 mysqld[38003]: 2021-11-12 22:48:07 0 [Note] Reading of all Master_info entries succeeded
[root@database-rhel84 my.cnf.d]# mysql -u root -p -e "SELECT @@datadir;"
Enter password:
+-------------------+
| @@datadir |
+-------------------+
| /data/mysql-data/ |
No comments:
Post a Comment