How to check event scheduler status mysql - mysql

In MySQL, we can enable the event scheduler by following query:
SET GLOBAL event_scheduler = ON;
Similarly, to turn off the scheduler:
SET GLOBAL event_scheduler = OFF;
But, Is there any query/way to check the status of this event_scheduler whether it's on or off?

Use SHOW VARIABLES
SHOW VARIABLES
WHERE VARIABLE_NAME = 'event_scheduler'

Use below command to see event status , you can choose any of them.
SELECT ##global.event_scheduler
or
SHOW variables WHERE variable_name ='event_scheduler'
To enable event temporarily ON or OFF
SET GLOBAL event_scheduler = OFF;
SET GLOBAL event_scheduler = ON;
For permanent setup go to my.cnf or my.ini or inside /etc/my.cnf.d/server.cnf file and under [mysqld] set event_scheduler =ON or event_scheduler=OFF depending on your requirements.

This should also work:
select ##global.event_scheduler = 'ON'
That is a little easier to use in a stored procedure, where you might want to know if it is ON before turning it on. Note that I tested this on MySQL 5.7 after turning on Event_Scheduler either with ON or 1. In both cases, querying the variable returns 'ON'.
Also, note the quotes are used for querying, but not for setting the variable. A little mysql weirdness for you.

Related

How to enable --general-log without restarting the MYSQL server?

According to the mysql documentation this flag is possible to change dynamically.
Property Value
Command-Line Format --general-log
System Variable general_log
Scope Global
Dynamic Yes
SET_VAR Hint Applies No
Type Boolean
Default Value OFF
But by default this option is disabled. But I need to enable this flag in order to see the logs without restarting the server. What is the way to enable this without restarting the server.
MySQL provides a System variable general_log, which specifies whether the general query log is enabled or not. You will just need to execute the following queries to enable GLOBAL logging (for all the other client sessions as well):
SET GLOBAL general_log = 'ON';
You can also specify the log file path:
SET GLOBAL general_log_file = '/var/log/mysql/all.log';
Remember that when you restart the server, these settings will be lost. To make the changes persistent, you will have to make changes in the configuration file.
If you want to disable the general query logging, you can do the following:
SET GLOBAL general_log = 'OFF'

How do I see the last command issued in mysql?

I issued a query and got results at the mysql> command line.
Is there any way (other than edit) that I can see what the last query was?
I vaguely remember that in Oracle pl/sql you could just type list but that command isn't available (syntax error) in mysql
Additionally, for those blessed with MySQL >= 5.1.12:
Execute SET GLOBAL log_output = 'TABLE';
Execute SET GLOBAL general_log = 'ON';
Take a look at the table mysql.general_log
If you prefer to output to a file:
SET GLOBAL log_output = "FILE"; which is set by default.
SET GLOBAL general_log_file = "/path/to/your/logfile.log";
SET GLOBAL general_log = 'ON';
I prefer this method because:
you're not editing the my.cnf file and potentially permanently turning on logging
you're not fishing around the filesystem looking for the query log - or even worse, distracted by the need for the perfect destination. /var/log /var/data/log /opt /home/mysql_savior/var
restarting the server leaves you where you started (log is off)
For more information, see
http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_general_log

Mysql Event Not Working

