List of specific database tables that a user has access - mysql

I am beginner in mysql management, so I need help in writing query for listing specific database tables that a user has access.

This is a strange wish...
Normally users should not be interested in tables. Tables are a technical aspect of the data model, something that should not be visible to the user. The user is interested in storing and retrieving data. But he usually is and should not be aware of how that data is stored.
Anyways: there is no such function AFAIK. you will have to create some hack yourself. You can query the list of tables inside a database. Then you'd have to iterate over that list and test for access rights one by one.

Have a look at mysql information_schema tables.
19.14 The INFORMATION_SCHEMA TABLE_PRIVILEGES Table
The TABLE_PRIVILEGES table provides information about table
privileges. This information comes from the mysql.tables_priv grant
table.
http://dev.mysql.com/doc/refman/5.0/en/table-privileges-table.html
and
6.2.2 Privilege System Grant Tables
Normally, you manipulate the contents of the grant tables in the mysql
database indirectly by using statements such as GRANT and REVOKE to
set up accounts and control the privileges available to each one. See
Section 13.7.1, “Account Management Statements”. The discussion here
describes the underlying structure of the grant tables and how the
server uses their contents when interacting with clients.
http://dev.mysql.com/doc/refman/5.1/en/grant-table-structure.html

Related

Navicat for MySQL. How to let ordinary user login don't display information_schema?

How can I prevent an ordinary (unprivileged) user, when logged in to MySQL from displaying information_schema?
Anybody can see it.
The information_schema database cannot, itself, be hidden.
As you may know, the information_schema tables are not actually tables with data -- information_schema is an SQL interface for observing data structures in the server, and as such, access to information_schema is essentially harmless, because a user cannot use information_schema to access information the user isn't otherwise able to access.
The information_schema tables are subject to the same security as the objects they describe, so their contents appear to vary based on the permissions of the currently logged-in user.
For example, if a user does not have access to a database called "production" then the information_schema tables table and columns table will not expose those tables and their columns to that user.

Granting MySQL permissions to all but one table

I have a database that is used to store data for various applications. I want to grant users permissions to select data from these tables, however there is one table that has sensitive information that only one specific 'special' user should have access to.
How do i grant permissions to all tables EXCEPT that one table. If i grant all, then revoke on that table, it won't work as it says that there is no permission on that table.
I thought about writing a query that could do this, but what happens when tables are added to the database? I would have to go and add that permission to each of the users, which could get quite time consuming. Is there any way to do this easily in MySQL? Perhaps a trigger that on table creation grants permissions? Is that possible?
Put that table in a separate database. Then you can grant access to regulardb.* to all the normal users, and grant access to sensitivedb.specialtable only to the special username.

block access to a table from any user except one

I have a database with many many tables.
I want to block any users, except one, from accessing this table. All the users will also need to be able to access any future tables created in such database, and naturally it is not possible for me to add/remove access to those tables as some are temporary tables created just scripting purpose.
Something like:
GRANT ALL PRIV ON *.* TO user123 ...
REVOKE ALL PRIV ON mydb.table FROM user123
If some tables are temporary, why not just create them as TEMPORARY tables? Then they will only be accessible by the session which created them.
You can easily grant permissions on tables and then revoke for an individual table, but there's no way to easily maintain that going forward.
Another option is to simply put the table which needs different permissions in a separate database. Of course it managing permissions for two databases, so it could be messy, but it's somewhat closer to the goal.
There's a good reference guide to table privileges here. Think it has what you need. Other privileges are mentioned earlier in the same guide if you need those too.

mysql, permissions for different users to access different tables

I would like to understand how hard this is to implement.
In unix, there are unix groups where certain people with a group can access certain folders and files.
I would like to apply the same concept into MYSQL where people could only access, view certain tables or even same tables but different rows ...
How can I achieve this? Would I have to use a different database system?
Gordon
This is a very common and simple approach. You can create users and specify which databases/tables they can access and what type of operations they can execute. See the mysql documentation on this
For instance:
--create the user
CREATE USER 'gordon'#'localhost' IDENTIFIED BY 'yourpassword';
--specify table and specific operations for that user
GRANT SELECT,UPDATE,DELETE,INSERT ON database.table TO 'gordon'#'localhost';

List of tables that a user has SELECT privilege for in MySQL

Short version: How can I write an SQL procedure to list which of several tables in a MySQL database a particular user has access to?
Longer version:
I'm writing a multi-user app that accesses a database with data for several branches of a company. The database has a number of lookup tables that any user can access, and a table for each branch that only authorized users can access. My strategy is:
Write a stored procecure that returns a list of the relevant tables for which the user has SELECT privilege.
From the app, call the procedure. If there's only one table returned, use it, otherwise let the user select which branch they want to access (e.g. for managers).
I'm having trouble figuring out how to write such a stored procedure. SHOW GRANTS FOR CURRENT_USER is an obvious possibility, but parsing something like:
GRANT SELECT ON Company.BranchABC TO 'auser'#'%clientdomain.com'
in SQL to figure out what the tables are seems way too messy. Doing a SELECT from the actual tables that hold the permissions also seems problematic, because I'd have to duplicate MySQL's logic for combining the permissions from the various tables (user, db, host, etc.)
Any words of wisdom?
You can see what privileges on which tables a user has:
show grants for 'user'#'host';
For example, to see the privileges of user1 (all machines in the network 10.25), run:
show grants for 'user'#'10.25.%.%';
I have never granted per table permissions to MySQL users before, but to do this, you would check that the TABLE_PRIVILEGES table in the information_schema database.
That should point you in the right direction.
MySQL users list and its privilege can be check with the Query.
select * from mysql.user\G;
http://www.thedevheaven.com/2012/04/retrieve-mysql-users-list-and-its.html