Using XtraBackup on a Big MySQL Instance
Percona Xtrabackup takes extra steps to ensure database backups are safe. Find out how to use it on a MySQL instance with a large number of tables.
Join the DZone community and get the full member experience.Join For Free
In this blog post, we’ll find out how to use Percona XtraBackup on a MySQL instance with a large number of tables.
As of Percona Xtrabackup 2.4.5, you are required to have enough open files to open every single InnoDB tablespace in the instance you’re trying to back up. So if you’re running innodb_file_per_table=1, and have a large number of tables, you’re very likely to see Percona XtraBackup fail with the following error message:
InnoDB: Operating system error number 24 in a file operation. InnoDB: Error number 24 means 'Too many open files' InnoDB: Some operating system error numbers are described at http://dev.mysql.com/doc/refman/5.7/en/operating-system-error-codes.html InnoDB: File ./sbtest/sbtest132841.ibd: 'open' returned OS error 124. Cannot continue operation InnoDB: Cannot continue operation.
If you run into this issue, here is what you need to do. First, find out how many files you need:
root@ts140i:~# find /var/lib/mysql/ -name "*.ibd" | wc -l 1000005
I would add at least another 1000 to this number for system tablespace and other miscellaneous open file needs. You might want to go even higher to accommodate for a growing number of tables.
Check the maximum number of files you can keep open in the system. If this number is too small Percona Xtrabackup might monopolize the open files in the system, causing other processes to fail when they try to open files. This can cause MySQL Server to crash, and other processes to fail.
root@ts140i:/mnt/data/backup# cat /proc/sys/fs/file-max 3262006
If you need to, here is how to increase the number:
sysctl -w fs.file-max=5000000 echo "fs.file-max=5000000" >> /etc/sysctl.conf
You can also increase the limit on the number of files the Percona XtraBackup process can open.
The best way to do this is using --open-files-limit option. For example, you can specify the following in your my.cnf:
Alternatively, you can pass it as a command-line option, or run ulimit -n 2000000 before running the backup command.
You need to be sure your user account has permissions to set open files limit this high. If you are doing backups under the “root” user, it shouldn’t be a problem. Otherwise, you might need to adjust the limits in /etc/security/limits.conf:
mysql hard nofile 2000000 mysql soft nofile 2000000
Specifying a “soft” limit in this file eliminates the need to run ulimit before Percona XtraBackup, or specifying it in the configuration.
There is one more possible limit to overcome. Even running as a root user, you might get the following error message:
root@ts140i:/mnt/data/backup# ulimit -n 2000000 -su: ulimit: open files: cannot modify limit: Operation not permitted
If this happens, you might need to increase the kernel limit on the number of processes any can have:
pz@ts140i:~$ cat /proc/sys/fs/nr_open 1048576
The limit I have on this system is slightly above 1 million. You can increase it using the following:
sysctl -w fs.nr_open=2000000 echo "fs.nr_open=2000000" >> /etc/sysctl.conf
With these configuration adjustments, you should be able to use Percona XtraBackup to backup MySQL instances containing millions of tables without problems.
What if you can’t allow Percona XtraBackup to open that many files? Then there is the option –close-files that won’t normally require increasing the limit to the number of open files. Using this option, however, might cause the backup corruption if you’re doing DDL operations during the backup.
From where does this strange limitation requiring you to keep all tablespaces open come? It comes from this issue. In some cases, DDL operations such as RENAME TABLE might cause the wrong file to be copied, and unable to be caught up by replying to InnoDB redo logs. Keeping the file open clearly shows which file corresponds to a given tablespace at the start of a backup process, and gets handled correctly.
This problem is not unique to Percona XtraBackup. If anything, Percona Xtrabackup goes the extra mile to ensure database backups are safe. For comparison, MySQL Enterprise Backup 4.0 simply states:
“Do not run the DDL operations ALTER TABLE, TRUNCATE TABLE, OPTIMIZE TABLE, REPAIR TABLE, RESTORE TABLE or CREATE INDEX while a backup operation is going on. The resulting backup might become corrupted.”
Published at DZone with permission of Peter Zaitsev, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comparing Cloud Hosting vs. Self Hosting
Security Challenges for Microservice Applications in Multi-Cloud Environments
What ChatGPT Needs Is Context
Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline