MySQL Performance Analyse mit mtop
Das Linux Tool mtop zeigt jene MySQL Kommandos, die am meisten CPU Zeit konsumieren. Standardmäßig werden nur non-sleeping Threads angezeigt - mittels --idle Option können diese eingeblendet werden.
Inhaltsverzeichnis |
Anwendung von mtop
Ähnlich wie top zeigt auch mtop nach dem Starten eine interaktive Ansicht (mtop wird über den gleichnamigen Befehl mtop von der Kommandozeile aus gestartet):
load average: 0.62, 0.36, 0.17 mysqld 5.0.32-Debian_7etch8-log up 50 day(s), 0:54 hrs 11 threads: 2 running, 6 cached. Queries/slow: 88.3M/0 Cache Hit: 99.99% Opened tables: 0 RRN: 21.9K TLW: 2.5K SFJ: 0 SMP: 0 QPS: 797 ID USER HOST DB TIME COMMAND STATE INFO 783968 neufelde localhost neufeldensql Query Writing to n SELECT ... FROM tt_content WHERE uid=738 784066 mysqltop localhost Query show full processlist ---
Durch drücken der 'e' Taste kann man nähere Details (explanation) abfragen. Dies erfordert einen User der die Berechtigung hat, auf diese Datenbank zuzugreifen:
explain 783968 Unable to connect to neufeldensql1 as mysqltop, enter another user user: root Password:
Anschließend werden folgende Details ausgegeben:
Id: 783968 User: neufeldensql1 Host: localhost Db: neufeldensql1 Time: 0 Command: Query State: Writing to net SELECT * FROM tt_content WHERE uid=738 table |type |possible_keys |key | ken_len|ref | rows| 1 |SIMPLE |tt_content |const | PRIMARY|PRIMARY | 4|
Hilfetext
Durch Drücken von '?' zeigt das laufende mtop folgenden Hilfetext:
mtop ver 0.6.6/20070304, Copyright (c) 2002, Marc Prewitt/Chelsea Networks
A top users display for mysql
These single-character commands are available:
q - quit
? - help; show this text
f - flush status
F - fold/unfold column names in select statement display
k - kill processes; send a kill to a list of ids
s - change the number of seconds to delay between updates
m - toggle manual refresh mode on/off
d - filter display with regular expression (user/host/db/command/state/info)
h - display process for only one host
u - display process for only one user
i - toggle all/non-Sleeping process display
o - reverse the sort order
e - explain a process; show query optimizer info
t - show mysqld stats (show status/mysqladmin ext)
T - show mysqld important stats
v - show mysqld variables (show variables/mysqladmin vars)
z - zoom in on a process, show sql statement detail
r - show replication status for master/slaves
Stats Explanation (See SHOW STATUS docs for full details):
Cache Hit: Key_read / Key_read_requests. If small, consider increasing
key_buffer_size (current=16MB)
Opened tables: If large, consider increasing table_cache
(current=64)
RRN: Handler_read_rnd_next High if you are doing a lot of table scans.
TLW: Table_locks_waited If high, consider optimising queries or
splitting db.
SFJ: Select_full_join Number of joins without keys (Should be 0).
SMP: Sort_merge_passes If high, consider increasing sort_buffer
(current=0B).
Installation von mtop
Installation von mtop unter Debian
Bei Debian ist mtop bereits im Repository enthalten. Die Installation kann daher direkt mittels apt-get durchgeführt werden. Bei der Installation versucht mtop allerdings einen MySQL User anzulegen. Falls für den MySQL root Benutzer ein Passwort definiert ist, schlägt dies bei der Installation fehl:
debian40srv:/# apt-get install mtop Reading package lists... Done Building dependency tree... Done The following extra packages will be installed: libcurses-perl The following NEW packages will be installed: libcurses-perl mtop 0 upgraded, 2 newly installed, 0 to remove and 1 not upgraded. Need to get 167kB of archives. After unpacking 594kB of additional disk space will be used. Do you want to continue [Y/n]? y Get:1 http://ftp.debian.at etch/main libcurses-perl 1.13-1 [116kB] Get:2 http://ftp.debian.at etch/main mtop 0.6.6-1.2 [51.7kB] Fetched 167kB in 1s (116kB/s) Preconfiguring packages ... Selecting previously deselected package libcurses-perl. (Reading database ... 18848 files and directories currently installed.) Unpacking libcurses-perl (from .../libcurses-perl_1.13-1_i386.deb) ... Selecting previously deselected package mtop. Unpacking mtop (from .../mtop_0.6.6-1.2_all.deb) ... Setting up libcurses-perl (1.13-1) ... Setting up mtop (0.6.6-1.2) ... ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) dpkg: error processing mtop (--configure): subprocess post-installation script returned error exit status 1 Errors were encountered while processing: mtop E: Sub-process /usr/bin/dpkg returned an error code (1) debian40srv:/#
Damit der Benutzer für mtop erstellt werden kann ist es erforderlich das MySQL root Benutzer Passwort kurzzeitig zurückzusetzen. Dazu verwendet man folgende MySQL Befehle:
-
UPDATE mysql.user SET Password=PASSWORD() WHERE User='root'; -
FLUSH PRIVILEGES;
debian40srv:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 783675
Server version: 5.0.32-Debian_7etch8-log Debian etch distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> UPDATE mysql.user SET Password=PASSWORD('') WHERE User='root';
Query OK, 2 rows affected (0.03 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.09 sec)
mysql> exit
Bye
debian40srv:~#
Nun funktioniert das Einrichten des Benutzers:
debian40srv:~# apt-get install mtop Reading package lists... Done Building dependency tree... Done mtop is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded. 1 not fully installed or removed. Need to get 0B of archives. After unpacking 0B of additional disk space will be used. Setting up mtop (0.6.6-1.2) ... debian40srv:~#
Abschließend wird das Passwort für den MySQL root Benutzer wieder gesetzt:
debian40srv:~# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 783717
Server version: 5.0.32-Debian_7etch8-log Debian etch distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> UPDATE mysql.user SET Password=PASSWORD('hier-kommt-das-passwort-hinein') WHERE User='root';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
debian40srv:~#
Hinweis: durch dieses Setzen des Passwortes gelangt das Passwort im Klartext in die MySQL History. Es ist empfehlenswert die betreffende Zeile einfach mit einem Editor aus der MySQL History Datei ~/.mysql_history zu entfernen.
Installation von mtop unter anderen Distributionen
Für andere Distributionen kann es erforderlich sein, mtop selbst zu kompilieren. Vereinzelt gibt es aber auch vorbereitete Packages (z.B. unter http://dag.wieers.com/rpm/packages/mtop/ - siehe auch http://techgurulive.com/2008/08/26/how-to-install-mtop-mysql-monitoring-tool-on-rhel/).