linux 'at' command like functionality for mysql database - mysql

I need to update a particular field in the database based on some timestamps in the same table. Now are there time based triggers which can be scripted to look at the value and do things? much like the linux 'at' command.
I know one way is to execute a script using the at command. I am asking about something at the database level itself.

MySQL has an event scheduler.
See
http://dev.mysql.com/doc/refman/5.5/en/events.html

Related

How to update stored procedures in multiple databases in mysql

I am very new to mysql and I have I a situation where I need to update all my stored procedure in all my database. for example I have 10 database just say:
client_1,client_2,client_3,.....client_10.
Every database have same stored procedure just say:
proc_1,proc_2,proc_3,proc_4.
So if I made any changes to one of my stored procedure then it should get updated in all other database So that I don't have to do it manually.
I know the similar question have been asked but I am looking for some different approach. So what I want is some kind of mysql query or something like that in which we will pass the name of the database like:
client_1, client_3, client_8
and changes will only made to this databases.
I am using heidiSql- 10.2 with MySQL 5.6.
Thanks.
I am not entirely sure what you are wanting to do but I think you want something like this. First save the definition of your stored procedure to a file. Make sure it doesn't contain an schema references like client1.tableA. You want it to be able to run in any copy of your schema correctly. Be sure to follow the syntax rules defined by MySQL
Defining Stored Programs
Then once the stored procedure is saved you can use the mysql command line to run it for each client you want to update.
You would first connect to the database server using the mysql command line. Then issue a USE command to activate the first client database. Then run the script using the SOURCE command. See MySQL Batch Commands Then repeat for each client.
USE client1;
source c:\temp\storedProcedure.sql
USE client2;
source c:\temp\storedProcedure.sql
If this is not exactly what you needed hopefully it gives you some ideas to get you what you need.
Note that you could do the connection to the database and execute these commands via batch file instead of manually if you wanted to.
There are no statements in MySQL that create/drop/alter multiple procedures at once. You can only change one procedure at a time.
You can write an SQL script that includes a series of statements. But it's up to you to write that script.
You may write some script in Python (or other favorite language) that outputs the SQL script.
I don't know HeidiSQL, but I doubt it has any facility to apply the same change to many procedures. Nor does any other MySQL client that I'm aware of.

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.

How to update a table when its specified date is today in mysql?

I have a table in my database that has a closing date column that was an input from a user. Now I want to update a column status when the closing date is today..
Are there any suggestions on how can I do this?
After searching for answers I found out there you can CREATE AN EVENT in MySQL.
this link should help http://www.sitepoint.com/how-to-create-mysql-events/.
If your have running your database on an server that is allways running, try to use crontab(linux) or something equal.
Crontab is a deamon that runs some commands for you at specified date. I myself use crontab in combination with php scripts to reset some stuff in by mysql database.
Where is your database running ? - Maybee this would helf to answer.
Ask if you wanna know mor about this .
Gordon

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.

Automation on SQL (MySQL)

I would like to ask a question for connoisseurs of SQL (MySQL, to be specific).
I have a table reservation schedules. And when a customer makes a reservation there is a time to let the client to use my service. Therefore, the reservation that he did have to leave the table reservations.
Once the time limit of use is reached, there is some method (trigger,
I believe), which automatically erase the record of this book on the
table?
If so, can someone give me some idea of ​​how to start my search for it, or it is also totally welcome some help as some more advanced lines of code.
There is also the possibility that this only be possible to be implemented via Server-Side (PHP, ASP ...), which does not believe is so true because SQL is a language very complete (to my knowledge).
Edit1: The problem is that I believe this is a task of the DBMS, so I wanted to leave this responsibility to the MySQL The problem is: how?
A trigger is triggered by either before or after an insert , update or delete event (at least in MySQL according to the docs)
What you want is some sort of scheduled job either through your application be it php, asp.net, etc.. or cron job that calls some sort of SQL script.
So to answer, it can't be done purely with triggers.
You can use SQL jobs, but if the removal logic is to complex to manage it with queries I suggest you to use a PHP script that does all that work for you.
Just write down the data check/remove logic in PHP and set up a simple cron operation for it.
The advantage of this solution is that you can access to your scripts/classes/db providers and save your time and your can log all the operations separately (instead of logging to MySQL logs, no matter what script language you are relying on).
If you have a full control of your server the scheduled operation will look like this (if you want to check your DB entries every day at 00:01):
cat /etc/cron.d/php5
0 1 * * * php /path/to/your/script.php >> /path/to/your_script.log
..otherwise you will have to check the control panel of your hosting account and figure out how to manage
You can create one more column in your table where you will create the expiration date. Then you can on your sql server create the job that will erase all records that have expiration date less than curent date.
CREATE EVENT db_name
ON SCHEDULE EVERY 10 SECOND
DO
DELETE FROM myschema.mytable WHERE expiration_date < NOW()
I hope that will help.