How to periodically update status in MySQL [closed] - mysql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
The community reviewed whether to reopen this question 8 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm creating a todo app. I have a status column that receives 1, 2 or 3 (pending, overdue, completed).
Whenever I create a task it is set to pending. The user can mark it as complete. But is there a way to automatically update it to overdue in case it's not completed and due_date is less than today?

You can use MySQL event Scheduler.
Prerequisite:
You have to have event_scheduler ON in your mysql server.
Check whether event scheduler is ON or OFF
SELECT ##event_scheduler;
To turn event_scheduler ON run the following query:
SET GLOBAL event_scheduler = ON;
Note: If you restart MYSQL Server then event scheduler status will be reset unless the following is written in the configuration file.
For Windows: in my.ini file write this under [mysqld] section
[mysqld]
event_scheduler=on
For Linux: in my.cnf file
[mysqld]
event_scheduler=on
Event:
CREATE
EVENT `updateStatusEvent`
ON SCHEDULE EVERY 1 DAY STARTS '2016-08-11 00:00:00'
ON COMPLETION NOT PRESERVE
ENABLE
DO
UPDATE your_table SET status_column = 2 WHERE your_time_column < CURDATE();
The event will be started for the first time at '2016-08-11 00:00:00'
and after that the event will be scheduled in every 1 day interval and will update the status of the corresponding data.

If your version of MySQL supports it (version >= 5.1.6 if I'm not mistaken) you can use Event Scheduler.
CREATE EVENT check_overdue ON SCHEDULE EVERY 2 HOUR DO
UPDATE mytable SET status = 2 WHERE due_date < NOW();
Another option is to set up a Cron job that calls a PHP or another online script.
Anyway you have to query periodically for any overdue events and mark them as overdue.

Related

how to implement implicit row level locking in innodb? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm using MySql(innodb storage engine). I want to implement row level implicit locking on update statement. so, that no other transaction can read or update that row concurrently.
Example:
Transaction1 is executing
"UPDATE Customers
SET City='Hamburg'
WHERE CustomerID=1;"
Then, at same time Transaction2 should not able to read or update same row but Transaction2 should be able to access other rows.
Any help would be appreciated.
Thank you for your time.
If there are no other statements supporting that UPDATE, it is atomic.
If, for example, you needed to look at the row before deciding to change it, then it is a little more complex:
BEGIN;
SELECT ... FOR UPDATE;
decide what you need to do
UPDATE ...;
COMMIT;
No other connection can change with the row(s) SELECTed before the COMMIT.
Other connections can see the rows involved, but they may be seeing the values before the BEGIN started. Reads usually don't matter. What usually matters is that everything between BEGIN and COMMIT is "consistent", regardless of what is happening in other connections.
Your connection might be delayed, waiting for another connection to let go of something (such as the SELECT...FOR UPDATE). Some other connection might be delayed. Or there could be a "deadlock" -- when InnoDB decides that waiting will not work.

What is heartbeat replication monitoring? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the meaning of "master heartbeat time period" in MySQL server, and how can I configure this variable in my.cnf?
As mentioned here on the mysql performance blog
MASTER_HEATBEAT_PERIOD is a value in seconds in the range between 0 to 4294967 with resolution in milliseconds. After the loss of a beat the SLAVE IO Thread will disconnect and try to connect again.
You can configure it on a slave using syntax also mentioned in that article and in the queries below.
mysql_slave > STOP SLAVE;
mysql_slave > CHANGE MASTER TO MASTER_HEARTBEAT_PERIOD=1;
mysql_slave > START SLAVE;
More information on using CHANGE MASTER can be found on the mysql documentation site
MASTER_HEARTBEAT_PERIOD sets the interval in seconds between replication heartbeats. Whenever the master's binary log is updated with an event, the waiting period for the next heartbeat is reset. interval is a decimal value having the range 0 to 4294967 seconds and a resolution in milliseconds; the smallest nonzero value is 0.001. Heartbeats are sent by the master only if there are no unsent events in the binary log file for a period longer than interval.
Setting interval to 0 disables heartbeats altogether. The default value for interval is equal to the value of slave_net_timeout divided by 2.

MySQL event not triggering [duplicate]

This question already has answers here:
why this mysql event can't get run?
(2 answers)
Closed 9 years ago.
I've created the following event, but for some reason it's not getting triggered:
CREATE EVENT Del_logs
ON SCHEDULE EVERY 1 HOUR
DO
TRUNCATE TABLE security.errors;
Is there any log I could check to see what went wrong?
It looks like the event scheduler is OFF.
use SHOW PROCESSLIST to check if the event scheduler is enabled. If it's ON you should see a process "Daemon" by user "event_scheduler".
use
SET GLOBAL event_scheduler = ON;
to enable the scheduler if it's currently not enabled.
More on configuring event scheduler read here

deleting item from database after 30 minutes [duplicate]

This question already has an answer here:
Delete MySQL Row after 30 minutes using Cron Jobs/Event Scheduler
(1 answer)
Closed 9 years ago.
I want to have a script, that counts how many users are online on my site, but this script should count guests, so I have created a database for session, this script gives user an ID and set 30 minutes session, and now I have a problem, because if he is not active more than 30 minutes, he should be deleted from the database, because I want to count by ID how many users are online, and I have headache how can I do this.
Is there a simple way to do this?
As stated by Barmar in their answer here:
DELETE FROM my_table
WHERE timestamp < NOW() - INTERVAL 30 MINUTE
Write a PHP script that executes this SQL, and add a crontab entry
that runs it every 30 minutes. Or use the MySQL Event Scheduler to run
it periodically; it is described here.
Since you have not mentioned any database in specific, I give you a general idea of how to implement it:
You can have a table like this:
USER_SESSIONS
(
USER_ID //UNIQUE_ID
LAST_ACTIVE //TIMESTAMP
)
Here is the functionality you can associate to implement the session:
When a user logs in, you create an entry in this table.
When a user logs out, you delete the corresponding entry from this table.
When user does some activity (depends on how you want to track activity in the front end), update the corresponding TIMESTAMP of the user.
Create a DB Schedule (a continuously running process) that monitors the TIMESTAMP column. All good databases have a built in scheduler. Here is some pseudo code for the scheduler:
FOR each entry in USER_SESSIONS
If (CURRENT_TIMESTAMP - LAST_ACTIVE) > [30 mins] then
delete the entry of the corresponding user from this table.
//This will essentially cause a session timeout.
End If
LOOP;
I hope you got a fair idea of how to implement it.

How does mysql replication work? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
From what I've been able to find in the web, mysql stores statements that alter data in the bin log, which is then read by the slave. What remains unclear is what happens to those statements next? Are they replayed as if they happenned on the slave server?
For example, say there is a query with current time in the conditional, like "UPDATE something SET updatedat = NOW()", and due to the replication delay, the query ends at the slave a couple of seconds later. Will the values in the table be different?
Or if there is master-master replication, at time 1000 the following query happens on server 1:
UPDATE t SET data = 'old', updatedat = 1000 WHERE updatedat < 1000
At time 1001 on server 2 the following query happens:
UPDATE t SET data = 'new', updatedat = 1001 WHERE updatedat < 1001
Then server 2 fetches the replication log from server 1, the value on the server 2 will be "old"? If so, is there a way to avoid it?
For example, say there is a query with current time in the conditional, like "UPDATE something SET updatedat = NOW()", and due to the replication delay, the query ends at the slave a couple of seconds later. Will the values in the table be different?
No. The replication duplicates the row, which means that the time will be the same