Event Scheduler should execute every month - mysql

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.

Related

How to remove the data from MySQL DB from node when the scheduled time limit expire

I would need to remove some data from the MySQL DB when the expire time has arrived.
Like, expire the cookie value on the server-side when user session time has arrived. It's like an automatic action. It's needs to do even the customer was not online. When he was back to the online he needs to login again. And it needs to do some works like reminder action. User can fix some time to bring some reminder notification. On that time server needs to fetch some data from the MySQL DB and sent it to the customer through the push notification automatically.
You need to do the following:
Add a column in your table to store the expiry date/time using default systime + expiry. You want the time to be generated by the DB and not your app code, so it will be exact.
Index this column with BTree, not Hash. You need this index.
Write a stored procedure to delete rows < current system time.
Create a mysql scheduled event to run every 1 minute to run your stored procedure.

How can I run a query in a specific time using MySQL or PHP?

is it possible to run a query in a specific time like for example every seconds/minutes. By the way I am building a simple auction system and the query I am talking about is to check if there are products that are already expired. Means their datetime_end is less than the current date and time.
So I have a query like this:
SELECT id, datetime_end FROM auction_product WHERE datetime_end < NOW() AND `status` = 0;
And after identifying if there are products that are already expired I will create a query that will change their status to 1 means the status is cancelled/closed.
Is there a function in MySQL that can run a certain query? Like automatic query. Or should I make an Ajax that will run every seconds and check if there is an expired product? Can you give me some idea about this?
Because in my current setup I just put the AJAX in product page and homepage. Means if the user is in the other page my AJAX will not run. So I think I will put my created AJAX somewhere in the header or footer or in a global JS. If I did that does it affect the performance of my page load? Because it will run every seconds.
That's all thanks. I hope you can give me some idea about this. :)
You could do this in MySQL using scheduled events e.g.:
CREATE EVENT IF NOT EXISTS expire_products
ON SCHEDULE
EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
DO
<insert your update statement here>;
You need to enable the event scheduler first either via the command line argument:
--event-scheduler=ON
or the my.cnf/my.ini option:
event_scheduler=ON
or by running the following in MySQL:
SET GLOBAL event_scheduler = ON;
See https://dev.mysql.com/doc/refman/5.7/en/create-event.html for more details.
You don't need to check it every second on the client side. In case someone visits the page of the product, you'll run an ajax there to check if there are enough products left or not. If there are no products left you can update the database on the page itself.
Now you also want to make sure it is regularly updated, so you can run a script on the server side on a Cron Job. But, you also need to make sure you don't run some heavy resource intensive scripts on it. You can run a cron job about every hour or two hours to regularly update it from the server side. And in case any of the users views a product, you will update it automatically with the ajax, so the next time a user visits, in between two cron jobs, they will see the page being already updated because of the earlier user. This will keep the pressure out of your server by distributing the work.
So the idea is somewhat like this:
1)user enters-> visits page-> runs ajax to check if products are left -> update db if products are over
2)cron job checks if products are left every two hours-> updates db if products are over

autocron.php in OSTicket

Linux Cron Job or MySQL Events doesn't work with my server, So I'm planning to use the Autocron.php in OSTicket.
My requirement is simple I need to call a Stored Procedure, 1st of every month. Everything else the procedure will take care.
Any ways on how to do it without using Linux Cron Job & Mysql Events ?

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

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

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.