Get Rid of Cron Scripts Using Events Sheeri Cabral Senior DB Admin/Architect,

Slides:



Advertisements
Similar presentations
MySQL Access Privilege System
Advertisements

CMPT 354 Views and Indexes Spring 2012 Instructor: Hassan Khosravi.
Drop in replacement of MySQL. Agenda MySQL branch GPL licence Maria storage engine Virtual columns FederatedX storage engine PBXT storage engine XtraDB.
Week 6: Chapter 6 Agenda Automation of SQL Server tasks using: SQL Server Agent Scheduling Scripting Technologies.
Oracle9i Database Administrator: Implementation and Administration 1 Chapter 12 System and Object Privileges.
Fundamentals, Design, and Implementation, 9/e Chapter 11 Managing Databases with SQL Server 2000.
Manipulating MySQL Databases with PHP. PHP and mySQL2 Objectives Connect to MySQL from PHP Learn how to handle MySQL errors Execute SQL statements with.
System Administration Accounts privileges, users and roles
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
1 Chapter Overview Transferring and Transforming Data Introducing Microsoft Data Transformation Services (DTS) Transferring and Transforming Data with.
Database Security and Auditing: Protecting Data Integrity and Accessibility Chapter 3 Administration of Users.
Copyright 2007– WinWare, Inc. Session: How to Utilize the Open Database Architecture of CribMaster Presenter: Phil Stenger.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Agenda Journalling More Embedded SQL. Journalling.
Database Security and Auditing: Protecting Data Integrity and Accessibility Chapter 3 Administration of Users.
CHAPTER 21 Automating Jobs. Introduction to Automating Jobs DBAs rely heavily on automating jobs. DBAs cannot be effective without automation. Listed.
Week 6 Lecture 2 System and Object Privileges. Learning Objectives  Identify and manage system and object privileges  Grant and revoke privileges to.
By Lecturer / Aisha Dawood 1.  You can control the number of dispatcher processes in the instance. Unlike the number of shared servers, the number of.
ITN Wake Tech1 ITN270 Advanced Internet Databases Lecture 15. General MySQL Administration Topics: –Securing a New MySQL Installation –MySQL Server.
Introduction to MySQL Lab no. 10 Advance Database Management System.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Triggers A Quick Reference and Summary BIT 275. Triggers SQL code permits you to access only one table for an INSERT, UPDATE, or DELETE statement. The.
Alerting With MySQL and Nagios Sheeri Cabral Senior DB Admin/Architect
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Managing users and security Akhtar Ali. Aims Understand and manage profiles Understand and manage users Understand and manage privileges Understand and.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
MySQL More… 1. More on SQL In MySQL, the Information Schema is the “Catalog” in the SQL standard SQL has three components: Data definition Data manipulation.
Oracle 11g: SQL Chapter 4 Constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
CHAPTER 6 LESSON B Creating Custom Forms. Lesson B Objectives  Suppress default system messages  Create alerts and messages to provide system feedback.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Confidencial - TRACASA Automatize test [e- Reporting]
Best Practices in Loading Large Datasets Asanka Padmakumara (BSc,MCTS) SQL Server Sri Lanka User Group Meeting Oct 2013.
SQL Triggers, Functions & Stored Procedures Programming Operations.
 CONACT UC:  Magnific training   
Slide Set #24: Database security SY306 Web and Databases for Cyber Operations.
Exploreengage elevate explore engage elevate Presented By: Laura Murphy, Turnkey Technologies.
Navigating MySQL Stored Procedures & Functions and Triggers Presented by: Sheeri K. Cabral At ODTUG Kaleidoscope 2010.
SQL Database Management
3 A Guide to MySQL.
ASP.NET Programming with C# and SQL Server First Edition
Trigger used in PosgreSQL
Tables and Triggers.
Web Technologies IT230 Dr Mohamed Habib.
Open Source Server Side Scripting Permissions & Users
Importing and Exporting Data with MySQL
Unix System Administration
Stored Routines Tips, Tricks and Solutions Alex Nozdrin Software Developer, Server team George Trujillo Senior Instructor.
ISC440: Web Programming 2 Server-side Scripting PHP 3
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Tutorial 6 PHP & MySQL Li Xu
PRACTICE OVERVIEW PL/SQL Part - 1.
Chapter 11 Managing Databases with SQL Server 2000
Prof. Arfaoui. COM390 Chapter 9
Presentation transcript:

