Skip to content

# Month: September 2011

## Three wishes for a new year

MySQL SQL September 27, 2011

It’s another new year by Jewish calendar. And what do I wish for in the following year?

1. World peace
2. Good health to all
3. Have some way to turn SHOW commands into SELECT statements, server side. I’m fervently trying to hack around this. Stored routines, export/import from file, text manipulation, I don’t care! I want to SELECT seconds_behind_master somehow. Without plugins.

PS, none of my last year’s wishes came true. I’ll settle for two out of three.

## common_schema rev. 68: eval(), processlist_grantees, candidate_keys, easter_day()

MySQL common_schema Indexing Open Source Security September 6, 2011

Revision 68 of common_schema is out, and includes some interesting features:

- eval(): Evaluates the queries generated by a given query
- match_grantee(): Match an existing account based on user+host
- processlist_grantees: Assigning of GRANTEEs for connected processes
- candidate_keys: Listing of prioritized candidate keys: keys which are UNIQUE, by order of best-use.
- easter_day(): Returns DATE of easter day in given DATETIME’s year.

Let’s take a slightly closer look at these:

#### eval()

I’ve dedicated this blog post on MySQL eval() to describe it. In simple summary: eval() takes a query which generates queries (most common use queries on INFORMATION_SCHEMA) and auto-evaluates (executes) those queries. Read more

#### match_grantee()

As presented in Finding CURRENT_USER for any user, I’ve developed the algorithm to match a connected user+host details (as presented with PROCESSLIST) with the grantee tables (i.e. the mysql.user table), in a manner which simulates the MySQL server account matching algorithm.

This is now available as a stored function: given a user+host, the function returns with the best matched grantee. Read more

#### processlist_grantees

This view relies on the above, and maps the entire PROCESSLIST onto GRANTEEs. The view maps each process onto the GRANTEE (MySQL account) which is the owner of that process. Surprisingly, MySQL does not provide one with such information. Continue reading » “common_schema rev. 68: eval(), processlist_grantees, candidate_keys, easter_day()”

## MySQL eval()

MySQL common_schema SQL Stored routines September 6, 2011

I’ve just implemented an eval() call for MySQL. It is implemented with SQL, using a stored procedure. So this is not some plugin: you can use it from within your normal database server.

#### Just what is an eval() call?

In some programming languages it would mean: get some text, and execute it as though it were complied code. So, dynamic coding.

In SQL: get the text of query which generates SQL statements in itself (either DML or DDL), and invoke those implied SQL statements.

#### A simple example

Best if I present Mass killing of MySQL Connections by Peter Zaitsev. The thing is to execute a query, typically on INFORMATION_SCHEMA, which uses metadata so as to generate SQL queries/commands. Peter’s example is:

>     select concat('KILL ',id,';') from information_schema.processlist where user='root'

The above query generates KILL commands for all users called ‘root’. I do many such queries in common_schema: like creating the GRANT statements for accounts, the DROP KEY statements for redundant keys, the ADD and DROP statements for foreign keys etc.

So the problem is you have to export those statements to file, then execute them from file: either using SOURCE, as in Peter’s example, or from shell prompt, piping file contents into mysql client.

#### You can now eval()

I’ve been on family holiday for a couple of weeks, which meant no need to think of work. Which means more time to think of SQL (darn!). And I’ve found the way to do it completely within the server (no external files required). Continue reading » “MySQL eval()”

## You know you’ve been doing too much SQL when…

MySQL Humor SQL September 1, 2011

…you’re traveling on family holiday, and each time you wish to suggest you should check up on the tourist information center, you repeatedly come up with: “Let’s ask at the INFORMATION_SCHEMA and see what we can get”.

Also, doing so while wearing an OpenSQL Camp T-shirt doesn’t help.