I have a mysql event that is scheduled to run nightly defined as follows:
create definer = myuser@`%` event nightly on schedule
every '1' DAY
starts '2023-10-21 00:00:00'
enable
do
call mydb.nightly_script;
Although when I look at the tables that would be modified by the proc, the updates aren’t happening. If I copy the line call mydb.nightly_script;
from the event and execute it, I see the proc executes correctly however nothing happens as a result of the event.
I have gone through basic troubleshooting steps. I have verified the event scheduler is ON
SELECT @@event_scheduler
returns
event_scheduler |
---|
ON |
And, when I look at the EVENTS table in information_schema, it shows that the event has run every night.
SELECT *
FROM information_schema.EVENTS
WHERE EVENT_NAME = 'nightly';
EVENT_CATALOG | EVENT_SCHEMA | EVENT_NAME | DEFINER | TIME_ZONE | EVENT_BODY | EVENT_DEFINITION | EVENT_TYPE | EXECUTE_AT | INTERVAL_VALUE | INTERVAL_FIELD | SQL_MODE | STARTS | ENDS | STATUS | ON_COMPLETION | CREATED | LAST_ALTERED | LAST_EXECUTED | EVENT_COMMENT | ORIGINATOR | CHARACTER_SET_CLIENT | COLLATION_CONNECTION | DATABASE_COLLATION |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
def | mydb | nightly | myuser@% | SYSTEM | SQL | call mydb.nightly_script | RECURRING | null | 1 | DAY | STRICT_TRANS_TABLES | 2021-10-21 18:08:00 | null | ENABLED | NOT PRESERVE | 2023-10-05 18:07:45 | 2023-11-05 00:00:45 | 2023-11-06 00:00:02 | 2866802262 | utf8mb4 | utf8mb4_general_ci | latin1_swedish_ci |
Although everything indicates the scheduler is running and the event is firing, the stored procedure either isn’t running or is failing somehow and I’m not sure how to investigate further (it is an Azure DBaaS and I’m not sure how to locate the relevant logs, if they exist at all).
I have attempted a few variations of the call
command in the body of the event e.g. verified case of the proc name, fully qualifying the proc using dot notation, including or excluding the parens at the end of the proc (it doesn’t take any parameters), etc.
The definer of both the proc and the event are my user e.g.
CREATE
definer = myuser@`%` procedure nightly()
BEGIN
...
And my user has the following GRANTS:
SHOW GRANTS FOR myuser
Grants for myuser@% |
---|
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, PROCESS, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER ON . TO ‘myuser’@’%’ WITH GRANT OPTION |
The body of the stored procedure itself looks something like this:
CREATE
definer = myuser@`%` procedure AutoTagAll()
BEGIN
/*Clean up any tables that shouldn't be there*/
drop table if exists jobuserlist;
drop table if exists pl;
drop table if exists listIds;
drop table if exists OldList;
/*Create staging tables */
CREATE TEMPORARY TABLE OldList(
listId CHAR(36),
contactId CHAR(36),
PRIMARY KEY(contactId, listID))
;
CREATE TEMPORARY TABLE jobUserList
(
user_id CHAR(36) PRIMARY KEY,
company_id CHAR(36)
);
CREATE INDEX IXN__jobUserList__company_id on jobUserList (company_id);
CREATE TEMPORARY TABLE pl
(
ListId CHAR(36),
contactId CHAR(36),
primary key (listId, contactId)
);
CREATE TEMPORARY TABLE listIds(
listID CHAR(36) PRIMARY KEY,
listName VARCHAR(200)
);
insert into jobUserList ( user_id, company_id)
...
/*clean up*/
drop table if exists jobuserlist;
drop table if exists pl;
drop table if exists listIds;
drop table if exists OldList;
END;
Given the event is not functioning correctly, I currently have a bash script running on another machine that is connecting and making the call:
...
#Prepare sql query
SQL_Query='call mydb.nightly'
#mysql command to connect to database
mysql -h$MASTER_DB_HOST -u$MASTER_DB_USER -p$MASTER_DB_PASSWD --ssl_mode=REQUIRED -D$MASTER_DB_NAME <<EOF
$SQL_Query
EOF
echo "script complete."
However, I am unhappy with the security and reliability of this workaround and would prefer to integrate this job into the RDBMS scheduler.
At this point, I’m not sure how to use events for my desired purpose. Is there something I’m missing with events? Is this the wrong approach? Is there something verboten in the stored proc itself (creating temporary tables, performing inserts, updates, and deletes within mydb)?
Thank you, SO community, in advance!
2
Answers
I am not sure if this will work with your Azure DB but you could try querying the performance_schema:
Failing that, you could add a simple log table and add some logging statements to your proc:
I checked your scenario with sample store procedure, and it is working correctly. there are two possible scenarios for which you are facing issue.
here I tried below code with sample store procedure for every minute:
you can see above procedure name is same while creating and using it in event.
To Verify if the
event_scheduler
is On – execute the following command:It’ll output a table/entries, you must look for an entry with User
event_scheduler
, and CommandDaemon
:Inserting row per minute for me:
As you are saying when you are calling it outside event it is working properly so check the name of SP you are using in event.