MySql: Hide certain tables from certain users - mysql

I want to hide certain tables (some definition tables) in the database from certain users. There is this mysql.tables_priv table which is empty. Should I insert something in that table to make it happen and what should be the value of 'table_priv' column?

You should be looking into the SQL GRANT command. With GRANT, you can assign privileges to users like this:
GRANT SELECT ON table TO user;

If the tables_priv is empty, i believe it means no privileges has been granted for that database table. You can do a quick test and grant select on database.table_name to user
and see if a row appears in that table. But normally your grants on tables appear in that table.

Related

MySql: Restrict update permission on one column in one table

I have a table, lets call it student, in a schema called enrollment. Table student has a column called address that I don't want a certain user to update (other permissions are fine such as select, insert). All other columns in that table AND in that schema should have the update privilege.
Is this doable?
You can set privileges on database / table / column. But I really would not try to use MySQL's privilege mechanism at that level. I would instead write application code to decide who can see/change what. This is more flexible in the long run. And more graceful to the user -- instead of getting a cryptic MySQL error message about permissions, the UI would simply not show what should not be shown. For updating, the UI would not even give the user the option.
In my case, I wanted a specific application to be able to update only 1 field (my_field) in only 1 table (table_name) while being able to read the entire database.
I created a special user for that purpose:
CREATE USER 'restrictedUser'#'%' IDENTIFIED BY 'PASSWORD_HERE';
SET PASSWORD FOR 'restrictedUser'#'%' = PASSWORD('PASSWORD_HERE');
GRANT USAGE ON *.* TO 'restrictedUser'#'%';
GRANT SELECT ON DATABASE_NAME.* TO 'restrictedUser'#'%';
GRANT UPDATE (my_field) ON DATABASE_NAME.table_name TO 'restrictedUser'#'%';
Documentation for Column privilege can be found here for mariaDb and here for mysql

How to hide column from user in MySQL table

I figured that this would be easy, but apparently (and to my frustration) it is not.
I have a user. We will say the user's name is 'user'. I simply want this user to NOT be able to see a column in my MySQL database.
I am using HeidiSQL. There seems to be no way to use the GUI to disallow users to see a column in a table. So I assumed that the following would work;
GRANT SELECT ON database_name.user TO 'user'#'%';
GRANT SELECT (column_name) ON database_name.table_name TO 'user'#'%';
REVOKE SELECT (column_name) ON database_name.table_name FROM 'user'#'%';
But it doesn't. Whenever I flush privileges and log in through the user, I still see the column that I do not want the user to see.
What is the algorithm for this, exactly? I'd like to assume this is possible.
Thanks in advance,
-Anthony
We can grant/revoke privileges at the column level as MySQL stores column privileges in the mysql.columns_priv table, and should be applied for single column in a table.
GRANT SELECT (col1), INSERT (col1,col2) ON dbname.tblname TO 'user'#'hostname';

Mysql temporary table for different users

Is there any table type that can only be accessed by a particular user?
This table can only be viewed and accessed only by the user who created it
Yes you can.
But you can create a table that has user created column so you can use it on your where condition.
I think the answer is Yes
You can set privileges for that particular table like who can access that table. Like,
SELECT, INSERT, UPDATE, DELETE, .
I have tried in via phpmyadmin.
I was wrong. Yes you can set user specific access for particular table. Its syntax is as follow
GRANT SELECT ON db2.invoice TO 'jeffrey'#'localhost';
Temporary tables are available under particular session and not accessible by any other session. It will be dropped on session close.
For more information read http://dev.mysql.com/doc/refman/5.5/en/grant.html

How can I restrict a MySQL user to a particular tables

How can I restrict a user_account in MySQL database to a particular tables. Ex:
UserName: RestrictedUser
DatabaseName: db_Payroll
TableName:
tb_Employees
tb_Users
tb_Payroll_YYMMDD
tb_Payroll_Processed
I want to restrict "RestrictedUser" to tb_Users and tb_Employees only and the rest of the tables of db_Payroll that will be created for future use is granted to have access.
Assuming the user has no current privileges, you can do the following
GRANT SELECT, INSERT, DELETE ON db_Payroll.tb_Users TO RestrictedUser#'%'
GRANT SELECT, INSERT, DELETE ON db_Payroll.tb_Employees TO RestrictedUser#'%'
Depending on exactly which privileges you wish to grant the user, you can change SELECT, INSERT, DELETE to something else, e.g. ALL PRIVILEGES.
Afterwards, remember to flush the privileges so they become effective by running
FLUSH PRIVILEGES;
You can grant access to individual tables by running:
GRANT ALL ON db_Payroll.tb_Users to RestrictedUser#RestrictedHostName;
And similarly for other tables. Use a list of operations instead of ALL if appropriate.
You cannot grant access to individual tables which do not exist yet without granting access to all tables.
Assuming the user has no current privileges, if you have a lot of tables and you only want to give the user access to a few of those tables, the simplest work-around I know of is using a technique I personally refer to as QueryCeption™ (Query Within a Query):
SELECT GROUP_CONCAT(CONCAT('grant select on `db_Payroll`.', table_name, ' to `RestrictedUser`#`%`') SEPARATOR ';
') from information_schema.tables where table_schema = 'db_Payroll' and
table_name not in ('TABLE-YOU-WANT-TO-RESTRICT-1', 'TABLE-YOU-WANT-TO-RESTRICT-2','TABLE-YOU-WANT-TO-RESTRICT-3');
This will output a text field that you can copy and paste into your editor. This particular example will grant SELECT privileges to all tables that are not within the restricted table array for that user.

MySQL: How do I lock individual rows in a table?

To increase the security of the app I'm working on, I want to be able to protect against any sort of db modification. I know I can lock entire tables from mysql users, but what about individual rows?
Would I still be able to add new rows to the table? maybe for just one particular table, only the SELECT and INSERT commands are allowed?
It sounds like you want to permit inserting new rows and querying existing rows, but you do not want to permit updating or deleting rows. If that is correct, then you'll want to create a MySQL user that has only INSERT and SELECT privileges on the table(s) in question. Do not grant UPDATE and DELETE privileges.
To grant INSERT and SELECT privileges to user foo on my_table:
GRANT SELECT, INSERT ON my_table TO 'foo'#'localhost';
To revoke UPDATE and DELETE privileges from user foo on my_table:
REVOKE UPDATE, DELETE ON my_table FROM 'foo'#'localhost';
This would be "Row level security". MySQL doesn't have it, so you'd need to implement yourself.
For example, an "AddedBy" column can be used to restrict data changes to other members in the same group. Of course, if the Addedby user changes group you have to track this
To restrict allow INSERT and SELECT only, just GRANT these permissions.
Otherwise, please add more use cases
You could use a specific database users for your application with limited rights (No INSERT, DELETE) for the desired tables.