Does User Privileges affects the execution time of my mysql query - mysql

Suppose we have 2 users, A and B for a mysql DB customer.
User A is having all the privileges on a particular DB ,
and user B is having some complex specific privileges like
GRANT SELECT,INSERT
-> ON customer.Table1
-> TO 'user B'#'server.domain'
-> IDENTIFIED BY 'pwrd';
Now if I run a query on customer DB on Table1 table, then will there be any difference in the execution times of the query when run separately each through User A and User B ?
And how does the Privileges are actually checked at the time of the query execution or they are checked at the time of connection building itself and stored some where else?
what I know is that the privileges are stored in table named as 'user (Host,User,Password)' .

Permissions are checked for the login user at compile time of sql statement just before executing a sql statement.
The permissions are checked for only the resources e.g. table, views, stored procedure, functions used in that particular sql statement.
User's Priviledges or user level does not affect the execution time.
When a user tries to connect to the database, MySQL checks that that particular username/host/password combination has permission to connect. Once the connection has been made, before any operations are carried out, MySQL again checks to see whether the user/host combination has the right level of access to carry out that operation. The user table is the first table MySQL checks. All user/host/password combinations must be listed in this table before any access can be granted.

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.

Prevent users from removing MySQL triggers set by different user

Im currently setting up a database system with a lot of different users, having access to only limited views, and tables of the database system.
Now I need to create several triggers as the root user, to prevent some actions. But all the users should be able to create a trigger for a database created for them and the tables within. This is working fine since every user has database-specific privileges. Sadly this does allow for some reason the user to delete triggers set by the root user on their database.
If I have database 'A' with table 'test'. I create a trigger as root user for database 'A' table 'test'. Now user 'someone' has privileges to create triggers for database 'A', but he should NOT be able to remove any trigger set by the root account on database 'A'. Sadly he can remove triggers created by root... anyone know how to fix this for MySQL?
Here is the privileges for the user for the specific database:
Now the query executed by the root user:
Result in with SHOW TRIGGERS executed by user 'someone' on database 'A':
Execution of DROP TRIGGER by user 'someone' on database 'A':
Why can the user remove this trigger? It's not created by him but root... Also for anyone asking, the query 'SELECT CURRENT_USER();' returns 'someone#localhost' and NOT 'root#localhost', i have activly switched accounts.
If you grant a user the TRIGGER privilege to create triggers on a given table, you grant them all the operations that privilege covers, which includes both create and drop
https://dev.mysql.com/doc/refman/8.0/en/drop-trigger.html says:
DROP TRIGGER requires the TRIGGER privilege for the table associated with the trigger.
It doesn't matter who defined the trigger. MySQL generally has no concept of ownership for database objects like tables or triggers.
You are going to have to think of a different design that does not require users to be disallowed this access.

SELECTing a view shows data for the root user, but not for a different user (who has SELECT privileges)

Even though SELECT privileges are granted to our testuser account for databaseName.ViewName, SELECT * FROM ViewName returns all the column names, but 0 rows of data.
But when selecting the same view in SQLyog when logged in as root, all the data rows are shown.
We tried extracting the SELECT statement from the view and running it directly as testuser, but this resulted in the same empty table.
Is there another privilege setting we should verify? Why would we be able to SELECT the column names but not the data rows?
I am a low-tier employee and don't have immediate access to db privilege settings, but I will try to get them as fast as I can if requested. Thank you in advance!
EDIT: SELECT COUNT(*) FROM ViewName as testuser returns 0
You probably defined the view to run in the name of the user invoking it, which means the user invoking it must also have at least select privilege to the columns referenced by the view:
When a view has been referenced, privileges for objects accessed by the view are checked against the privileges held by the view DEFINER account or invoker, depending on whether the SQL SECURITY characteristic is DEFINER or INVOKER, respectively.
If you do not want the referencing user to have direct access to the underlying table, then you need to provide a user in whose name the query runs at view create time using the definer clause.

MySQL unable to import SQL that creates procedures

I am using a hosted web service account that uses cpanel as its management system. When logged into phpmyadmin, I am trying to import an SQL file that contains tables and some procedures.
CREATE DEFINER=`root`#`localhost` PROCEDURE `getClientDashboardStatsMap` (IN `in_userID` INT) BEGIN
SELECT
rl.city,
rl.state,
rl.zip,
rl.longitude,
rl.latitude,
rl.timestamp,
count(rl.ID) as total
FROM
crowd.redemption_log as rl
JOIN
reward as r
ON
rl.rewardID = r.rewardID
WHERE
r.userID = 1
AND
rl.timestamp BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
GROUP BY
rl.city, rl.state, rl.zip
ORDER BY
total DESC;
END$$
When I try to run this, I get an error about needing super user privileges to do so. Doing some searching, people suggested removing the definer line like so:
CREATE PROCEDURE getClientDashboardStatsMap (IN in_userID INT) BEGIN.
While this allows me to complete the import, I am running into another issue. The procedures are getting a default definer of cpaneluser#localhost. However, the database user that is set up is different than this user so the procedure has no permissions for things like select, update, delete. The database user is cpaneluser_dbusername, which is typical for hosted sites like this so you can associate databases with accounts.
How else can I get this procedures to run, under the correct user? I don't see any settings in PHPMYADMIN for privileges in order to run these as a super user.
There are two ways to solve this problem:
Log into phpmyadmin with the database user cpaneluser_dbusername. I am not familar with phpmyadmin and cpanel, so I'm not sure whether they provide you the option to change the user. You should check it out by yourself. I'm using MySQL Workbench and MySQL Administrator, they both privode me this option.
Grant privileges to cpaneluser#localhost. Such as:
GRANT ALL ON db_name.table_name TO 'cpaneluser#localhost';
It's better if you grant each privilege explicitly, e.g.: GRANT SELECT,INSERT,UPDATE,DELETE ON .... Check GRANT Syntax.

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