I have added the following simple test event on my mysql database via phpmyadmin:
CREATE DEFINER=`root`#`localhost` EVENT `my_event`
ON SCHEDULE EVERY 1 MINUTE STARTS '2013-05-27 00:00:00'
ON COMPLETION NOT PRESERVE ENABLE DO
BEGIN
UPDATE `test` SET `name`="z";
END
My environment is mac + MAMP Pro. I am expecting to change all rows on my 'test' table with name 'z' within a minute. But not happening so.
Do I have to something additional to get my events start working?
Output of "SHOW PROCESSLIST":
Thanks.
Events are run by the scheduler, which is not started by default.
Using SHOW PROCESSLIST is possible to check whether it is started. If not, run the command
SET GLOBAL event_scheduler = ON;
to run it.
Verify if the event_scheduler is On - execute the following command:
SHOW PROCESSLIST;
It'll output a table/entries, you must look for an entry with User event_scheduler, and Command Daemon:
Id User Host db Command Time State Info
22870 event_scheduler localhost \N Daemon 23 Waiting for next activation \N
OR, you can also verify using the following command:
SELECT ##global.event_scheduler;
The result should be ON, otherwise set it off (will get 0 for the command), as stated in the next section.
If you don't have any such entry (as above), you may start the event scheduler using the following command:
SET GLOBAL event_scheduler = ON;
Once done, you can verify if it has been executed properly using the SHOW PROCESSLIST command, as mentioned above.
For those wondering how to enable it by default at startup, add the following to your config file (my.ini, my.cnf):
#Event scheduler can be set to 1 (On), 0 (Off), or Disabled
event_scheduler=1
Restart of the service is required in this case, so if you want minimal disruption, add this to the config file, and then run the SQL:
SET GLOBAL event_scheduler = ON;
That way, it will run for the current process, and if the server is restarted it will still work.
Note that this doesn't work if the event_scheduler was set to disabled. In that case the only option is to restart the service.
If you want your event_scheduler to startup automatically every time mysql server restarts, anywhere under the [mysqld] section of the my.ini or my.cnf file that you find in /etc/mysql you should place
[mysqld]
# turning on event_scheduler
event_scheduler=ON
restart mysql to check if it is running (in command line terminal!)
sudo service mysql restart
then check your processlist
SHOW PROCESSLIST
you can check if your events are running by checking the last time they ran
SELECT * FROM INFORMATION_SCHEMA.events
Temporal
SET GLOBAL event_scheduler = ON;
Will not work if event_scheduler is explicitly DISABLED, see the method below
Permanent (needs restart)
In your config file (In Ubuntu it's /etc/mysql/mysql.cnf):
[mysqld]
event_scheduler = ON
Notes:
The event_scheduler variable can have this possible states:
OFF (or 0) (default)
ON (or 1)
DISABLED: you cannot use the temporal enabling, you can only change state through the config file and restarting the server
WARNING: Keywords ON / OFF are preferred over their numerical equivalents.
And in fact Mysql Workbench doesn't recognize the configuration event_scheduler=1, it shows as OFF in the Options File section.
Tested in Ubuntu with Mysql Workbench 8.0.17 and Mysql Server 5.7.27
Although ON and OFF have numeric equivalents, the value
displayed for event_scheduler by SELECT or SHOW VARIABLES is always
one of OFF, ON, or DISABLED. DISABLED has no numeric
equivalent. For this reason, ON and OFF are usually preferred
over 1 and 0 when setting this variable.
Source: https://dev.mysql.com/doc/refman/5.7/en/events-configuration.html
I just figured out that on MariaDB, after adding an event (in my case, it was the first one), you have to restart the event-scheduler
SET GLOBAL event_scheduler = OFF;
and then
SET GLOBAL event_scheduler = ON;
to make it actually bring the scheduler into "waiting for activation"-state.
I would just like to add to this thread. I dumped my database to another server and as a result the definer of my event had no such grant defined for the user. I updated my definer with
ALTER DEFINER='root'#'localhost' EVENT event.name COMMENT '';
Make sure your definer has the correct PRIVILEGES.
Remember to add in 'Commit', after 'DO BEGIN' or 'DO'. Works for me after that.
Try
SET GLOBAL event_scheduler = ON;
DELIMITER $$
CREATE DEFINER=`root`#`db01` EVENT `PRICEALERT_STATUS`
ON SCHEDULE EVERY 1 DAY STARTS TIMESTAMP(CURRENT_DATE)
DO BEGIN
// Your Query
END $$
DELIMITER ;

change Event Scheduler status in mysql

I have 5 event Scheduler in my MySql data base.
I want to stop one event Scheduler from them.
I have used below statements,
SET GLOBAL event_scheduler = OFF;
SET ##global.event_scheduler = OFF;
SET GLOBAL event_scheduler = 0;
SET ##global.event_scheduler = 0;
but it stops all 5 event Scheduler. Please help me for this.
Thanks in Advance.
Regards,
Saurabh
I think you mean that you have 5 events on one scheduler. Right?
Here is how you disable one event:
ALTER EVENT myevent
DISABLE;
Here is how you disable the entire scheduler (all events):
SET GLOBAL event_scheduler = OFF;
All this can be read at http://dev.mysql.com/doc/refman/5.1/en/events.html
How to disable one event is here http://dev.mysql.com/doc/refman/5.1/en/alter-event.html
If you need to get the current global event status, this may be useful for you;
select ##global.event_scheduler;
One thing to consider :
It is possible to set the Event Scheduler to DISABLED only at server startup. If event_scheduler is ON or OFF, you cannot set it to DISABLED at runtime. Also, if the Event Scheduler is set to DISABLED at startup, you cannot change the value of event_scheduler at runtime.
In such case to disable the event scheduler, use one of the following two methods:
As a command-line option when starting the server:
--event-scheduler=DISABLED
In the server configuration file (my.cnf, or my.ini on Windows
systems), include the line where it will be read by the server (for
example, in a [mysqld] section):
event_scheduler=DISABLED
https://dev.mysql.com/doc/refman/5.7/en/events-configuration.html

Change Permanently Session System Variable in MySQL

I added this line to my.ini
wait_timeout=2000000
When I type
show global variables
It prints wait_timeout=2000000,
but when I type
show variables
It prints wait_timeout=28800
I can set with
set wait_timeout=2000000
But I do not want to set it all the time manually.
Do you have any suggestion to set permanently session system variable?
You probably need to check the interactive_timeout is set also - regular client connections are probably picking up your new setting, but when you check it manually using an interactive client, MySQL will set the timeout from this setting:
On thread startup, the session
wait_timeout value is initialized from
the global wait_timeout value or from
the global interactive_timeout value,
depending on the type of client (as
defined by the CLIENT_INTERACTIVE
connect option to
mysql_real_connect()). See also
interactive_timeout.
See manual for details.