Author: banyek

  • Keeping track of database table size changes in MySQL

    I don’t know how common this problem is, but it is good to know from time to time how much disk space each table needs at a certain time. For example, you can catch a piece of software running amok that fills up your database. Or, – as you will see – you can catch some code that doesn’t work as expected.

    So, let’s start at the beginning. You wanna know how big your tables are, and you need to know how much data gets there day-by-day (or minute-by-minute, or whatever).

    (more…)

  • MySQL connections listing

    Currently, at Kinja we are in the middle of a big architectural change on database servers, so I have run into a problem regarding this. Sometimes I have to check current connections on database servers, to see what schemas are in use, what servers are using a given DB server, or even which users are connected to the database server.

    Previously when I had to determine connected hosts, I just used a one-liner script in bash that parsed through the output of netstat and listed the number of connections from given servers like this:

    [root@sql-slave1 banyek]# netstat | grep mysql | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c
          1 app01.bfc.kinja-ops.c
         83 app05.bfc.kinja-ops.c
         84 app09.bfc.kinja-ops.c
          9 dbcacti.bfc.kinja-ops
          1 nagios.bfc.kinja-ops.
          1 sql-master1.bfc.kinja
    

    This was enough to quickly see the connected hosts, but the output wasn’t too chatty, and a lot of information was hidden.

    (more…)

  • Grep trick for locating processes

    Yesterday Dominis mentioned shell-foo, and one cool thing that one of my ex-colleagues showed me a few years ago came to mind. (Hi Pali!)

    So let’s pretend you have to locate the pid of a mysql process. You run the good old ps command and grep for mysql:

    (more…)

  • MySQL replication module for ansible

    We maintain a lot of servers under Kinja, so we have to use some orchestrator software to perform some tasks on a lot of servers. The Ansible software is used by us because it is cool.

    We also have a lot of MySQL servers (and counting!) under Kinja, so we have some tasks to perform on them, such as managing replication. Of course, there are some ways to do this, for example by using multiplexed terminals or running ansible shell commands that perform mysql queries (e.g. ansible mysql-master1 -m shell -a “mysql -e “SOME SQL QUERY HERE”) but it is not too comfy and needs a lot of manual work.

    So, there is a way to make it easier for us, and that’s why I made a mysql_replication module for Ansible. (And I made a pull request for that on GitHub, so I hope it will be merged into the ‘official’ branch soon.)

    The mysql_replication module helps you to

    • Query slave status
    • Query master status
    • Change the replication slave’s parameters (such as master server IP, master_log_pos, etc.)
    • Stop the slave thread
    • Start the slave thread

    The module can be found in my GitHub repo here.

  • Free up space on mysql servers

    First, I have to tell you that “fragmentation” is not the best word I should use, but that was the closest to what I wanted to say. The base of the fragmentation checker script is Peter Zaitsev’s 2008 article about Information_schema. I used that query to get the results I needed.

    Some background: a few weeks ago we got some problems related to free space (well, they weren’t real problems, but they could lead to one, and we had to act fast). InnoDB works in a bit of a strange way. It doesn’t like to free up space after data has been deleted, and because of this, the datafiles will grow to the end of the world. The first thing we could do to reclaim free space is to use the

    innodb_file_per_table
    

    configuration option, which will split up the database into a lot of .ibd datafiles, instead of one big ibdata file. If you got your data in different tables, you can run “OPTIMIZE TABLE” from mysql, which will compact the datafiles (it creates a new one, copies the table contents onto this new table, deletes the old file, and renames the new) for you. It takes a lot of time to run, and you have to know that the “optimize” command will also be replicated, so after you do this on the master, be aware that it will happen on the slaves too. So, when you take some maintenance time, you have to factor this in. It’s easy to determine what tables would be optimized. You can check the table size from the data dictionary, and you can see how big that table is on disk. If they differ, then you can reclaim the space that is the difference between the two sizes.

    (more…)