Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Query to count how many databases a user owns?

Tags:

mysql

Is there a query like

pseudo-code:

SELECT databases FROM mysql.databases WHERE owner = 'myUser'

or any query that'd do the job?

like image 821
Devrim Avatar asked Jun 03 '12 00:06

Devrim


People also ask

How can I tell how many MySQL databases I have?

Open the Command Prompt and navigate to the bin folder of your MySQL Server installation directory. Then connect to the server using the mysql -u root -p command. Enter the password and execute the SHOW DATABASES; command we have discussed above.

What is COUNT (*) in MySQL?

MySQL COUNT() Function The COUNT() function returns the number of records returned by a select query.

How do I find out how many users a database has?

We can see the currently logged user in the database server by using the following query in the MySQL server: mysql> SELECT user, host, db, command FROM information_schema.

How do I COUNT number of employees in MySQL?

SELECT COUNT(*) AS "Number of employees" FROM employees WHERE salary > 75000; In this COUNT function example, we've aliased the COUNT(*) expression as "Number of employees". As a result, "Number of employees" will display as the field name when the result set is returned.


3 Answers

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.

like image 150
Eugen Rieck Avatar answered Oct 25 '22 14:10

Eugen Rieck


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)

like image 27
mjhennig Avatar answered Oct 25 '22 16:10

mjhennig


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'; 
like image 37
Andy Jones Avatar answered Oct 25 '22 16:10

Andy Jones