how to fire a Mysql trigger on an specific date and time - mysql

I have to call a store procedure at the end of each month, i just need to know how to fire a trigger on an specific date and time to call my stored procedure.
Thanks for your time.

MySQL can't do this. Instead, you should use a cronjob. If you're on a Linux system, you can create a script or program that runs your stored procedure and schedule it for execution using the crontab command.
In the command line, writing crontab -e will allow you to edit your cronjobs. In here, you can add
0 5 L * ? * /path/to/script.pl
This will run /path/to/script.pl at 5 AM, the last day of every month.

MySQL triggers are responses to query events - the clock ticking is not one of them. If you need to execute something at a specific date/time, use an external job scheduler (e.g. cron) to trigger the job.
As stated in the MySQL docs:
MySQL triggers are activated by SQL statements only.

What you can do is to create an event.
CREATE EVENT event_name
ON SCHEDULE
period_of_time
DO
your_code_here

Related

Run a sql select query in a loop

I am monitoring a table in real-time, let's say the table name is user_stats, which keeps a record of users logging into my website and I am monitoring as to how many users have logged in, the query I am using is simple SELECT * FROM user_stats
Now, since the project is live and users are coming logging-in in real time, I am running the same script again and again. Is there a way such that I can tail the query and not run it manually, maybe like an infinite loop? I am using MySQL and running the query in ubuntu terminal.
If you want to run a Query,Stored Procedure in a Specific date and time without executing by a user you can do it by using:
Event
Mysql Event is a very powerful tool to execute a query or any, "Automatically" by setting up the execution date and time.

Mysql - Automatically Copy Data from one table to another on a specific date

I need to be able to copy data from one table to another on a specific date. I have a list of names that can be altered by a user. However, on a certain date, these need to be locked in and copied to a uneditable table and then mailed to key users.
How would I do this?
Cheers
mysql has trigger.
trigger is like functions which is called on a specific events and cannot be called explicitly. If the event specified occurs, database automatically calls that trigger and does what it supposed to do.
So to do your job, you can write a trigger which will work on your tables and set the date when you want to do your task. Then use if to check the system date is your desired date and if yes do your job.
I used trigger on oracle and never used on mysql but as triggers are using pl/sql, there should be no difference between triggers of oracle and mysql.
PHP has Mail sending options. You can send mail using php.
Let me know if this helps you.
First step would be to create a script in any language, let's say PHP, this script will just connect to the database, and execute the copy queries (grab and insert queries) whenever called.
Then, create a cronjob using Linux to execute the script on the needed schedule.
Sending Emails would be carried out by a similar technique.
Configure a cronjob: setup a cronjob
Copying data using PHP: copying data using php
MySQL implements an event scheduler which can execute mysql stored procedures either once after a specific interval, once at a specific time or repeatedly at a specific interval; that might be of some use to you.

Execute script when date matches mysql cell

I have a debian machine with a mysql server.
On mysql I have a table that contains a number of rows with a datetime field.
How can I execute a php script when the date and time of the machine match with those specified in any mysql record?
a) Mysql triggers ?
b) Deamon that runs in background and checks time every n seconds ?
c) Cron?
Let me know!
Polling the MySQL will work but surely is not efficient, especially when there are a lot of "triggers".
So use a cronjob. Note: MySQL-Queries cannot schedule or cancel cronjobs. So the (i guess) PHP-Script updating that date field also has to schedule the cronjobs. Also, every time the column changes you will have to cancel the previously scheduled cronjob and schedule a new one.
Since you can't run shell command from within Mysql database the MySql Triggers are no good. The MySQL Scheduler is therefore useless too. So you need an external script to help.
I'd suggest to create a (php) script and add it to the crontab to run every minute. Its task would be to check for matching dates in the database and run whatever command you need.
I think that the easiest solution is to use , if possible , the at command.
Basically after inserting the data into mysql do the following:
1)Create (using PHP) a BASH script that runs the php file
script_1.sh should look like this:
!#/bin/bash
php /path/to/file.php
and make it executable with :
exec("chmod +x script_1.sh");
2)create a second BASH script that executes the first at the desired time:
script_2.sh should look like this:
!#/bin/bash
at H:M Y:M:D < script_1.sh
Make script_2 executable and run it with the exec command.

Event Scheduler should execute every month

I have Win XP os and XAMPP installed in my machine.
I need to execute my event/scheduler at 12:00:00 AM of First day of every month.
Means 1st of every month. (e.g. jan 1st, Feb1 1st, March 1st, ... ).
And
I also need to call the stored procedure in the same event.
And I want achieve all this using Event/Job only not from front end.
Please spend few minutes for my query.
The MySQL event syntax is very simple -
DELIMITER $$
CREATE EVENT event1
ON SCHEDULE EVERY '1' MONTH
STARTS '2011-05-01 00:00:00'
DO
BEGIN
-- your code
END$$
DELIMITER ;
Event will start working on '2011-05-01' at '00:00:00' (datetime must be in a future).
More information - Using the Event Scheduler
Do not forget to enable global event scheduling thread -
SET GLOBAL event_scheduler = 1;
Assuming your mysql database lives on a web server, you can set up a cron job that runs at the first of every month. Your server probably provides some web interface to do this.
If you can't start the cron job from the server itself, there are free web services where you can set up cron jobs that call scripts on your server at given times.
The script called by the cron job can be a simple php script that runs the query.
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_query(' /* insert query here */ ');
mysql_close($link);
You may want to include some error checking and either send yourself an email about success / failure or leave some messages in a log file. That way you can monitor whether the script runs at all and / or successfully.

MySQL daily check and update

Is possible to create a routine or procedure that can automatic everyday check a date in a table.
To be simpler : I want to check if the date on a user have permitions to enter a site and when the date pass want to make the user field activated false.
routine daily
check if (todaydate < dateclient) then
client.activated= false
Thanks for all the help.
Yes, this should be a simple UPDATE query. Something like the following:
UPDATE Client
SET Activated = 'false'
WHERE NOW() < dateclient
You would obviously need to modify this for your schema and then schedule it to run daily using cron or an alternative scheduler of your choice.
Creating the routine is simple enough, but to run it on a daily basis you'll need to wrap it in a cronjob (*nix) or Scheduled Task (Windows). The routine, unfortunately, cannot execute itself - and to the best of my knowledge MySQL server does not possess the ability to run routines at scheduled intervals.