Innobackupex backup [ERROR] failed to set datadir to /var/lib/mysql/]

xtrabackup version: 2.4
mysql version: 5.7

I follow the document backup my database and restore it, but it’s something wrong

step1 Backup server1 mysql5.6

innobackupex --defaults-file=~/.my.cnf \
--datadir=/alidata1/mysql_multi/data3307/mydata/ \
--backup /data/backups/

step2: copy backup file to server2

step3: prepare the backup

xtrabackup --prepare --target-dir=/data/backups/2023-08-07_23-52-16

step4: set server2 mysql5.7

service mysqld stop
rm -rf /var/lib/mysql/*

step5: xtrabackup copy back

xtrabackup --copy-back --target-dir /data/backups/2023-08-07_23-52-16/

step:5 restart mysql5.7

chown -R mysql:mysql /var/lib/mysql/*
service mysqld start

the journalctl -xe

-- Unit mysqld.service has begun starting up.
Aug 08 01:35:52 vm1 mysqld[19214]: mysqld: Can't change dir to '/var/lib/mysql/' (Errcode: 13 - Permission denied)
Aug 08 01:35:52 vm1 mysqld[19214]: 2023-08-07T17:35:52.738901Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
Aug 08 01:35:52 vm1 mysqld[19214]: 2023-08-07T17:35:52.739176Z 0 [Warning] Can't create test file /var/lib/mysql/vm1.lower-test
Aug 08 01:35:52 vm1 mysqld[19214]: 2023-08-07T17:35:52.739257Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.43) starting as process 19217 ...
Aug 08 01:35:52 vm1 mysqld[19214]: 2023-08-07T17:35:52.742877Z 0 [Warning] Can't create test file /var/lib/mysql/vm1.lower-test
Aug 08 01:35:52 vm1 mysqld[19214]: 2023-08-07T17:35:52.742942Z 0 [Warning] Can't create test file /var/lib/mysql/vm1.lower-test
Aug 08 01:35:52 vm1 mysqld[19214]: 2023-08-07T17:35:52.743058Z 0 [ERROR] failed to set datadir to /var/lib/mysql/
Aug 08 01:35:52 vm1 mysqld[19214]: 2023-08-07T17:35:52.743085Z 0 [ERROR] Aborting
Aug 08 01:35:52 vm1 mysqld[19214]: 2023-08-07T17:35:52.743137Z 0 [Note] Binlog end
Aug 08 01:35:52 vm1 mysqld[19214]: 2023-08-07T17:35:52.743229Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
Aug 08 01:35:52 vm1 systemd[1]: mysqld.service: control process exited, code=exited status=1
Aug 08 01:35:52 vm1 mysqld[19214]: Initialization of mysqld failed: 0
Aug 08 01:35:52 vm1 systemd[1]: Failed to start MySQL Server.
-- Subject: Unit mysqld.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel

Hi @seqwait

First, please do not use innobackupex anymore, this is not related to your problem, but this tool is no longer maintained and on 2.4 you should be using xtrabackup --backup.

Comming to your problem, seems like the mysql user is not able to change dir to /var/lib/mysql:

Aug 08 01:35:52 vm1 mysqld[19214]: mysqld: Can’t change dir to ‘/var/lib/mysql/’ (Errcode: 13 - Permission denied)

Even thought you changed the permissions on every file or folder inside /var/lib/mysql/* it seems like the root directory is still owned by someone else, or your are configuring mysqld service to use other user.

Can you please try:
sudo chown -R mysql.mysql /var/lib/mysql/ - note that here we are not doing the /* part.

1 Like