Slow query log in mysql - mysql

Can someone share how to enable slow query log (to see the slow queries) in Mysql 5 versions?
I need to analyse the slow queries, please suggest.
Thanks in advance.

Begin by reading the documentation.
Edit
You can add one of the following lines in mysql server's configuration file:
log-slow-queries
log-slow-queries = /path/to/file
More recent versions of MySQL use these settings instead:
slow_query_log = 1
slow_query_log_file = /path/to/file
The slow query log file will contain the complete query text plus some additional information about the queries that take more then 10 seconds to execute. The 10 second threshold is configurable as well:
long_query_time = 10

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

Related

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

Enable MySQL syslog to log aborted connections and access-denied

What is the command in mysql that is required to that I can implement aborted connections and access-denied logs to be written to the syslog?
The commands are first to see what your settings are:
select ##general_log; -- a 1 indicates it is turned on for capture
select ##general_log_file; -- the file that it logs to
select ##datadir; -- directory location where the log lives
To turn logging on for the General Query Log, use the following:
SET GLOBAL general_log = 'ON'; -- 0 is off, 1 is on
Note that the datadir is read-only and requires a restart of the server after changes. Basically, don't change this. It is the home of your database schemas.
For expanded connection failures perform a
select ##log_warnings; -- make a note of your prior setting
set global log_warnings=2; -- setting above 1 increases output
The immediate above relates to the Error log written out in the same datadir.
See the Percona article Auditing login attempts in MySQL and my prior answer Here.

how to set up long_query_time?

I would like to know , how to set up the long_query_time as 1 minute . I want the queries that run more than 1 minute in my slow_query_log . how to do that ? And kindly provide me how to restart the sql ? is it the command MySQL service restart ; ?
Thanks in advance.
Prabhakaran.R
In my.cnf, add (or change to)
long_query_time = 60
and restart mysqld (The details depend on what OS you are using and whether you are using some package, such as WAMP, which has its own 'restart' mechanism.)
Or, you set it for the remainder of your session by executing this command. (The details depend on what client you are using.)
SET long_query_time = 60

general_log.csv deleted by accident now error Can't get stat of './mysql/general_log.CSV' (Errcode: 2)

We have a dedicated web server with a 50Gb partition for MySQL.
In order to do some tunning on MySQL, I turned on general_log var using:
set global general_log = 1;
set global expire_logs_days = 30;
After 2 days the general_log.CSV took over all the free space in the partition, making MySQL unavailable.
Our tech support on the hosting company , solved that by deleting the file.
Now I need to turn on general_log again and I get this error:
Can't get stat of './mysql/general_log.CSV' (Errcode: 2)
What can I do to fix it?
Thanks

How do I enable the MySQL slow query log? [duplicate]

This question already has answers here:
How can I enable MySQL's slow query log without restarting MySQL?
(8 answers)
Closed 8 years ago.
My MySQL version details are
Server: Localhost via UNIX socket
Software: MySQL
Software version: 5.0.96-community-log - MySQL Community Edition (GPL)
Protocol version: 10
How do I enable the MySQL slow query log?
Version 5.1.6 and above:
1. Enter the MySQL shell and run the following command:
set global slow_query_log = 'ON';
2. Enable any other desired options. Here are some common examples:
Log details for queries expected to retrieve all rows instead of using an index:
set global log_queries_not_using_indexes = 'ON'
Set the path to the slow query log:
set global slow_query_log_file ='/var/log/mysql/slow-query.log';
Set the amount of time a query needs to run before being logged:
set global long_query_time = 20;
(default is 10 seconds)
3. Confirm the changes are active by entering the MySQL shell and running the following command:
show variables like '%slow%';
Versions below 5.1.6:
Edit the /etc/my.cnf file with your favorite text editor
vi /etc/my.cnf
Add the following line under the “[mysqld]” section. Feel free to update the path to the log file to whatever you want:
log-slow-queries=/var/log/mysql/slow-query.log
3. Enable additional options as needed. Here are the same commonly used examples from above:
Set the amount of time a query needs to run before being logged:
`long_query_time=20
(default is 10 seconds)`
Log details for queries expected to retrieve all rows instead of using an index:
`log-queries-not-using-indexes`
4. Restart the MySQL service:
service mysqld restart
5. Confirm the change is active by entering the MySQL shell and running the following:
show variables like '%slow%';
Update:1
According to MySQL docs, the error #1193 occurs when you use wrong code for SQLSTATE.
Message: Unknown system variable %s
And, as you can see on the same page, the SQLSTATE 99003 is not defined.
refer this link:
http://dev.mysql.com/doc/refman/5.5/en/slow-query-log.html
http://dev.mysql.com/doc/refman/5.1/en/slow-query-log.html
If your server is above 5.1.6 you can set the slow query log in the runtime itself. For which you have to execute this queries.
set global log_slow_queries = 1;
set global slow_query_log_file = <some file name>;
Or alternatively you can set the this options in the my.cnf/my.ini option files
log_slow_queries = 1;
slow_query_log_file = <some file name>;
Refer: http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_slow_query_log_file