June 15, 2009

How to pretty-print my.cnf with a one-liner

Posted by Baron Schwartz

When I'm looking at a server, I often want to see the /etc/my.cnf file nicely formatted, and with comments stripped. This Perl one-liner will pretty-print the file:

CODE:
  1. perl -ne 'm/^([^#][^\s=]+)\s*(=.*|)/ && printf("%-35s%s\n", $1, $2)' /etc/my.cnf
  2. [client]                           
  3. port                               = 3306
  4. socket                             = /var/run/mysqld/mysqld.sock
  5. [mysqld_safe]                     
  6. socket                             = /var/run/mysqld/mysqld.sock
  7. nice                               = 0
  8. [mysqld]                           
  9. user                               = mysql
  10. pid-file                           = /var/run/mysqld/mysqld.pid
  11. socket                             = /var/run/mysqld/mysqld.sock
  12. port                               = 3306
  13. ....

June 11, 2009

The feature I love in TokuDB

Posted by Vadim

Playing with TokuDB updates I noticed in SHOW PROCESSLIST unsual for MySQL State.

CODE:
  1. mysql> show processlist;
  2. +----+------+-----------+--------+---------+------+---------------------------+-----------------------------+
  3. | Id | User | Host      | db     | Command | Time | State                     | Info                        |
  4. +----+------+-----------+--------+---------+------+---------------------------+-----------------------------+
  5. 3 | root | localhost | sbtest | Query   |   30 | Updated about 764000 rows | update sbtest set email=zip |
  6. ...
  7. mysql> show processlist;
  8. +----+------+-----------+--------+---------+------+----------------------------+-----------------------------+
  9. | Id | User | Host      | db     | Command | Time | State                      | Info                        |
  10. +----+------+-----------+--------+---------+------+----------------------------+-----------------------------+
  11. 3 | root | localhost | sbtest | Query   |   79 | Updated about 1900000 rows | update sbtest set email=zip |
  12. ...

(Do not look in stupid UPDATE query, it's just for testing :) )

So looking in SHOW PROCESSLIST you can see progress of query execution.

I would want to see it in standard MySQL and InnoDB more than all these triggers and stored routines! Probably will implement this in XtraDB.

June 5, 2009

A rule of thumb for choosing column order in indexes

Posted by Baron Schwartz

I wanted to share a little rule of thumb I sometimes use to decide which columns should come first in an index. This is not specific to MySQL, it's generally applicable to any database server with b-tree indexes. And there are a bunch of subtleties, but I will also ignore those for the sake of simplicity.

[read more...]

May 31, 2009

Using netcat to copy MySQL Database

Posted by peter

This is mainly a cheat sheet for me to remember. Nothing rocket science.

It often makes sense to use netcat/nc to copy MySQL database between hosts in trusted networks. It bypasses encryption overhead of SSH and depending on configuration can be significantly faster.

Also note MySQL should be down when you copy data unless you're copying from snapshot etc.

So to copy go to the mysql data directory on both boxes; such as cd /var/lib/mysql . Make sure target directory is empty. Now on the TARGET server do nc -l 4000 | tar xvf - and on the SOURCE server do tar -cf - . | nc target_ip 4000

Also note - the port you're using should be open in the firewall.

May 21, 2009

Mass killing of MySQL Connections

Posted by peter

Every so often I run into situation when I need to kill a lot of connections on MySQL server - for example hundreds of instances of some bad query is running making server unusable. Many people have special scripts which can take the user, source host or query as a parameter and perform the action. There is also a way to do it just using MySQL with a few commands:
[read more...]

May 20, 2009

Hint: throttling xtrabackup

Posted by Vadim

Using xtrabackup for copying files can really saturate your disks, and that why we made special option --throttle=rate to limit rate of IO per second. But it really works when you do local copy.
What about stream backup ? Even you copy just to remote box with
innobackupex --stream=tar | ssh remotebox "tar xfi -"
, read may be so intensive so your mysqld feels impact, slave getting behind, etc...

For this there is a nice small utility - pv .

With pv you run:
innobackupex --stream=tar | pv -q -L10m | ssh remotebox "tar xfi -"

and it will limit channel rate to 10 M per second.

May 17, 2009

What time 18446744073709550.000 means

Posted by Vadim

Sometimes when you do profiling you can see number like this in timestamps. Periodically in our patches there was Query_time: 18446744073709550.000 in slow.log file (well, it was fixed recently, but still appears in other places).

I faced this problem several years ago when only 2-core AMD Opteron systems appeared and I noticed sysbench getting crazy showing query execution time 18446744073709550.000 or like this.

Obviously this is unsigned integer which was received by subtraction bigger number from smaller.
But how it can be if we use:

start_time = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp);
...
query_execution
...
end_time = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp);

total_time = end_time - start_time;

How we can get query executed in negative time ? It would be too good to be real :).

After some research I found the reason, and it is interesting that I want to share it even few years later. The reason is that different CPU on the same motherboard uses different frequency generators (each CPU has its own frequency generator, though exact implementation depends on CPU vendor), and there is no way to get these generators synchronized. That is if in your server you have 4 CPU, each of them lives on its own time. Usually difference between generators is very small (microseconds matter), but it appears this difference is enough to get negative time.
It happens when your query started on one CPU and during execution it was switched to another CPU (where generator delayed to first CPU) and time between two measurements were small enough to catch difference in generators. In result you have negative time, but it actually, it is more serious, it mean - we can't get exact time between two points in application.
So designing profiling in your application be aware of. I wonder how realtime sensor systems, where microseconds are important, deal with this.

May 14, 2009

Why MySQL’s binlog-do-db option is dangerous

Posted by Baron Schwartz

I see a lot of people filtering replication with binlog-do-db, binlog-ignore-db, replicate-do-db, and replicate-ignore-db. Although there are uses for these, they are dangerous and in my opinion, they are overused. For many cases, there's a safer alternative.

[read more...]

May 7, 2009

Btw xtrabackup is not only backup..

Posted by Vadim

It is obvious thing for me, but I just figured out it may be not common understanding. Xtrabackup is also can be used (not only can, but we actually use it this way) to clone one slave to another, or just setup new slave from the master. And it is done in almost non-blocking way ( true for InnoDB setups) for cloned server. Here is command

CODE:
  1. innobackupex-1.5.1 --stream=tar /tmp/ --slave-info | ssh user@DESTSERVER "tar xfi - -C /DESTDIR"

When it finished on destination server you run

CODE:
  1. innobackupex-1.5.1 --apply-log --use-memory=2G  /DESTDIR

And you have ready database directory, just copy my.cnf from original server and start mysqld.

April 15, 2009

How to decrease InnoDB shutdown times

Posted by Baron Schwartz

Sometimes a MySQL server running InnoDB takes a long time to shut down. The usual culprit is flushing dirty pages from the buffer pool. These are pages that have been modified in memory, but not on disk.

If you kill the server before it finishes this process, it will just go through the recovery phase on startup, which can be even slower in stock InnoDB than the shutdown process, for a variety of reasons.

[read more...]