How can I enable MySQL's slow query log without restarting MySQL? - mysql

I followed the instructions here: http://crazytoon.com/2007/07/23/mysql-changing-runtime-variables-with-out-restarting-mysql-server/ but that seems to only set the threshold.
Do I need to do anything else like set the filepath?
According to MySQL's docs
If no file_name value is given for --log-slow-queries, the default name is
host_name-slow.log. The server creates the file in the data directory unless
an absolute path name is given to specify a different directory.
Running
SHOW VARIABLES
doesn't indicate any log file path and I don't see any slow query log file on my server...
EDIT
Looks like I'm using server version 5.0.77, so I needed to do:
SET GLOBAL log_slow_queries = 1;
but I get: ERROR 1238 (HY000): Variable 'log_slow_queries' is a read only variable
I assume I'm going to need to restart the server and have log_slow_queries set in my config?

Try SET GLOBAL slow_query_log = 'ON'; and perhaps FLUSH LOGS;
This assumes you are using MySQL 5.1 or later. If you are using an earlier version, you'll need to restart the server. This is documented in the MySQL Manual. You can configure the log either in the config file or on the command line.

For slow queries on version < 5.1, the following configuration worked for me:
log_slow_queries=/var/log/mysql/slow-query.log
long_query_time=20
log_queries_not_using_indexes=YES
Also note to place it under [mysqld] part of the config file and restart mysqld.

Find log enabled or not?
SHOW VARIABLES LIKE '%log%';
Set the logs:-
SET GLOBAL general_log = 'ON';
SET GLOBAL slow_query_log = 'ON';

MySQL Manual - slow-query-log-file
This claims that you can run the following to set the slow-log file (5.1.6 onwards):
set global slow_query_log_file = 'path';
The variable slow_query_log just controls whether it is enabled or not.

I think the problem is making sure that MySQL server has the rights to the file and can edit it.
If you can get it to have access to the file, then you can try setting:
SET GLOBAL slow_query_log = 1;
If not, you can always 'reload' the server after changing the configuration file. On linux its usually /etc/init.d/mysql reload

These work
SET GLOBAL LOG_SLOW_TIME = 1;
SET GLOBAL LOG_QUERIES_NOT_USING_INDEXES = ON;
Broken on my setup 5.1.42
SET GLOBAL LOG_SLOW_QUERIES = ON;
SET GLOBAL SLOW_QUERY_LOG = ON;
set ##global.log_slow_queries=1;
http://bugs.mysql.com/bug.php?id=32565
Looks like the best way to do this is set log_slow_time very high thus "turning off" the slow query log. Lower log_slow_time to enable it. Use the same trick (set to OFF) for log_queries_not_using_indexes.

If you want to enable general error logs and slow query error log in the table instead of file
To start logging in table instead of file:
set global log_output = “TABLE”;
To enable general and slow query log:
set global general_log = 1;
set global slow_query_log = 1;
To view the logs:
select * from mysql.slow_log;
select * from mysql.general_log;
For more details visit this link
http://easysolutionweb.com/technology/mysql-server-logs/

This should work on mysql > 5.5
SHOW VARIABLES LIKE '%long%';
SET GLOBAL long_query_time = 1;

Related

web server always change mode to "ONLY_FULL_GROUP_BY"

As I reinstall old project for my customer and the service is working fine but I found sometimes it can't query some information. from checking it's because ##Global.sql_mode and ##SESSION.sql_mode set to "ONLY_FULL_GROUP_BY"
the step to solve it need to run command
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
nowadays it's happen 3 times per week.
So any way can I fix it permanent?
If you have control over the server instance then edit the config file to turn off ONLY_FULL_GROUP_BY.
SELECT ##sql_mode;
The above will probably return something similar to this -
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
Then you can add the following to your config file (same list as above with ONLY_FULL_GROUP_BY removed) -
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
Depending on your CakePHP version, you could add a settings key to your db config -
'settings' => array(
'##SESSION.sql_mode' => "'TRADITIONAL'",
),

Set locktimeout using JDBC for MySQl

Is there a way to set the locktimeout on SQL queries from the ConnectorJ JDBC driver. I'm looking for something like the SQL server:
connectURL = url + domain + ":1433;" + "databaseName="+databaseName+ ";lockTimeout=" + lockTimeOut;
driver parameter.
Thanks.
Thought I'd post a solution that I found for this, which I hope helps someone: for MySQl you're looking to add a line to your conf file. On Linux, SystemD, this resides inside /etc/my.cnf
Add this line to the [mysqld] entries
innodb_lock_wait_timeout=1
That's it. Lock timeout is now set to one second.

