|
|
MySQL - Access Control
MySQL and SQL
(Continued from previous question...)
MySQL - Access Control
Once you establish a connection, the server enters Stage 2. For each request that comes in on the connection, the server
checks whether you have sufficient privileges to perform it, based on the type of operation you wish to perform. This is
where the privilege fields in the grant tables come into play. These privileges can come from any of the user, db, host,
tables_priv, or columns_priv tables. The grant tables are manipulated with GRANT and REVOKE commands.
The user table grants privileges that are assigned to you on a global basis and that apply no matter what the current
database is. For example, if the user table grants you the delete privilege, you can delete rows from any database on the
server host! In other words, user table privileges are superuser privileges. It is wise to grant privileges in the user
table only to superusers such as server or database administrators. For other users, you should leave the privileges in the
user table set to 'N' and grant privileges on a database-specific basis only, using the db and host tables.
The db and host tables grant database-specific privileges. Values in the scope fields may be specified as follows:
The wild-card characters `%' and `_' can be used in the Host and Db fields of either table.
A '%' Host value in the db table means ``any host.'' A blank Host value in the db table means ``consult the host table for
further information.''
A '%' or blank Host value in the host table means ``any host.''
A '%' or blank Db value in either table means ``any database.''
A blank User value in either table matches the anonymous user.
The db and host tables are read in and sorted when the server starts up (at the same time that it reads the user table). The
db table is sorted on the Host, Db, and User scope fields, and the host table is sorted on the Host and Db scope fields. As
with the user table, sorting puts the most-specific values first and least-specific values last, and when the server looks
for matching entries, it uses the first match that it finds.
The tables_priv and columns_priv tables grant table- and column-specific privileges. Values in the scope fields may be
specified as follows:
The wild-card characters `%' and `_' can be used in the Host field of either table.
A '%' or blank Host value in either table means ``any host.''
The Db, Table_name and Column_name fields cannot contain wild cards or be blank in either table.
The tables_priv and columns_priv tables are sorted on the Host, Db, and User fields. This is similar to db table sorting,
although the sorting is simpler because only the Host field may contain wild cards.
The request verification process is described below. (If you are familiar with the access-checking source code, you will
notice that the description here differs slightly from the algorithm used in the code. The description is equivalent to what
the code actually does; it differs only to make the explanation simpler.)
For administrative requests (shutdown, reload, etc.), the server checks only the user table entry, because that is the only
table that specifies administrative privileges. Access is granted if the entry allows the requested operation and denied
otherwise. For example, if you want to execute mysqladmin shutdown but your user table entry doesn't grant the shutdown
privilege to you, access is denied without even checking the db or host tables. (They contain no Shutdown_priv column, so
there is no need to do so.)
For database-related requests (insert, update, etc.), the server first checks the user's global (superuser) privileges by
looking in the user table entry. If the entry allows the requested operation, access is granted. If the global privileges in
the user table are insufficient, the server determines the user's database-specific privileges by checking the db and host
tables:
The server looks in the db table for a match on the Host, Db, and User fields. The Host and User fields are matched to the
connecting user's hostname and MySQL user name. The Db field is matched to the database the user wants to access. If there
is no entry for the Host and User, access is denied.
If there is a matching db table entry and its Host field is not blank, that entry defines the user's database-specific
privileges.
If the matching db table entry's Host field is blank, it signifies that the host table enumerates which hosts should be
allowed access to the database. In this case, a further lookup is done in the host table to find a match on the Host and Db
fields. If no host table entry matches, access is denied. If there is a match, the user's database-specific privileges are
computed as the intersection (not the union!) of the privileges in the db and host table entries, that is, the privileges
that are 'Y' in both entries. (This way you can grant general privileges in the db table entry and then selectively restrict
them on a host-by-host basis using the host table entries.)
After determining the database-specific privileges granted by the db and host table entries, the server adds them to the
global privileges granted by the user table. If the result allows the requested operation, access is granted. Otherwise, the
server checks the user's table and column privileges in the tables_priv and columns_priv tables and adds those to the user's
privileges. Access is allowed or denied based on the result.
Expressed in boolean terms, the preceding description of how a user's privileges are calculated may be summarized like this:
global privileges
OR (database privileges AND host privileges)
OR table privileges
OR column privileges
It may not be apparent why, if the global user entry privileges are initially found to be insufficient for the requested
operation, the server adds those privileges to the database-, table-, and column-specific privileges later. The reason is
that a request might require more than one type of privilege. For example, if you execute an INSERT ... SELECT statement,
you need both insert and select privileges. Your privileges might be such that the user table entry grants one privilege and
the db table entry grants the other. In this case, you have the necessary privileges to perform the request, but the server
cannot tell that from either table by itself; the privileges granted by the entries in both tables must be combined.
The host table can be used to maintain a list of secure servers.
At TcX, the host table contains a list of all machines on the local network. These are granted all privileges.
You can also use the host table to indicate hosts that are not secure. Suppose you have a machine public.your.domain that is
located in a public area that you do not consider secure. You can allow access to all hosts on your network except that
machine by using host table entries like this:
+--------------------+----+-
| Host | Db | ...
+--------------------+----+-
| public.your.domain | % | ... (all privileges set to 'N')
| %.your.domain | % | ... (all privileges set to 'Y')
+--------------------+----+-
Naturally, you should always test your entries in the grant tables (for example, using mysqlaccess) to make sure your access
privileges are actually set up the way you think they are.
(Continued on next question...)
Other Interview Questions
- What's MySQL
- What is DDL, DML and DCL?
- How do you get the number of rows affected by query?
- If the value in the column is repeatable, how do you find out the unique values?
- How do you return the a hundred books starting from 25th?
- You wrote a search engine that should retrieve 10 results at a time, but at the same time you’d like to know how many rows there’re total. How do you display that to the user?
- How would you write a query to select all teams that won either 2, 4, 6 or 8 games?
- How would you select all the users, whose phone number is null?
- What does this query mean: SELECT user_name, user_isp FROM users LEFT JOIN isps USING (user_id)
- How do you find out which auto increment was assigned on the last insert?
- What does –i-am-a-dummy flag to do when starting MySQL?
- On executing the DELETE statement I keep getting the error about foreign key constraint failing. What do I do?
- When would you use ORDER BY in DELETE statement?
- How can you see all indexes defined for a table?
- How would you change a column from VARCHAR(10) to VARCHAR(50)?
- How would you delete a column?
- How would you change a table to InnoDB?
- When you create a table, and then run SHOW CREATE TABLE on it, you occasionally get different results than what you typed in. What does MySQL modify in your newly created tables?
- How do I find out all databases starting with ‘tech’ to which I have access to?
- How do you concatenate strings in MySQL?
- How do you get a portion of a string?
- What’s the difference between CHAR_LENGTH and LENGTH?
- How do you convert a string to UTF-8?
- What do % and _ mean inside LIKE statement?
- What does + mean in REGEXP?
- How do you get the month from a timestamp?
- How do you offload the time/date handling to MySQL?
- How do you add three minutes to a date?
- What’s the difference between Unix timestamps and MySQL timestamps?
- How do you convert between Unix timestamps and MySQL timestamps?
- What are ENUMs used for in MySQL?
- How are ENUMs and SETs represented internally?
- How do you start and stop MySQL on Windows?
- How do you start MySQL on Linux?
- Explain the difference between mysql and mysqli interfaces in PHP?
- What’s the default port for MySQL Server?
- What does tee command do in MySQL?
- Can you save your connection settings to a conf file?
- How do you change a password for an existing user via mysqladmin?
- Use mysqldump to create a copy of the database?
- Have you ever used MySQL Administrator and MySQL Query Browser?
- What are some good ideas regarding user security in MySQL?
- Explain the difference between MyISAM Static and MyISAM Dynamic.
- What does myisamchk do?
- Explain advantages of InnoDB over MyISAM?
- Explain advantages of MyISAM over InnoDB?
- What are HEAP tables in MySQL?
- How do you control the max size of a HEAP table?
- What are CSV tables?
- Explain federated tables.
- What is SERIAL data type in MySQL?
- What happens when the column is set to AUTO INCREMENT and you reach the maximum value for that table?
- Explain the difference between BOOL, TINYINT and BIT.
- Explain the difference between FLOAT, DOUBLE and REAL.
- If you specify the data type as DECIMAL (5,2), what’s the range of values that can go in this table?
- What happens if a table has one column defined as TIMESTAMP?
- But what if you really want to store the timestamp data, such as the publication date of the article?
- Explain data type TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
- What does TIMESTAMP ON UPDATE CURRENT_TIMESTAMP data type do?
- Explain TIMESTAMP DEFAULT ‘2006:09:02 17:38:44? ON UPDATE CURRENT_TIMESTAMP.
- If I created a column with data type VARCHAR(3), what would I expect to see in MySQL table?
- General Information About MySQL
- The Main Features of MySQL
- Database Basics
- MySQL Command Interpreter
- Installing a MySQL Binary Distribution
- MySQL - Quick Installation Overview
- MySQL - MySQL Extensions to ANSI SQL92
- MySQL - Running MySQL in ANSI Mode
- Functionality Missing from MySQL - Sub-selects
- Functionality Missing from MySQL - SELECT INTO TABLE
- Functionality Missing from MySQL - Transactions
- Functionality Missing from MySQL - Stored Procedures and Triggers
- Functionality Missing from MySQL - Foreign Keys
- MySQL - Reasons NOT to Use Foreign Keys constraints
- Functionality Missing from MySQL - `--' as the Start of a Comment
- Functionality Missing from MySQL - How to Cope Without COMMIT/ROLLBACK
- MySQL - General Security
- How to Make MySQL Secure Against Crackers
- MySQL - Startup options to mysqld which concerns security
- MySQL - What the Privilege System Does
- MySQL - User Names and Passwords
- How to connecting to the MySQL Server
- MySQL - Keeping Your Password Secure
- Privileges Provided by MySQL
- MySQL - How the Privilege System Works
- MySQL - Access Control, Stage 1: Connection Verification
- MySQL - Access Control
- MySQL - When Privilege Changes Take Effect
- Setting Up the Initial MySQL Privileges
- Adding New User Privileges to MySQL
- MySQL - Setting Up Passwords
- MySQL - Causes of Access denied Errors
- Replication in MySQL
- MySQL - Replication Implementation Overview
- MySQL - how to set up complete replication on your current MySQL server
- MySQL - Replication Features and known problems
- MySQL - SQL Commands Related to Replication
- MySQL - Why do I sometimes see more than one Binlog_Dump thread on the master after I have restarted the slave?
- MySQL - How do I rotate replication logs?
- MySQL - How do I upgrade on a hot replication setup?
- MySQL - What issues should I be aware of when setting up two-way replication?
- MySQL - How can I use replication to improve performance of my system?
- MySQL - What should I do to prepare my client code to use performance-enhancing replication?
- MySQL - When and how much can MySQL replication improve the performance of my system?
- MySQL - How can I use replication to provide redundancy/high availability?
- MySQL - Troubleshooting Replication
- How to get Maximum Performance from MySQL
- MySQL - Optimization Overview
- MySQL - System/Compile Time and Startup Parameter Tuning
- MySQL - Disk Issues
- MySQL - Using Symbolic Links for Databases and Tables
- MySQL - Tuning Server Parameters
- How MySQL Opens and Closes Tables
- MySQL - Drawbacks to Creating Large Numbers of Tables in the Same Database
- MySQL - Why So Many Open tables?
- How MySQL Uses Memory
- How MySQL Locks Tables
- MySQL - Table Locking Issues
- How MySQL uses DNS
- MySQL - Get Your Data as Small as Possible
- How MySQL Uses Indexes
- MySQL - Speed of Queries that Access or Update Data
- MySQL - Estimating Query Performance
- MySQL - Speed of SELECT Queries
- How MySQL Optimizes WHERE Clauses
- How MySQL Optimizes DISTINCT
- How MySQL Optimizes LEFT JOIN and RIGHT JOIN
- How MySQL Optimizes LIMIT
- MySQL - Speed of INSERT Queries
- MySQL - Speed of UPDATE Queries
- MySQL - Speed of DELETE Queries
- MySQL - Other Optimization Tips
- MySQL - Using Your Own Benchmarks
- How MySQL Stores Its Row Data and Index Data?
- MySQL is Portability
- What Have We Used MySQL For?
- What is the difference between mysql_fetch_array and mysql_fetch_object?
- What are the different table present in MYsql?
|