Get Rid of Cron Scripts Using Events Sheeri Cabral Senior DB Admin/Architect,

MySQL Events Scheduled SQL statements Recurring (like “cron”) – Scheduled times – At intervals One-time (like “at”) Platform-independent – Runs on all platforms the same, 5.1 and up

Why use MySQL events? Put db maintenance inside the db – Cache table Backed up with the db No need to store authentication information – More secure than a shell script

About the MySQL Event Scheduler Separate MySQL thread for event scheduler Each event also has its own thread Events can be done in parallel Server attempts to run events as close to the time scheduled as possible, but there is no guarantee

MySQL Event Process What it looks like in SHOW PROCESSLIST\G Id: User: event_scheduler Host: localhost db: NULL Command: Daemon Time: 2 State: Waiting for next activation Info: NULL

About MySQL Events Database objects Can be defined regardless of whether or not the event scheduler is on If the event scheduler is turned off, no new events will be run. – Currently executing events will finish normally.

Turning the event scheduler on SET GLOBAL event_scheduler=1; or SET GLOBAL event_scheduler='ON'; Need the SUPER privilege (as with setting any GLOBAL system variable) Can check the PROCESSLIST to see if it's running

Disabling the Event Scheduler Is not dynamic Change option file (/etc/my.cnf) or mysqld command line: mysqld –event_scheduler=DISABLED or [mysqld] event_scheduler=DISABLED

Disabling the Event Scheduler If the event scheduler is DISABLED, you cannot turn on the event scheduler: mysql> SET GLOBAL event_scheduler='ON'; ERROR 1290 (HY000): The MySQL server is running with the --event_scheduler=DISABLED or --skip-grant-tables option so it cannot execute this statement

Events and Disabled Event Scheduler The only change is that events will not run When the event scheduler is disabled, events can still be: – Created – Dropped – Modified

Minimal CREATE EVENT statement Requires the EVENT privilege CREATE EVENT event_name ON SCHEDULE schedule DO statement; The statement can be one SQL statement or a BEGIN...END block with many SQL statements BEGIN...END block requires the DELIMITER to be changed while creating the event.

Sample Event Simple test: insert the time into a table USE test; CREATE TABLE my_log ( my_time DATETIME NOT NULL ) ENGINE=InnoDB;

Sample Event CREATE EVENT event_scheduler_test ON SCHEDULE EVERY 5 SECOND DO INSERT INTO my_log (my_time) SELECT NOW();

CREATE EVENT / DROP EVENT If an event with the same name exists, an error is raised Can use CREATE EVENT IF NOT EXISTS event_name to raise a warning instead of an error To drop an event: – DROP EVENT event_name; – DROP EVENT IF EXISTS event_name ;

Starting / ending times By default, an event's period starts immediately By default, an event's period never ends Can set STARTS and/or ENDS clauses in CREATE EVENT statement as part of ON SCHEDULE Can set STARTS and ENDS relative or absolute: – STARTS ' :00:00' – ENDS NOW() + INTERVAL 1 YEAR

Sample Event with Start Time DROP EVENT IF EXISTS event_scheduler_test; CREATE EVENT event_scheduler_test ON SCHEDULE EVERY 1 SECOND ENDS NOW() + INTERVAL 10 second DO INSERT INTO my_log (my_time) SELECT NOW();

Specifying starting / ending times By default, an event's period starts immediately By default, an event's period never ends If you set the start time but not the end time: – Event's period starts at the start time – Event's period never ends If you set the end time but not the start time: – Event's period starts immediately – Event's period ends at the end time

Seeing Events SHOW EVENTS command – like SHOW TABLES – SHOW EVENTS FROM db_name – SHOW EVENTS LIKE 'text%' INFORMATION_SCHEMA.EVENTS table Stored in the mysql.event table – Do not change this table!

SHOW CREATE EVENT mysql> SHOW CREATE EVENT event_scheduler_test\G ******************* 1. row ******************* Event: event_scheduler_test sql_mode: time_zone: SYSTEM Create Event: CREATE EVENT `event_scheduler_test` ON SCHEDULE EVERY 1 SECOND ENDS NOW() + INTERVAL 10 SECOND ON COMPLETION NOT PRESERVE ENABLE DO INSERT INTO my_log (my_time) SELECT NOW(); character_set_client: latin1 collation_connection: latin1_swedish_ci Database Collation: latin1_swedish_ci 1 row in set (0.00 sec)

