Difference between revisions of "MySQL"

From ZoneMinder Wiki
Jump to navigationJump to search
Line 151: Line 151:
UPDATE Config set Value=0 where Name='ZM_OPT_USE_AUTH';
UPDATE Config set Value=0 where Name='ZM_OPT_USE_AUTH';
</pre>
</pre>
===MySQL segfault on service start===


==See Also==
==See Also==

Revision as of 14:04, 7 June 2020

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

In older installs, you needed to setup the db, and user after installing ZM. In more recent installs (1.34), this is handled by the dpkg scripts, but it will be included here for reference:

mysql -u root -p < /usr/share/zoneminder/db/zm_create.sql
mysql -u root -p -e "grant all on zm.* to 'zmuser'@localhost identified by 'zmpass';"
mysqladmin -u root -p reload

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.

Backup:

mysqldump -u root -p zm > zmdb.sql

Restore:

mysql -u root -p zm < zmdb.sql

If upgrading, make sure to zmupdate.pl

Example Queries

sudo mysql -u root -p
mysql> use zm;
> show tables;
> select * from Users;
> update Users set MaxBandwidth = 'Low' where Username = 'user1';

A full list of db columns can be found in zm source under db folder.

Change Storage Area for Multiple Cameras

Say you have 50 cameras to set to a HDD. It would be painful to manually update each one to a new storage area...

Storage Area is the key StorageID in Monitors (1.32.3), therefore

use tmux to view mysql

select * from Monitors\G

use ctrl-b pageup pagedn to navigate and review fields.

press q to exit this view.

update Monitors set StorageID = 2 where StorageID = 0;

Set all Cameras to Use H264 Encode

Per previous example: (1.32.3 tested, review your tables for changes)

update Monitors set VideoWriter = 1 where VideoWriter = 0;

You may also want to disable JPEG encoding.

update Monitors set SaveJPEGs = 0 where SaveJPEGs = 3;

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:

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.

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 for MySQL

MySQL and MariaDB in Debian 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.

Option 2 is to login to mysqld_safe --skip-grant-tables and change password for root (many guides online follow this approach).

Forgot Admin Password for Zoneminder

There are different ways to resolve this. Best advice, is probably option 3.

  • Option 1: black out password

If this happens, blank out the password for admin, and you should be able to login with a blank password. Alternatively you can add a new password to the ZM DB. Note that mysql passwords for ZM must be encrypted. You can't just query add a new plaintext password. The following should work with mariaDB, and mysql < 8.0.11 (untested).

MariaDB [zm]> update Users set Password=PASSWORD(NewPassword) where Username="David";
  • Option 2: delete and restore DB

Alternatively, you can delete and restore the DB.

Reference: Mysql Reference Docs 8 : Password Function Deprecated

  • Option 3: Turn off Auth, fix, turn auth on

Another approach (forums):

Best bet is to turn off auth, use UI to change admin password, turn auth back on.

So to turn if off use mysql

mysql -u zmuser -p zm
UPDATE Config set Value=0 where Name='ZM_OPT_USE_AUTH';

See Also