mysql loggin slow queries - mysql

I ran the mysqltuner script recently and i noticed around 5000 joins done without indexes, this has to be reduced to a small value.
There is an option that allows us to log these queries in mysql
i have added the following lines under [mysqld] section of my.cnf
log-queries-not-using-indexes
log_slow_queries=/var/log/mysqld.slow.log
But the logs still remains empty, how can i get the logging to work in order to optimize those queries

Did you restart MySQL after doing this?
sudo service mysql restart
If you didn't, it won't log anything. I've also wondered myself if there's a delay in when it actually logs queries. Give it time, the logs will show up.

Related

Q: MYSQL Data tables suddenly missing

Earlier today [11-09-2021] one of our databases from our production environment suddenly dropped it's table for reasons we don't know. This happened around after 4am, since we still had a snapshot of our drive for that time, which is weird as no one was using or accessing the server at the time. Can someone tell if this normally happens?
This for sure its not normal behavior, you should check MySQL logs to see what was happening at that time.
In MySQL we need to see often 3 logs which are mostly important:
The Error Log. It contains information about errors that occur while the server is running (also server start and stop)
The General Query Log. This is a general record of what mysqld is doing (connect, disconnect, queries)
The Slow Query Log. Ιt consists of "slow" SQL statements (as indicated by its name).
The one that will be your starting point is The General Query Log.
By default no log files are enabled in MYSQL. All errors will be shown in the syslog (/var/log/syslog).
To Enable them just follow below steps:
1. Go to mysql conf file (/etc/mysql/my.cnf) and add following lines:
Enable general query log add following
general_log_file = /var/log/mysql/mysql.log
general_log = 1
2. Save the file and restart mysql using following commands
service mysql restart
To read content of the error log file in real time, run:
sudo tail -f $(mysql -Nse "SELECT CONCAT(##datadir, ##general_log_file)")
Hope this will help you to find out what actually happened on your database server.

Enable alerts for MySQL long running queries

My application is loading very slow. After doing some research I got to know that MySQL is causing this slowness. I have around 15-20 users that access this server. After preliminary investigation (googling and stackoverflow), I found out that they were some queries running at the time and those were the culprit. It’s annoying to run query every now and then to look out for the queries running for a long time. Is there a workaround for this and also get email/SMS alerts for it. How can I enable email/SMS alerts to look over those queries
execute below commands on mysql from admin/root user-
slow_query_log = ON;
long_query_time = 2;
Now get slow query log file path from belowcommand-
SHOW VARIABLES LIKE 'slow_query_log_file';
Now go to your mysql db machine and check logged slow queries and optimize them.
Note: By this you can get slow queries even without restart mysql service but for better do these entries in your conf file so that you can get slow logs even after service restart.

MySQL long_query_time value

I'm debugging a MySQL 5.1.61 database, and I have the long_query_time set in the my.cnf file to 10. Slow queries are being logged to a database table.
However, it's logging queries that take a fraction of a second.
In fact, my queries being logged are so fast, the query_time field in mysql shows "00:00:00" for every query logged. Even when I had them logging to a file, they showed query times in the range of "Query_time: 0.004763"
I know that my configuration file is being read, because all my other changes have worked.
From all the documentation I've read, long_query_time should be seconds. Is there something else I need to do for that setting to stick?
Do those queries have indexes? If not, then that's probably why they're being logged.
Before MySQL 4.1, if you also use --log-long-format when logging slow queries, queries that are not using indexes are logged as well. Starting with MySQL 4.1, logging of queries not using indexes for row lookups is enabled using the --log-queries-not-using-indexes option instead. The --log-long-format is deprecated as of MySQL 4.1, when --log-short-format was introduced, which causes less information to be logged. (The long log format is the default setting since version 4.1.) (>>)
I believe you just forgot to restart your Mysql after editing 'my.cnf' file?
sudo /etc/init.d/mysqld restart
or
sudo service mysql restart

Mysqld and logging queries

I'm seriously having problems logging queries with mysql. I've opened my.cnf and modified a few lines, according to some online sources. But the outcome was nil.
I've also read up on the docs at dev.mysql.com: http://dev.mysql.com/doc/refman/5.1/en/query-log.html
I'm obviously misunderstanding what they're trying to convey.
Here's what I'm typing into the command line:
service mysqld start -l mysqllogs.log
That didn't work.
I've opened /etc/my.cnf and added:
[mysqld_safe]
log-error=/var/log/mysqld/errors.log
log=/var/log/mysqld/mysql.log
pid-file=/var/run/mysqld/mysqld.pid
I even tried:
log-query=/var/log/mysqld/queries.log
So far I don't see any queries being logged. But I do see the errors.log getting generated just fine. I'm confused.
I don't want to edit the /etc/init.d/mysqld , kinda worried I'll mess something up.
Has anyone had this issue? How do I turn on query logging? (not talking about slow queries)
mysqld_safe is just shell script that invokes mysql server. So you need to add lines to [mysqld] section. Not in [mysqld_safe]
kind of
[mysqld]
log=/var/log/mysqld/queries.log
and restart mysql server after that.

How do I routinely kill MySQL queries that have been alive for "too long"?

How do I routinely kill MySQL queries that have been alive for "too long"?
Is there a system information table of sorts that shows all current queries, and their age?
Edit: updated question from "killing connections" to "killing queries"
Install the RubyGem mysql_manager (sudo gem install mysql_manager) and then run a command like this:
mysql-manager --kill --kill:user api --kill:max-query-time 30 --log:level DEBUG
For more options, run mysql-manager --help.
You might need to specify an alternative --db:dsn, --db:username, or --db:password.
Read more about it here: https://github.com/osterman/mysql_manager
You can execute...
SHOW FULL PROCESSLIST;
...to show you the currently executing queries.
However, you shouldn't just kill the connections, as this may cause data integrity issues. Instead, you should use the processlist output as a means of highlighting where potential problems may lie prior to correcting the issues at source. (It's sort of a (very) poor man's MySQL Enterprise Monitor in that sense.)
MySQL 5.0.x only supports the “SHOW FULL PROCESSLIST” command. There’s no ability to query and filter the process list as thought it were a SQL table, e.g. SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST. This ability was added in MySQL 5.1+
MySQL has a KILL command that can kill either a query or the entire connection.
http://dev.mysql.com/doc/refman/5.0/en/kill.html
Still, you’d need a Ruby or Perl script that runs “SHOW FULL PROCESSLIST”, identifies which queries are running “too long”, then issues the appropriate KILL commands.
You can also do this from the command line, e.g.
mysqladmin processlist
mysqladmin kill
The command "mysqladmin processlist" will show the current connection, and a Time column which indicates the time since last activity.
You can do the same with the SQL command "SHOW FULL PROCESSLIST;"