Sunday, 3 March 2013

Mysql Information Schema

Re-publishing from my wiki.
  • Show tables that use Barracuda disk format
select * from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA NOT IN ('mysql', 'INFORMATION_SCHEMA', 'performance_schema') AND ( ROW_FORMAT='Compressed' OR ROW_FORMAT='Dynamic'); 

  • Show me all tables that are InnoDB 
SELECT `table_schema`, `table_name` FROM `information_schema`.`TABLES` WHERE `Engine`='Innodb' AND `TABLE_SCHEMA` !='information_schema' AND `TABLE_SCHEMA` !='mysql'; 

  • Show me all tables that are MyISAM 
SELECT `table_schema`, `table_name` FROM `information_schema`.`TABLES` WHERE `Engine`='MyISAM' AND `TABLE_SCHEMA` !='information_schema' AND `TABLE_SCHEMA` !='mysql'; 

  • Print Queries to aid in conversion FROM MyISAM to InnoDB 
 use `information_schema`; SELECT CONCAT("ALTER TABLE `" , `TABLE_SCHEMA`, "`.`", `table_name`, "` Engine=Innodb;") AS "" FROM `information_schema`.`TABLES` WHERE `Engine`='MyISAM' AND `TABLE_SCHEMA` !='information_schema' AND `TABLE_SCHEMA` !='mysql'; 

You can save the above in a file and run this
mysql --batch < input.sql > out.sql 

  • Show me a count of tables grouped by engine type 
SELECT `Engine`, count(*) as Total FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` !='information_schema' AND `TABLE_SCHEMA` !='mysql' GROUP BY `Engine`;

  • Show me the datasize and index size of all tables grouped by engine type
SELECT `Engine`, COUNT(ENGINE), sum(data_length)/(1024*1024*1024) as 'Datasize-GB', sum(index_length)/(1024*1024*1024) as 'Indexsize-GB' FROM `information_schema`.`TABLES` GROUP BY `Engine`; 

  • Show me the top 10 tables by size outside of information_schema and mysql 
SELECT TABLE_SCHEMA, TABLE_NAME,data_length/1024*1024 FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` !='information_schema' AND `TABLE_SCHEMA` !='mysql' ORDER BY `data_length` DESC LIMIT 10; 

  • Tables without indexes
USE `information_schema`; SELECT CONCAT(TABLES.table_schema,".",TABLES.table_name) as name, `TABLES`.`TABLE_TYPE`,`TABLE_ROWS` FROM `TABLES` LEFT JOIN `TABLE_CONSTRAINTS` ON `TABLES`.`table_schema` = `TABLE_CONSTRAINTS`.`table_schema` AND `TABLES`.`table_name` = `TABLE_CONSTRAINTS`.`table_name` AND `TABLE_CONSTRAINTS`.`constraint_type` = 'PRIMARY KEY' WHERE `TABLE_CONSTRAINTS`.`constraint_name` IS NULL; Check for redundant indexes SELECT * FROM ( SELECT `TABLE_SCHEMA`, `TABLE_NAME`, `INDEX_NAME`, GROUP_CONCAT(`COLUMN_NAME` ORDER BY `SEQ_IN_INDEX`) AS columns FROM `information_schema`.`STATISTICS` WHERE `TABLE_SCHEMA` NOT IN ('mysql', 'INFORMATION_SCHEMA') AND NON_UNIQUE = 1 AND INDEX_TYPE='BTREE' GROUP BY `TABLE_SCHEMA`, `TABLE_NAME`, `INDEX_NAME` ) AS i1 INNER JOIN ( SELECT `TABLE_SCHEMA`, `TABLE_NAME`, `INDEX_NAME`, GROUP_CONCAT(`COLUMN_NAME` ORDER BY `SEQ_IN_INDEX`) AS columns FROM `information_schema`.`STATISTICS` WHERE INDEX_TYPE='BTREE' GROUP BY `TABLE_SCHEMA`, `TABLE_NAME`, `INDEX_NAME` ) AS i2 USING (`TABLE_SCHEMA`, `TABLE_NAME`) WHERE i1.columns != i2.columns AND LOCATE(CONCAT(i1.columns, ','), i2.columns) = 1 

  • List character sets 
 SELECT `TABLE_SCHEMA`, `TABLE_NAME`, `CHARACTER_SET_NAME`, `TABLE_COLLATION` FROM `INFORMATION_SCHEMA`.`TABLES` INNER JOIN `INFORMATION_SCHEMA`.`COLLATION_CHARACTER_SET_APPLICABILITY` ON (`TABLES`.`TABLE_COLLATION` = `COLLATION_CHARACTER_SET_APPLICABILITY`.`COLLATION_NAME`) WHERE `TABLES`.`TABLE_SCHEMA` !='information_schema' AND `TABLES`.`TABLE_SCHEMA` !='mysql' ;

  • List average row length and index length 
