Wednesday, October 12, 2011

TIL: Lookout For DEFINER

The Issue

I haven't blogged in a while an I have a long TODO list of things to publish: The repository for the SNMP Agent, video and slides of my OSCON talk and a quick overview of MHA master-master support. In the meantime, here's a little fact that I didn't know from MySQL CREATE VIEW documentation:

Although it is possible to create a view with a nonexistent DEFINER account, an error occurs when the view is referenced if the SQL SECURITY value is DEFINER but the definer account does not exist.
How can this be possible?

The Problem

For a number of reasons we don't have the same user accounts on the master than we have on the slaves (ie: developers shouldn't be querying the master). Our configuration files include the following line:
So if we create a user on the master, the user definition doesn't go through the replication chain.

So a VIEW can be created in the master, but unless we run all the proper GRANT statements on the slave as well, the VIEWs won't be effective on the slaves. Example from our slave (output formatted for clarity):

show create view view3\G
*************************** 1. row ***************************
                View: view3
               SQL SECURITY DEFINER VIEW `view3` AS select 

show grants for `app`@``;
ERROR 1141 (42000): There is no such grant defined 
for user 'app' on host ''

The Solution

Once again, Maatkit's to the rescue with mk-show-grants on the master:
mk-show-grants | grep
-- Grants for 'app'@''
GRANT USAGE ON *.* TO 'app'@'' 
IDENTIFIED BY PASSWORD '*password_hash';
SHOW VIEW, UPDATE ON `pay`.* TO 'app'@'';
A simple copy from the master and paste onto the slave fixed it.


Every now developers come to me with unusual questions. In this case it was: How come I can access only 2 out of 3 views?. In cases like these, it usually pays off to not overthink the issue and look into the details. A SHOW CREATE PROCEDURE on the 3 views quickly showed that one had a different host for the DEFINER. A quick read through the documentation and an easy test confirmed the mistake. That's why I have 3 mantras that I keep repeating to whomever wants to listen:
  • Keep it simple
  • Pay attention to details
  • RTFM (F is for fine)
It constantly keeps me from grabbing some shears and going into yak shaving mode.

1 comment:

  1. Genius! All my views were created with root@localhost DEFINER. I moved to a Plesk server that only had admin@localhost. Then your normal DB users can't access the views, not because they don't have permissions but because the DEFINER account didn't exist.