Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to watch a mysql database for changes using perl?

I'm looking for a solution similar to the inotify method of watching files for changes. I'm aware that I could watch the binlog file of the mysql database and run queries to pick out the new results but that seems very inefficient and inelegant; as does simply doing masses of queries in a loop waiting for new results.

like image 226
Drake Avatar asked Dec 11 '10 19:12

Drake


People also ask

How do I connect to a MySQL database in Perl?

When you connect to a MySQL database, you need the following information: First, you need to tell DBI where to find the MySQL database server. This is called the data source name or DSN. The data source name specifies the driver to use and the database to connect. Perl requires the data source name to begin with dbi: and the name of the driver.

What information do I need to connect to a MySQL database?

When you connect to a MySQL database, you need the following information: First, you need to tell DBI where to find the MySQL database server. This is called the data source name or DSN. The data source name specifies the driver to use and the database to connect.

How does DBI detect errors in Perl?

Perl DBI detects errors when they occur and calls either warn () or die () function with an appropriate error message. The PrintError attribute instructs DBI to call the warn () function that shows the errors to the output. The RaiseError attribute tells DBI to call the die () function upon error and to abort the script immediately.

How do I create a connection to a perlmysqdb database?

1 First, use the use DBI; statement at the top of the script. 2 Next, define some variables to hold the data source name, username, and password. 3 Then, define a hash that contains the connection’s attributes. 4 After that, pass the corresponding arguments to the connect () method to create a connection to the perlmysqdb database. More items...


2 Answers

If you add a TRIGGER to the table(s) you're interested in, you can use that to alert the watching application. You could do that in a number of ways:

  1. Create an audit table in the database, and have the trigger write the relevant info there; and have your watching application poll the audit table for new entries. You're still polling, but in a controlled way which won't hit the server too hard.
  2. Have the trigger call an external app through a UDF.
like image 160
Vince Bowdren Avatar answered Nov 15 '22 06:11

Vince Bowdren


As far as MyISAM tables go you can watch information_schema.TABLES.UPDATE_TIME. That would save you from polling all tables you're interested in. For InnoDB, watching binlog is the best I can think of.

like image 21
Mchl Avatar answered Nov 15 '22 07:11

Mchl