SELECT CONCAT (`TABLE_SCHEMA`, "." , `TABLE_NAME`) as name , `AVG_ROW_LENGTH`, `DATA_LENGTH`, `INDEX_LENGTH` FROM `TABLES` ORDER BY `AVG_ROW_LENGTH` DESC LIMIT 15; 

  • Oldest tables with respect to update times 
SELECT CONCAT (`TABLE_SCHEMA`, "." , `TABLE_NAME`) as name , `UPDATE_TIME` FROM `TABLES` WHERE `UPDATE_TIME` IS NOT NULL ORDER BY `UPDATE_TIME` LIMIT 10; 

  • Tables with foreign keys 
SELECT * FROM `table_constraints` WHERE `constraint_type` = 'FOREIGN KEY' ; 

  • List of indexes and their total count 
SELECT `INDEX_TYPE`, count(*) as NUM FROM `STATISTICS` group by `INFORMATION_SCHEMA`.`INDEX_TYPE`; 

  • Get a summary of privileges 
SELECT * from `INFORMATION_SCHEMA`.`USER_PRIVILEGES`;

Wednesday, 20 February 2013

Rackspace cloud files - symlinks / aliases



The requirement is to have multiple names to the same object. For starters, there is no inbuilt way to do aliases or multiple names to the same object. However, after some documentation trolling there is a way to achieve it, although it is not straightforward.

Cloud files offers large file (over 5G) support by allowing multiple segments to be uploaded and then a manifest that links the segments.

http://www.rackspace.com/blog/rackspace-cloud-files-now-supporting-extremely-large-file-sizes/

You can use this feature to sort of achieve symlinking/aliasing

Here's a small example

curl -D - \

     -H "X-Auth-Key: AUTH key" \

     -H "X-Auth-User: user" \

     https://lon.identity.api.rackspacecloud.com/v1.0



curl -X PUT -H 'X-Auth-Token: AUTH TOKEN' \

https://storage101.lon3.clouddrive.com/v1/<URL>/stream/imagedata1/1 --data-binary 'Image1'



curl -X PUT -H 'X-Auth-Token: AUTH TOKEN' \

-H 'X-Object-Manifest: sriramrajan.com/imagedata1/' \

https://storage101.lon3.clouddrive.com/v1/<URL>/stream/image1.txt --data-binary ''



curl -X PUT -H 'X-Auth-Token: AUTH TOKEN' \

-H 'X-Object-Manifest: sriramrajan.com/imagedata1/' \

https://storage101.lon3.clouddrive.com/v1/<URL>/stream/image2.txt --data-binary ''



This can be on CDN enabled containers as well and so once you do this, the following will be technically pointing to the same object.
<CDN URL>/image1.txt

<CDN URL>/image2.txt


Friday, 21 September 2012

MySQL Versions - A survey


Different versions

MySQL versions make interesting and at times confusing reading. Here's a list of the version soup.

  • Version 5.2 was re-branded as version 6.0.
  • Version 6.0 was then cancelled.
  • Version 5.4 then replaced version 6.0.
  • Version 5.4 was then re-branded as version 5.5.
  • MySQL NDB cluster comes with it's own versioning system. As of this writing 7.2 is the latest release.
  • Innodb, the most used engine in MySQL now has it's own versioning. MySQL 5.0 had Innodb version 1.0.xx and MySQL 5.5 has innodb version 1.1.xx.
  • MySQL 5.6 will be the next release from MySQL(Oracle). For a full feature list see http://dev.mysql.com/tech-resources/articles/whats-new-in-mysql-5.6.html

Forks and Patches

On top of this there are quite a few forks and patches.

  • Drizzle was a fork of MySQL 6.0. Drizzle is probably the only true fork, has fully re-factored code and, is in active development.
  • MariaDB is a release driven by Michael "Monty" Widenius, the original author of MySQL. MySQL 5.1 was the basis for MariaDB and as of this writing the latest version is Maria DB 5.5.
  • Percona has several patches for both MySQL 5.1 and MySQL 5.5. Percona patches are a re-base of the main release and XtraDB is a re-base of InnoDB. Both Percona Server and XtraDB is also not a true forks of MySQL or Innodb. They have some very good performance & monitoring enhancements.
  • Facebook also releases patches for MySQL.  These are done for specific Facebook requirements but as is often the case some of these patches eventually find their way into main releases.  These can be found at https://www.facebook.com/MySQLatFacebook
  • OurDelta is another set of patches released by former MySQL employee Arjen Lentz.  This is now more aligned with MariaDB. These patches are similar to the Percona ones in terms of adding extra functionality to existing releases.

