Monday, January 28, 2013

relay-log-space-limit : IO_Thread and SQL_Thread

relay-log-space-limit

We don’t often see this option configured (default: unlimited) but it might be a good idea to set it. What it does is limit the amount of disk space the combined relay logs are allowed to take up.

A slave’s IO_Thread reads from the master and puts the events into the relay log;
the slave’s SQL_Thread reads from the relay log and executes the query.

If/when replication “breaks”, unless it’s connection related it tends to be during execution of a query. In that case the IO_Thread will keep running (receiving master events and storing in the relay log). Beyond some point, that doesn’t make sense.

The reason for having two separate replication threads (introduced in MySQL 4.0) is that long-running queries don’t delay receiving more data. That’s good. But receiving data is generally pretty fast, so as long as that basic issue is handled, it’s not necessary (for performance) to have the IO_Thread run ahead that far.
So you can set something like relay-log-space-limit=256M. This prevents slave disk space from getting gobbled up in some replication failure scenarios. The data will still be available in the logs on the master (provided of course the log expiration there isn’t too short – replication monitoring is still important!).

Conclusion: the relay log as a cache. Don’t leave it at “Unlimited”, that’s inefficient (and potentially problematic) use of resources. If you do run out of diskspace, the relay log can get corrupted – then you have to reposition, which will re-read the data from the master anyway.

Monday, January 21, 2013

Mass killing of MySQL Connections

Mass killing of MySQL Connections

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:


mysql> select concat('KILL ',id,';') from information_schema.processlist where user='root';
+------------------------+
| concat('KILL ',id,';') |
+------------------------+
| KILL 3101;             |
| KILL 2946;             |
+------------------------+
2 rows in set (0.00 sec)

mysql> select concat('KILL ',id,';') from information_schema.processlist where user='root' into outfile '/tmp/a.txt';
Query OK, 2 rows affected (0.00 sec)

mysql> source /tmp/a.txt;
Query OK, 0 rows affected (0.00 sec)

Kill all threads from a MySQL user

Kill all threads from a MySQL user

Terminal - Kill all threads from a MySQL user 
mysql -BNe "SELECT id FROM processlist WHERE user = 'redmine';" information_schema | while read id; do mysqladmin kill $id; done

Kill all threads from a MySQL user
Kills all the threads from the user provided in the WHERE request.

Alternatives

Terminal - Alternatives 
for i in `mysqladmin -h x.x.x.x --user=root -pXXXX processlist | grep <<username>>| grep <<Locked>>| awk {'print $2'}` do mysqladmin -h x.x.x.x --user=root -pXXX kill $i; done;

Unix nohup : How to spool Log File And Error File

Unix nohup Log File And Error File

Unix type operating systems have a command called nohup which means no hang up. Problem with running applications especially when you log off from the system, is that the application that was running gets terminated as well. To avoid this problem, we use the nohup command. If in case we want to separate the log file from the error file, this is the way to do it:
nohup ./[file] 2>[error_file] > [log_file]

Tuesday, January 15, 2013

In Unix, how do I check who else is logged into the same computer as I am?

You can use any one of several Unix commands to see who is logged into the same computer as you. Each command yields different amounts of information:

You can obtain a list of information about current users by entering the finger command with no options:   

finger 

For a list of usernames currently logged in, presented in a condensed, single-line format, enter:  

users

For more information, including each login of a username, the terminal users are attached to, the date and time that login began, and possibly the computer from which they are making the connection, enter:  

who

For extensive information, including username, terminal, IP number of the source computer, the time the login began, any idle time, process CPU cycles, job CPU cycles, and the currently running command, enter:

w

Applying binary logs without adding to the binary log

Applying binary logs without adding to the binary log

Applying binary logs to a MySQL instance is not particularly difficult, using the mysqlbinlog command line utility:
1
2
$> mysqlbinlog mysql-bin.000003 > 03.sql
$> mysql < 03.sql
Turning off binary logging for a session is not difficult, from the MySQL commandline, if you authenticate as a user with the SUPER privilege:
1
mysql> SET SESSION sql_log_bin=0;
However, sometimes you want to apply binary logs to a MySQL instance, without having those changes applied to the binary logs themselves. One option is to restart the server binary logging disabled, and after the load is finished, restart the server with binary logging re-enabled. This is not always possible nor desirable, so there’s a better way, that works in at least versions 4.1 and up:

The mysqlbinlog utility has the --disable-log-bin option. All the option does is add the SET SESSION sql_log_bin=0; statement to the beginning of the output, but it is certainly much better than restarting the server twice!

Here’s the manual page for the --disable-log-bin option of mysqlbinlog: http://dev.mysql.com/doc/refman/5.1/en/mysqlbinlog.html#option_mysqlbinlog_disable-log-bin