ON COMPLETION When the event's period is over (after ENDS), should you save the event or not? Default is to drop the event – ON COMPLETION NOT PRESERVE Change by using the ON COMPLETION clause: – ON COMPLETION PRESERVE

Sample One-time Event DROP EVENT IF EXISTS event_scheduler_test; CREATE EVENT event_scheduler_test ON SCHEDULE AT NOW()+INTERVAL 5 SECOND DO INSERT INTO my_log (my_time) SELECT NOW();

Preserving a one-time event DROP EVENT IF EXISTS event_scheduler_test; CREATE EVENT event_scheduler_test ON SCHEDULE AT NOW()+INTERVAL 5 SECOND ON COMPLETION PRESERVE DO INSERT INTO my_log (my_time) SELECT NOW();

SHOW EVENTS ******************* 1. row ******************* Db: test Name: event_scheduler_test Definer: Time Zone: SYSTEM Type: ONE TIME Execute at: ' :32:11' Interval value: NULL Interval field: NULL Starts: NULL Ends: NULL Status: ENABLED Originator: 0 character_set_client: latin1 collation_connection: latin1_swedish_ci Database Collation: latin1_swedish_ci 1 row in set (0.00 sec)

ALTER EVENT Like ALTER TABLE Be mindful of clauses Incorrect: ALTER EVENT event_scheduler_test ENDS NOW() + INTERVAL 1 MINUTE; Correct: ALTER EVENT event_scheduler_test ON SCHEDULE EVERY 1 SECOND ENDS NOW() + INTERVAL 1 MINUTE

How to rename an event ALTER EVENT event_name RENAME new_event_name; There is no RENAME EVENT command RENAME is the only extra clause ALTER EVENT has compared to CREATE EVENT

Full CREATE syntax CREATE [DEFINER = { user | CURRENT_USER }] EVENT [IF NOT EXISTS] event_name ON SCHEDULE schedule [ON COMPLETION [NOT] PRESERVE] [ENABLE | DISABLE | DISABLE ON SLAVE] [COMMENT 'comment'] DO sql_statement;

Full CREATE syntax schedule: AT timestamp [+ INTERVAL interval]... | EVERY interval [STARTS timestamp [+ INTERVAL interval]...] [ENDS timestamp [+ INTERVAL interval]...] interval: quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE | WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE | DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}

Event logging :19:54 [Note] Event Scheduler: [test.event_scheduler_test] started in thread :19:54 [Note] Event Scheduler: [test.event_scheduler_test] executed successfully in thread :19:55 [Note] Event Scheduler: [test.event_scheduler_test] started in thread :19:55 [Note] Event Scheduler: [test.event_scheduler_test] executed successfully in thread :19:56 [Note] Event Scheduler: Last execution of test.event_scheduler_test. Dropping :19:54 [Note] Event Scheduler: Dropping test.event_scheduler_test.

Event error logging CREATE EVENT bad_event ON SCHEDULE AT NOW()+INTERVAL 1 SECOND DO INSERT INTO table_not_exists SELECT 1; :19:54 [ERROR] Event Scheduler: [test.bad_event] Table 'test.table_not_exists' doesn't exist :19:54 [Note] Event Scheduler: Last execution of test.event_scheduler_test. Dropping :19:54 [Note] Event Scheduler: Dropping test.bad_event.

Event errors Like stored procedures and triggers, event errors are not always easy to debug Do not ignore [ERROR] Event Scheduler You can ignore [Note] Event Scheduler – Perhaps except for “Dropping”

Event runtime behavior sql_mode – No way to explicitly set it – SET – CREATE EVENT.... – If desired, set the sql_mode back – If the event already exists, drop and re-create it. Character set and collation are the same way – SET CHARACTER SET charset – SET COLLATION_CONNECTION=collation

Changing whom an event is invoked as DEFINER is the invoker for an event Default is CURRENT_USER() CREATE event_name – ALTER event_name – Only if you have the SUPER privilege Warning if does not exist at CREATE/ALTER time – No warning if the user is dropped later – Error when the event is run

Event limitations Almost all SQL can be used in DO clause, except: – ALTER VIEW – LOCK TABLES – UNLOCK TABLES – LOAD DATA – INSERT DELAYED Statement is allowed, but inserts are not actually delayed

Questions, comments, feedback? Sheeri K. Cabral – MySQL DBA – MySQL – send me questions, I may be able to help! Worked with MySQL since 2001 Full-time MySQL DBA as of 2005