3rd Party Storage Engines

Outside of InnoDB and MyISAM, there are few 3rd party plugins that suit specific needs as well.


Selecting the right one

  • For most part, staying with the Oracle releases will suit most application needs. These releases are in active development, generally stable and have binary versions for most systems.
  • MariaDB is binary compatible with MySQL and may suit certain needs. It also includes XtraDB from Percona. For a good comparison and the various incompatibilities, refer to http://kb.askmonty.org/en/mariadb-versus-mysql/
  • Going with a fork like Drizzle will likely require some application change. Drizzle does not maintain server level compatibility but does talk the MySQL protocol. For a full list of differences refer to http://docs.drizzle.org/mysql_differences.html
  • The Percona patches and versions provide a more seamless migration as they are generally fully compatible with the MySQL releases. If you are concerned about vendor support , Percona also provides that. Percona toolkit (formerly MaatKit) is a good addition as it provides some nice tools to automate a variety of tasks. This toolkit can be used with any MySQL release and so you don't need to run Percona server.
  • The 3rd party engines come into a play only if are looking for specific features that come with the engines.

Links & further reading

URL fun


Some interesting ways, URLs work


http://sriramrajan.com - The conventional one

http://46.38.167.114 - The IP addresss

http://774285170 - The decimal converted

http://0x2e.0x26.0xa7.0x72 - The hexadecimal converted

http://0x2e26a772 -  Another hexadecimal variant



Wednesday, 6 June 2012

IPv6 Tunnel

Get a free IPv6 IP from a broker like http://tunnelbroker.net/

Configure the tunnel
modprobe ipv6
ip tunnel add he-ipv6 mode sit remote  local  ttl 255
ip link set he-ipv6 up
ip addr add  dev he-ipv6
ip route add ::/0 dev he-ipv6
ip -f inet6 addr


Check with ifconfig
#ifconfig he-ipv6
he-ipv6   Link encap:IPv6-in-IPv4  
          inet6 addr: fe80::2e26:a772/128 Scope:Link
          inet6 addr: 2001:470:1f08:1a7e::2/64 Scope:Global
          UP POINTOPOINT RUNNING NOARP  MTU:1480  Metric:1
          RX packets:411 errors:0 dropped:0 overruns:0 frame:0
          TX packets:251 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:488490 (477.0 KiB)  TX bytes:22616 (22.0 KiB)

Test ping
# ping6 -c2 ipv6.google.com
PING ipv6.google.com(par03s02-in-x11.1e100.net) 56 data bytes
64 bytes from par03s02-in-x11.1e100.net: icmp_seq=0 ttl=57 time=10.9 ms
64 bytes from par03s02-in-x11.1e100.net: icmp_seq=1 ttl=57 time=10.0 ms
--- ipv6.google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 10.070/10.512/10.955/0.454 ms, pipe 2

Test website
# curl -6  -v http://sriramrajan.com/status.php
* About to connect() to sriramrajan.com port 80
*   Trying 2001:470:1f08:1a7e::2... connected
* Connected to sriramrajan.com (2001:470:1f08:1a7e::2) port 80
> GET /status.php HTTP/1.1
> User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
> Host: sriramrajan.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Wed, 06 Jun 2012 20:34:45 GMT
< Server: Apache
< Vary: Accept-Encoding
< P3P: policyref="http://www.sriramrajan.com/p3p.xml",CP= "NON DSP COR CURa TIA"
< Content-Length: 7
< Content-Type: text/html; charset=UTF-8
ALL OK
* Connection #0 to host sriramrajan.com left intact
* Closing connection #0

#curl -I  -v  -g -k  http://[2001:470:1f08:1a7e::2]/status.php
* About to connect() to 2001:470:1f08:1a7e::2 port 80 (#0)
*   Trying 2001:470:1f08:1a7e::2... connected
* Connected to 2001:470:1f08:1a7e::2 (2001:470:1f08:1a7e::2) port 80 (#0)
> HEAD /status.php HTTP/1.1
> User-Agent: curl/7.21.7 (i386-redhat-linux-gnu) libcurl/7.21.7 NSS/3.13.3.0 zlib/1.2.5 libidn/1.22 libssh2/1.2.7
> Host: [2001:470:1f08:1a7e::2]
> Accept: */*
> 
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Sat, 09 Jun 2012 10:35:09 GMT
Date: Sat, 09 Jun 2012 10:35:09 GMT
< Server: Apache
Server: Apache


Check SSH
# ssh -6 -p 2222 2001:470:1f08:1a7e::2
root@2001:470:1f08:1a7e::2's password: 

Firewall
Don't forget the firewall. If you use iptables for a firewall, make sure, you have equivalent setup for IPv6. You can check using
# ip6tables -nvL