MySQL
About
MySQL (or MariaDB) creates a db named zm after ZoneMinder is installed.
$ mysql -u root -p > >use zm; >show tables; >select * from Monitors\G
Setup
Follow installation guides in Documentation
Backup
You can backup the database, without much resource strain. Note that this backs up ONLY the configuration and events metadata. It does not backup any videos or images. See this link on the forums
/usr/bin/zmaudit.pl will delete any database entries that don't have corresponding images on the filesystem. This means simply, that backing up a DB will backup your configuration, but not anything else, and when zmaudit.pl runs it will clean the DB of orphaned events.
Optimization
MySQLTuner
mysqltuner
Then read the output, and perform any recommended database tweaks.
Other
mysqlcheck -u root -p --optimize --databases zm
This will attempt to optimize your databases. Functions are limited with InnoDB format, however.
Troubleshooting
API Can't Connect
If you change the password from the default, the API CakePHP config files will need to have their password changed as well.
IBData files Large
In ZoneMinder 1.28, I had an issue with the ibdata1 file in /var/lib/mysql/ growing too large. It includes some database information and in my 10GB root partition, was taking up 8GB. This was because the DB was not in InnoDB format. Newer Zoneminder, I think, default to InnoDB.
The solutions I found were:
- backup zm database, delete zm db, delete ibdata file, then restore database How to Shrink/Purge Ibdata1 file
OR
- Move the ibdata file to another partition
OR
- Change DB type to InnoDB (requires backup, deletion, and restoring db, per first solution)
Changing the database type to have an innodb file per each table as mentioned in the "how to shrink purge ibdata1 file in mysql" link will keep less data used in the ibdata1 file in the future, allowing the former to be deleted when not needed. On the other hand the ibdata file by default, will not shrink, ever. This may not be an issue in MariaDB.
Looking for the least invasive procedure, I went with moving /var/lib/mysql, and adding the optional my.cnf parameter. This required the following tricks (may only apply to Ubuntu 14.04).
There are a number of guides on moving Mysql, yet many of them omit adding the alias to apparmors settings. This is required. Failing to do so will result in "Job failed to start" when mysql is run with #service mysql start
.
A guide that covers all the steps required to move mysql on Ubuntu Trusty without omitting anything is here: Ask Ubuntu: Moving Mysql datadir Note that within my mysql installation there was no socket file in /var/lib or in my.cnf.
Backup/Restore Mysql DB
After moving the Data directory, I ended up backing up the zm db and restoring it anyways, in order to get the ibdata files to split correctly. This is not hard to do. The only DB you need to mysqldump from a stock ZM installation is the ZM db. And it's also the only DB you need restore.
For a full walkthrough on converting a MyISAM DB to InnoDB (also covers backing up ZM DB) see Enable and convert MySQL to innodb file per table for Zoneminder.
MySQL server has gone away error with ZMTrigger
See ZMTrigger#MySQL_server_has_gone_away_error
MySQL Out Of Memory
If you have recently added more cameras (especially higher resolution and framerate) and you find that periodically ZM is crashing, it may be caused by MySQL running out of RAM. As an example, I have 26 cameras, ranging from 1024x720 to SD analog resolution with framerates of 3 for the HD, and 5 for the SD. This is running under 8GB of RAM. If I add two more 1024x720 cameras with a higher framerate of 5 or 6 (and double the ZMA/ZMC CPU usage) my server will periodically run out of memory and crash. Now, it's important to note that the memory doesn't run out immediately - instead, over a period of an hour or 30 minutes, or two hours (or more), the RAM will become overloaded and begin swapping, at which point there is a user mode crash from numerous programs. The lesson to all of this, is to beware of overloading a system. Multi-server is always an option.
Forgot Root Password
MySQL and MariaDB in Debian and Devuan based distros can have the root password changed with dpkg-reconfigure mysql-server-#.# where #.# is your server version (type dpkg-reconfigure mysql and hit tab). This does not affect the ZM database.