1

I have a fresh install of Centos 7.6 which includes standard partition scheme for Centos 7.6 install. Which includes a "/" directory partition of 50GB. I would like to move the default nysql (mariadb) data directory so I don't fill up the root partition with a big database. I want to move the directory to a directory in a user's home directory. MariaDB will not start after the changes and I don't understand the reason based on the error message.

I have tried following the instructions:

# Check the current data-dir
mysql -u root -p
MariaDB [(none)]> select @@datadir;

# Edit the my.cnf file and change:
[mysqld]
datadir=/home/development/mysql
socket=/home/development/mysql/mysql.sock
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
[client]
port=3306
socket=/home/development/mysql/mysql.sock


# Restart MariaDB
systemectl start mariadb

MariaDB fails to start and the following is output.

Dec 10 12:19:52 localhost.localdomain mariadb-prepare-db-dir[24554]: Database MariaDB is not initialized, but the directory /home/development/mysql is not empty, so initialization cannot be done.
Dec 10 12:19:52 localhost.localdomain mariadb-prepare-db-dir[24554]: Make sure the /home/development/mysql is empty before running mariadb-prepare-db-dir.

The directory in question is in fact empty. The permissions on the folder are mysql:mysql.

Also, I have disabled SELinux entirely with:

setenforce 0

Here the output of permissions on the home directory:

$ ls -haltr /home
total 4.0K
drwxr-xr-x.  3 root        root          25 Dec 10 15:41 .
dr-xr-xr-x. 17 root        root         242 Dec 10 15:59 ..
drwx------. 19 development development 4.0K Dec 10 18:00 development

$ ls -haltr /home/development/mysql
total 29M
drwx------.  2 mysql       mysql       4.0K Dec 10 17:56 mysql
drwx------.  2 mysql       mysql       4.0K Dec 10 17:56 performance_schema
-rw-rw----.  1 mysql       mysql         52 Dec 10 17:56 aria_log_control
-rw-rw----.  1 mysql       mysql        16K Dec 10 17:56 aria_log.00000001
-rw-rw----.  1 mysql       mysql       5.0M Dec 10 17:56 ib_logfile1
srwxrwxrwx.  1 mysql       mysql          0 Dec 10 17:56 mysql.sock
-rw-rw----.  1 mysql       mysql        18M Dec 10 17:56 ibdata1
-rw-rw----.  1 mysql       mysql       5.0M Dec 10 17:56 ib_logfile0
drwx------. 19 development development 4.0K Dec 10 18:00 ..
drwxrwxr-x.  4 mysql       mysql        165 Dec 10 18:01 

and the permissions of the mysql directory itself:

ls -haltr /home/development | grep mysql
drwxrwxr-x.  4 mysql       mysql        165 Dec 10 18:01 mysql
I'm Root James
  • 212
  • 3
  • 13

2 Answers2

1

Look at your systemd mariadb.service script. The default one includes ProtectHome to ensure mysqld cannot access anything under /home/ for security reasons.

Do you have an overwhelmingly good reason to deviate from default paths instead of using a bind-mount? SELinux and other security features are there for very good reasons.

Gordan Bobić
  • 971
  • 4
  • 11
0

Make sure the below actions are performed.

1- Clean data directory :

rm -rf /home/development/mysql/*

2- Give data directory the correct permissions :

chown -R mysql:mysql /home/development/mysql

3- Copy the content of the old directory with preserving the original permissions :

cp -R -p /var/lib/mysql/* /home/development/mysql

4- mysql.sock path should be the same on client & mysld section.

5- Start mariadb service :

systemctl start mariadb

EDIT :

I would suggest last thing to create another directory and try within it. If it still not work then go back to /var/lib/mysql backup your database, change /var/lib/mysql name and do a fresh install or initiate mariadb-install-db on the new dir and then restore your database. Also avoid putting a database under home dir, create a dedicated fs because if the user "development" are deleted the database will go with it.

Reda Salih
  • 241
  • 2
  • 7
  • I followed those instructions, but I still have the error: Dec 10 18:05:26 localhost.localdomain mariadb-prepare-db-dir[3753]: Database MariaDB is not initialized, but the directory /home/development/mysql is not empty, so initialization cannot be done. Dec 10 18:05:26 localhost.localdomain mariadb-prepare-db-dir[3753]: Make sure the /home/development/mysql is empty before running mariadb-prepare-db-dir. – I'm Root James Dec 10 '19 at 23:07
  • MariaDB keep complaining that the directory is not initialized and I have also tried a removable USB drive. – I'm Root James Dec 10 '19 at 23:14
  • Could you log the output with `ls -haltr` of the home dir ? – Reda Salih Dec 10 '19 at 23:22
  • drwxr-xr-x. 3 root root 25 Dec 10 15:41 . dr-xr-xr-x. 17 root root 242 Dec 10 15:59 .. drwx------. 19 development development 4.0K Dec 10 18:00 development – I'm Root James Dec 10 '19 at 23:23
  • And ls -haltr /home/developement/mysal ? – Reda Salih Dec 10 '19 at 23:26
  • I added the output of the permissions of the /home/development/mysql and it's contents to the OP. It looks to me that MySQL has write permissions on the correct directory. – I'm Root James Dec 10 '19 at 23:29
  • I think data directory is corrupted, when you have changed data dir have you stopped mariadb before ? i suggest you to add " innodb_force_recovery=3 innodb_purge_threads=0" and restart in recovery mode then execute "mysqlcheck --all-databases -p" and put the output – Reda Salih Dec 10 '19 at 23:41
  • I can change the data directory back to /var/lib/mysql and MariaDB will start fine. – I'm Root James Dec 10 '19 at 23:48
  • I would suggest last thing to create another directory and try within it. If it still not work then go back to /var/lib/mysql backup your database, change /var/lib/mysql name and do a fresh install ori initiate mariadb-install-db on the new dir and then restore your database. Also avoid putting a database under home dir, create a dedicate fs because if the user "developement" are deleted the database will go with it. – Reda Salih Dec 10 '19 at 23:58
  • YAY! Thanks! suggestion worked when I created a directory in the home root -> /home/mysql. I thought that I had tried that when I tried an external USB hard-drive. I'm not sure why A user directory is not OK, but If you add that suggestion to your answer I will accept it. – I'm Root James Dec 11 '19 at 00:13
  • Good ! Could you make it as resolved :) – Reda Salih Dec 11 '19 at 00:16
  • Can you make make that suggestion in your answer - that a user's home directory is not OK, and I will accept the answer. – I'm Root James Dec 11 '19 at 00:17
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/102092/discussion-between-jonnyjandles-and-reda-salih). – I'm Root James Dec 11 '19 at 21:11