MySQL sort_buffer_size

I have received this error:
Bad SQL query Incorrect key file for table '/tmp/#sql_659_0.MYI'; try to repair it
So, following this suggesttion, I try to edit:
sort_buffer_size=4M
join_buffer_size=4M
But I can not find them. Where are they located on Ubuntu 14.04 machine? There are no such options in /etc/mysql/my.cnf
You can run the command find / -name my.cnf on the terminal.This would work as mentioned here
try finding the location of my.cnf as explained here
Secondly i think the title of this question should be:
Where to find my.cnf in Ubuntu 14.04
If you just want to change the values in the running instance you could do (just fire it as a query from anywhere):
SET #FourMegs = 1024 * 1024 * 4;
SET GLOBAL sort_buffer_size = #FourMegs;
SET GLOBAL join_buffer_size = #FourMegs;

SQL command to display history of queries

I would like to display my executed sql command history in my MYSQL Query Browser. What is the sql statement for displaying history?
try
cat ~/.mysql_history
this will show you all mysql commands ran on the system
For MySQL > 5.1.11 or MariaDB
SET GLOBAL log_output = 'TABLE';
SET GLOBAL general_log = 'ON';
Take a look at the table mysql.general_log
If you want to output to a log file:
SET GLOBAL log_output = "FILE";
SET GLOBAL general_log_file = "/path/to/your/logfile.log"
SET GLOBAL general_log = 'ON';
As mentioned by jeffmjack in comments, these settings will be forgetting before next session unless you edit the configuration files (e.g. edit /etc/mysql/my.cnf, then restart to apply changes).
Now, if you'd like you can tail -f /var/log/mysql/mysql.log
More info here: Server System Variables
You 'll find it there
~/.mysql_history
You 'll make it readable (without the escapes) like this:
sed "s/\\\040/ /g" < .mysql_history
(Linux)
Open your Terminal ctrl+alt+t
run the command
cat ~/.mysql_history
you will get all the previous mysql query history enjoy :)
Look at ~/.myslgui/query-browser/history.xml
here you can find the last queries made with mysql_query_browser
(some days old)
#GoYun.Info answer but with python 3
cat ~/.mysql_history | python3 -c "import sys; print(''.join([l.encode('utf-8').decode('unicode-escape') for l in sys.stdin]))"
You can see the history from ~/.mysql_history. However the content of the file is encoded by wctomb. To view the content:
shell> cat ~/.mysql_history | python2.7 -c "import sys; print(''.join([l.decode('unicode-escape') for l in sys.stdin]))"
Source:Check MySQL query history from command line
You can look at the query cache: http://www.databasejournal.com/features/mysql/article.php/3110171/MySQLs-Query-Cache.htm but it might not give you access to the actual queries and will be very hit-and-miss if it did work (subtle pun intended)
But MySQL Query Browser very likely maintains its own list of queries that it runs, outside of the MySQL engine. You would have to do the same in your app.
Edit: see dan m's comment leading to this: How to show the last queries executed on MySQL? looks sound.

MYSQL Slow Query

How can I view mysql slow_query_log to see which query is taking too much time?
First, you need to check if it's enabled in your MySQL configuration (mysql.ini or mysql.cnf, depending on your system):
# enable slow log:
slow_query_log = 1
# log queries longer than n seconds:
long_query_time = 5
# where to log:
slow_query_log_file = /path/to/your/logs/mysql-slow.log
Restart your MySQL server, then watch the logfile using whatever program you like - tail is the simplest:
tail -f /path/to/your/logs/mysql-slow.log
You may need to play a bit with the long_query_time setting to find the limit where the volume of logging isn't too low or too high, but just right.
Check the location of this log in my.ini file, and then open it in any text editor.
if you ask google for "slow_query_log", this is the first hit - explaining all you need to know. you have to enable it, set a filename you like (if it's already set, you can find the configuration in you my.ini), start your queries and look ito that file...
If you're running mysqld < 5.2, your my.cnf may look like
log-slow-queries=/var/log/mysql/mysql-slow.log //where to store log
long_query_time=3 //quickest query to log