MySQL: remove a user's permission to delete specific tables - mysql

That is the question... I have 30 tables in a database, and I want to avoid deleting information in five tables.
With this I will also understand how to avoid inserting in three tables. Of course, I have created a new user and still don't have any privilege.
Can this mechanic be done?

Use REVOKE:
REVOKE DELETE ON contacts FROM 'user1'#'localhost';
Which would remove the DELETE permission on table contacts.
With mydb.*, for example, you can remove all DELETE permissions on all tables in the database mydb.

Related

Appropriate GRANT permissions only to list tables and structure in MySQL

I'm trying to create a MySQL user for my data catalog so it can extract metadata from my databases.
I don't want to grant DML permissions but only limit the user to query information_schema for databases, tables, columns and views.
I couldn't find an appropriate permission on the docs as it seems to require SELECT to be granted.
Any ideas?
https://dev.mysql.com/doc/refman/8.0/en/information-schema-introduction.html#information-schema-privileges says:
For most INFORMATION_SCHEMA tables, each MySQL user has the right to access them, but can see only the rows in the tables that correspond to objects for which the user has the proper access privileges.
https://dev.mysql.com/doc/refman/8.0/en/show-create-table.html says:
Shows the CREATE TABLE statement that creates the named table. To use this statement, you must have some privilege for the table.
MySQL doesn't have a distinct privilege for viewing metadata about a table without having some other DML privilege on that table.

How to remove user privileges associated with the dropped table?

When I was reading about the DROP TABLE statement in MySQL, I came across the term RESTRICT & CASCADE. On one website, click here to goto that link where I found information stating,
Note that the DROP TABLE statement only drops tables. It doesn’t remove specific user privileges associated with the tables. Therefore, if you create a table with the same name as the dropped one, MySQL will apply the existing privileges to the new table, which may pose a security risk.
It made me curious to know how one can remove user privileges associated with the dropped table?
Also, what are the use of RESTRICT & CASCADE keywords in the DROP TABLE statement?

MySQL - 'Drop User' vs 'delete from user'

I need to delete database access from some hosts. There are two options:
This option appears to be the best as I don't need to delete individual users
use mysql;
delete from user where host='myhost';
In second option below, I need to delete individual users.
drop user 'user1'#'myhost';
drop user 'user2'#'myhost';
drop user 'user3'#'myhost';
Any idea what is the difference between these options? Any pro and cons?
Thanks
When you use DROP USER Statement it removed one/more accounts + their accounts privileges.
When you use DELETE User it's just an SQL command which effects for table(s).
DROP is always more powerful than delete
Or You can use REVOKE to remove all permeation granted
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'user'#'myhost';
Privileges are
ALL PRIVILEGES – grants all privileges to the MySQL user
CREATE – allows the user to create databases and tables
DROP - allows the user to drop databases and tables
DELETE - allows the user to delete rows from specific MySQL table
INSERT - allows the user to insert rows into specific MySQL table
SELECT – allows the user to read the database
UPDATE - allows the user to update table rows
Read More about DROP USER Statement
Modifying the database tables requires a flush privileges call to refresh the cached data. Also, you have to make sure that you manually delete all user AND privileges (e.g., for tables, columns) manually.
Grant/drop user work immediately.
Cf. https://dev.mysql.com/doc/refman/8.0/en/privilege-changes.html

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.

How to prohibit the removal of any rows in a specific mysql table?

Is there a way to configure a mysql table so that writing and reading is possible but not deleting?
For example, a table that contains many logs that are legally important and that must never be deleted.
You would just grant the INSERT and SELECT privileges on the table in question (this prevents the possibility of a row being changed)
GRANT INSERT,SELECT ON mydb.mytable
TO secureduser#localhost
IDENTIFIED BY 'password';
From this you would go on to add wider permissions to the other tables in the database for this user.
Also, check out the Archive Storage Engine, which is custom designed for this kind of audit-trail application.
Revoke the delete privilege from all users for that table.
Another option is to use the Archive storage engine. It only allows insertion, no updates or deletes (from anyone - even a privileged account)
You could do as he says below or...
What you could do, is give users 'roles' in number form, then pass this number to a script which would remove the row... but if the passed number is below a certain 'minimum role expectation' then they are denied access to the script?