Look at these locking functions. Essentially you can obtain a named lock by using GET_LOCK()
function like so:
SELECT GET_LOCK('myNamedLock');
// Do your thing in the app, then
SELECT RELEASE_LOCK('myNamedLock');
Your current session will own the lock and no other session will be able to obtain a lock with the same name.
It is a sort of pseudo locking, nothing is actually locked but if you want to make sure another connection is not doing something on the DB, make that other connection obtain the named lock first and then for your current connection keep checking if lock has been released/is available.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…