backup and point in time recovery with binary logs

MySQL backup and point in time recovery with binary logs

Backing up MySQL data is very important if you run MySQL on any kind of production server. We’ll explore some basic concepts which allows you to do that.
We’re interested in two types of backup: full and incremental.

Full backup can be saved as a collection of queries which can restore a database (logical), or raw copy of directories which contains MySQL data (physical). For large databases (>10GB), you’ll probably want to use the latter option. You can do that by using tools like mysqlhotcopy for MyISAM and InnoDB Hot Backup or Xtrabackup for InnoDB table. In this example we will make the logical full backup with mysqldump command.

Incremental backup can be done by backing up MySQL binary logs.
Let’s make sure that binary logs are enabled. Edit MySQL’s my.cnf config file and check for this lines:
log_bin                 = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
First option tells MySQL to enable binary logging and save the logs under /var/log/mysql/ directory. Second option means that all logs older that 10 days will be automatically deleted.


First, from the full backup:
root@test1:~# mysql -u root -p

mysql> create database bk_test;
Query OK, 1 row affected (0.00 sec)

root@test1:~# mysql -u root -p bk_test < full_backup.sql
This is the contents of the table now:
mysql> select * from bk_test_t1;
+----+------------+---------------------+
| id | test_field | time_created        |
+----+------------+---------------------+
|  1 | val1       | 2010-09-05 10:15:35 |
|  2 | val2       | 2010-09-05 10:15:35 |
|  3 | val3       | 2010-09-05 10:15:35 |
+----+------------+---------------------+
3 rows in set (0.00 sec)
Now we need to just apply the changes from the binary log. There is a tool called mysqlbinlog which outputs the contents of the logs in text format. Its result can be piped directly to the mysql command.
root@test1:~# mysqlbinlog mysql-bin.000002 | mysql -uroot -p bk_test
And voila! we have restored the original table:
mysql> select * from bk_test_t1;
+----+------------+---------------------+
| id | test_field | time_created        |
+----+------------+---------------------+
|  1 | val1       | 2010-09-05 10:15:35 |
|  2 | val2       | 2010-09-05 10:15:35 |
|  3 | val3       | 2010-09-05 10:15:35 |
|  4 | val4       | 2010-09-05 10:57:43 |
|  5 | val5       | 2010-09-05 10:57:43 |
|  6 | val6       | 2010-09-05 10:57:44 |
+----+------------+---------------------+
6 rows in set (0.00 sec)
Dates are correctly restored because bin log contains SET TIMESTAMP command to adjust the time before the concrete query.

But, binary logs are much more powerful than that. You can, for example, pipe the result from mysqlbinlog to a file, remove bad queries and restore it.

You can also limit the list of returned queries with start and end time. For example, to restore the database to the state as it was on 2010-09-05 10:55:44, you can use following command:
root@test1:~# mysqlbinlog --stop-datetime="2010-09-05 10:55:44" mysql-bin.000002 | mysql -uroot -p bk_test

To include multiple logs, just include them in command, one after another:
root@test1:~# mysqlbinlog mysql-bin.000002 mysql-bin.000003 | mysql -uroot -p bk_test

You can use –start-datetime and –stop-datetime to locate positions in binary logs, and then use that positions in –start-position and –stop-position params to limit returned queries.

It’s a good idea to save binary logs on some SAN storage, or rsync them periodically to another location, so if something goes wrong, you can first restore data from the last incremental backup, and then restore additional data from those new logs, so minimum amount of data is lost. You can also check sync_binlog mysql parameter, which controls when binary logs are synchronized to the disk.

Wednesday, January 2, 2013

Monitoring from the command line

Basic solutions (CLI)

Those solutions are run from the command line (CLI):

MySQL database and host monitoring solution

More advanced MySQL database and host monitoring solutions with graphs and/or history and/or hints are:
MySQL Performance Monitor The FromDual Performance Monitor for MySQL/MariaDB is a monitoring solution based on Zabbix. It is freely available. More information about it you can find here.
MySQL Enterprise Monitor The MySQL Enterprise Monitor (aka Merlin or MEM) serves as an automated assistant for MySQL database administrators. For MySQL customers only!
cmon CMON - the Cluster Monitor for MySQL Cluster. CMON is the most comprehensive monitor for MySQL Cluster and collect all information that is possible to collect from the data nodes and management servers.
MySQL Activity Report The MySQL Activity Report package is a tool to help MySQL database administrators to collect several database parameters and variables. These collected values can be used for server monitoring or performance tuning purposes.
MySQL Administrator Exterminated! Is replaced by the MySQL Workbench.
Kontrollbase Kontrollbase is a monitoring, analytics, reporting, and historical analysis webapp for MySQL database administrators and advanced users of MySQL databases. You can download it from here.
Zabbix Zabbix offers advanced monitoring, alerting and visualisation features.
Nagios Nagios is a powerful monitoring system that enables organizations to identify and resolve IT infrastructure problems before they affect critical business processes.
FromDual provides the following Nagios plugins for MySQL/MariaDB:
  • check_db
  • check_errorlog
  • check_repl_cnt_slave_hosts
  • check_repl_heartbeat
  • check_repl_io_thread
  • check_repl_sql_thread
  • check_repl_read_exec_pos
  • check_repl_seconds_behind_master
  • check_repl_readonly
  • perf
