简体   繁体   中英

Delete login data from database when timestamp expires MySQL

I have this database:

login_id -> AI, INT, PK
login_token -> VARCHAR(255)
login_userId -> VARCHAR(255)
login_expires -> VARCHAR(255)

login_expires is a UNIX timestamp.

The login_token will be bound to a cookie. When the cookie expires, the row from the database with the login_token from the cookie should be deleted too.

What's the easiest why to do this?

With MySQL >= 5.1 you can use an event scheduler:

CREATE EVENT expired
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 5 MINUTES
DO
   DELETE FROM your_table
   WHERE login_expires < NOW();

Read more about this in MySQL reference manual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM