MySQL Query to count how many databases a user owns? MySQL Query to count how many databases a user owns? mysql mysql

MySQL Query to count how many databases a user owns?


SELECT COUNT(*) FROM information_schema.SCHEMATA;

(run as the user in Question)

SELECT count(*) FROM (  SELECT DISTINCT TABLE_SCHEMA FROM information_schema.SCHEMA_PRIVILEGES WHERE GRANTEE LIKE("'USERNAME'%") GROUP BY TABLE_SCHEMA) AS baseview;

(Run as root)

Caveat: There is no such thing as an "Owner" for a database in MySQL, the above queries will show information about the databases a user has soem sort of access to.


As far as I can tell, there is no concept in MySQL of "Owner" of database or its objects, as there is in MS Access and MS SQL Server. I surmise this from the lack of "owner" field anywhere in mysql system tables. (http://www.wideman-one.com/gw/tech/mysql/perms/index.htm)


You can count how many databases are associated with a user in the mysql database and the db table. This is the closest I can think of to "ownership" of a database by a user.

SELECT count(DISTINCT Db) FROM db WHERE User = 'someuser';