They can be downloaded from here. It is planned to make some more plugins for Nagios. Please make suggestions what you need next! PerfParse Add On for Nagios
Icinga Icinga (Nagios fork) is an enterprise grade open source monitoring system which keeps watch over a network and any conceivable network resource, notifies the user of errors and recoveries, and generates performance data for reporting.
Cacti Cacti is a complete network graphing solution designed to harness the power of RRDTool's data storage and graphing functionality. Cacti provides a fast poller, advanced graph templating, multiple data acquisition methods, and user management features out of the box.
MySQL monitoring with Cacti
MRTG MRTG will monitor SNMP network devices and draw pretty pictures showing how much traffic has passed through each interface. MRTG is being used to graph all sorts of network devices as well as everything else from weather data to vending machines.
RRD RRDtool is the OpenSource industry standard, high performance data logging and graphing system for time series data.
Munin Munin is a networked resource monitoring tool that can help analyse resource trends and "what just happened to kill our performance?" problems.
Moodds moodss is a graphical monitoring application. Seems to be stalled since 2006. MySQL modules
Hobbit Monitor Hobbit is a system for monitoring servers and networks. It takes its inspiration from the Big Brother monitoring system. Hobbit is the successor to the bbgen toolkit, which has been available as an add-on to Big Brother since late 2002.
Big Sister Big Sister originally was the Big Sister network monitor. Now, multiple projects in the realm of system management and monitoring are run under the label Big Sister.
Monit Monit is a free open source utility for managing and monitoring, processes, files, directories and file systems on a UNIX system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.
SmokePing SmokePing keeps track of your network latency.
ksysguard KDE System Guard allows you to monitor various statistics about your computer.
GKrellM GKrellM is a single process stack of system monitors which supports applying themes to match its appearance to your window manager, Gtk, or any other theme.
Ganglia Ganglia is a scalable distributed monitoring system for high-performance computing systems such as clusters and Grids.
Hyperic Hyperic monitors the health and well-being of operating systems, VMs, app servers, web servers, databases, messaging servers, authentication systems, and more.
Uptime Software A single tool for simplifying the management of Virtual, Physical and Cloud environments. Monitor services, monitor applications, monitor servers, and monitor platforms from a unified dashboard, even across multiple datacenters.
Zenoss Zenoss assures IT service delivery to applications, business services and real-time physical, virtual, and cloud-based infrastructures.
openNMS OpenNMS is the world’s first enterprise grade network management application platform developed under the open source model.
GroundWork Monitor GroundWork Monitor Enterprise Edition is the most scalable open source system and network management software for companies with heterogeneous operating systems, application and hardware environments.
Tivoli Monitoring Monitoring software to help improve the effectiveness and efficiency of your IT department.
HP OpenView
CA Unicenter Proactively manage the performance and availability of your infrastructure including physical and virtual systems, networks, databases and applications.
Big Brother Big Brother Professional Edition (BBPE) is a simple way to measure the health of your heterogeneous IT environment at-a-glance.
Shinken
MONyog MONyog MySQL Monitor and Advisor is a "MySQL DBA in a box" that helps MySQL DBAs manage more MySQL servers, tune their MySQL servers and fix problems with MySQL database applications.

MySQL quick poll for monitoring solutions

What monitoring software do you use? [Lit.]
Solution Votes %
none36540.240.2%
Nagios21123.323.3%
home grown solution859.49.4%
other788.68.6%
Big Brother384.24.2%
IBM Tivoli374.14.1%
HP OpenView313.43.4%
Mytop232.52.5%
CA-Unicenter151.71.7%
Ganglia141.51.5%
Moodss50.60.6%
Hyperic50.60.6%
A total of 907 votes have been cast in this poll so far. Poll opened January 24, 2005.

Linux Journal survey about Linux monitoring

What is your favourite Linux monitoring application?
Solution Votes %
Zabbix39433.233.2%
Nagios37331.431.4%
Uptime Software917.77.7%
other776.56.5%
Cacti625.25.2%
Hyperic524.433.2%
Zenoss514.34.4%
Ganglia413.53.5%
OpenNMS342.92.9%
Groundwork121.01.0%
A total of 1187 votes have been cast in this poll until April 21, 2010.