How to detect what is running on MySQL every hour? - mysql

Munin showing huge spike on MySQL queries every hour but I am unable to detect what is causing this. I am running version 5.6.30.
Tried to enable slow running queries but can't find it there.
Also logged all queries and tried to see what is running on that particular time. I cannot find it.
Checked cronjobs but there wasn't anything related
Disabled almost everything on LFD & CSF
The event scheduler status is set to OFF
Is there any other way to find what is running every hour?
Munin graph showing sql queries:

You can use a shell script and put the instruccion "processlist" and send the output to a log file.
Put the delay in seconds that you want for run again the instucction.
while [true];
mysql -h localhost -u root -ppasswd < process
delay xtime
done
And the file processlist you put the instrucction "show full processlist"
I hope this help you
Regards

Some crawlers were mining data from my website. I wasn't able to detect because requests sent from a million different IPs.
Added captcha to website as human control and spikes gone.

Related

Safely Cancel a MySQL import

I'm trying to upload a large database:
$mysql -u USERNAME -p MY_DATABASE < MY_DATABASE.sql
But the process is going extremely slowly, I would like to cancel and try again with different settings. How can I cancel this upload 'safely'? Or what is the best way to clean up my database after forced termination of the import?
You can kill MySQL process involved in your MY_DATABASE.sql
This show you running processes:
show processlist;
Then you should kill the process selecting it by the correct process_id from the user and time shown from previous command to understand which one is the correct one, if others processes are running:
kill process_id
I think that cleaning the DB is the hardest work, because it depends on the MY_DATABASE.sql content: if it populates a table, you can simply truncate it, if it create a DB and upload tables, views, stored procedures and other elements, you should drop the DB.
If you take a look at the MY_DATABASE.sql content and at the server content after killing the process (db created? tables or other elements created?) you will understand what to do.
If, as I can imagine, the script creates entirely a DB, you should only have to drop the DB created from the previous interrupted upload, if any, and restart the upload.
Rather stopping MySQL import with killing the process, I prefer use terminate key which is ctrl+c and service mysqld restart or service mysql restart.
This just ends the import process and it gets sometimes to end. Once it's done you can go with your process. If you end the process with killing it may cause some unexpected errors like a mysql server crash.

start mysql service if it is down programatically

I have a ubuntu server and i want to monitor it and start mysql service if it goes down, what can i do programatically to implement this.
I see this solution and set it via cron to execute every minute.
#!/bin/bash
if [[ ! "$(/usr/sbin/service mysql status)" =~ "start/running" ]]
then
/usr/sbin/service mysql start
fi
but would setting a cron to execute every minute load my server.
Any help in this direction will be appreciated. Thanks.
For the most part, running a short script like that every minute won't stress your server. So programmatically, assuming you tested it and all, I don't see much wrong with your approach.
I'd be more concerned as to why your service randomly shuts down though, have you looked for clues in logs? Depending on the severity of the cause, perhaps implementing a monitoring tool like nagios or icinga might be useful.

How do you investigate contention issues on MySQL

I am a website developer and I am being told by the server administrator that there are contention issues on the mysql database. I would like to investigate these contention issues myself and see what they are but I do not know how or where to look for contention issues. Please can you advise me where to look. Do I need to look in the logs? If so what logs and where will I find them? The database is MySQL v5.1 running on linux. I'm not sure what version yet as I don't have control panel or ssh access but I aim to get that today.
Thanks very much
Contention issues is usually a fancy way of saying there are locking problems, meaning queries get locked status.
You need to identify them and check your app for solutions.
You can run SHOW PROCESSLIST when server is busy to see what queries are currently locked.
SHOW OPEN TABLES can also help you to identify the problem and another question posted here
First you should check the mysql is running or not by using below command.
mysqladmin -u root -p status
This command will show :-
Enter password:
Uptime: 4 Threads: 1 Questions: 62 Slow queries: 0 Opens: 51 Flush tables: 1 Open tables: 60 Queries per second avg: 15.200
Also check the error type :-
http://dev.mysql.com/doc/refman/5.5/en/privilege-system.html
Maybe you should start by asking your server administrator what he/she sees that makes him/her believe that there are contention issues - they have a much better view of your DBMS than we do. And by the sound of things you are paying them for this service.
It would have been helpful if you'd said which database engine the tables are stored in: this is by far the most critical issue for contention in MySQL. You may be able to solve your problems by switching from MyISAM to Innodb.
I don't have control panel or ssh access
IME off-the-shelf control panels are fine for (very) rudimentary admin tasks, but not much use for this.
I would start by looking at the slow query log. This should be configured to log all queries - if it's not tell the admin you need it changed. Look at which queries have high lock times, then identify the lonf executing queries which are causing the high lock times. Then fix them.

Cancel a mysql query

Is it possible to cancel a mysql query?
I have the problem that I submit a query which is very time-consuming because of a mistake. And now I can't make a new query because the server is working and working...
Or is there a way to stop all queries in a database or in a table.
For your information I am using phpMyAdmin (MySql).
In phpMyAdmin, go to home page > Status; you'll see a list of your MySQL processes and you have a Kill link for each of them.
Probably your phpMyAdmin will be stuck showing the "Searching" message. What you can do is to open another tab / open a MySQL session with the console and do the following:
Look for all the processes running:
SHOW PROCESSLIST;
And then kill that specific thread with the KILL command:
KILL <thread_id>;
Resources:
SHOW PROCESSLIST page in MySQL
KILL page in MySQL
You could use the KILL statement : http://dev.mysql.com/doc/refman/5.0/en/kill.html
Sometimes, you can't even get to phpmyadmin even on a new tab.
If you have access to the environment via command line,
1) log into mysql
mysql -u yourusername -p
2) SHOW PROCESSLIST;
3) KILL <thread_id>;
This might help you:
Post in Mysql Forum
This will teach you to test your queries before just throwing them into a page. EXPLAIN PLAN is your friend.
Sometimes if the database server is not responding because of a too long query and:
the phpMyAdmin is stuck showing the "Loading..." message...
you can't log into MySQL server with appropriate permissions or if so but theSHOW PROCESSLIST command in CLI is not very useful because of for example too big amount of started processes...
then, the fastest option might be to restart the server, which stops all queries in a database:
service mysql restart
or
sudo /etc/init.d/mysql restart
This is not the safest option, because in some cases the interrupted queries may cause inconsistency of data in the database..., but sometimes it may be the best solution.

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;"