When a client attempts to connect, the server matches the sorted records to the client using the Host values first and the User values second:
When you attempt to determine which grant table record the server will find as the best match for a client, remember to take the sort order into account. In particular, the fact that Host matching is done before User matching leads to a property that might be surprising unless you're aware of it. Consider again the case where james connects from the local host. There are two entries with james in the User column, but neither is the first match. Host matching takes place first, so on that basis the entry that matches first is the anonymous-user entry: localhost matches the host from which james connects, and the blank User value matches any username. This means that when james connects from the local host, he will be treated as an anonymous user, not as james. When you connect successfully to the server, the USER() function returns the username you specified and the client host from which you connected. The CURRENT_USER() function returns the username and hostname values from the User and Host columns of the user table record the server used to authenticate you. The two values may be different. If james connects from the local host, USER() and CURRENT_USER() have these values: mysql> SELECT USER(), CURRENT_USER(); +-----------------+----------------+ | USER() | CURRENT_USER() | +-----------------+----------------+ | james@localhost | @localhost | +-----------------+----------------+ The username part of CURRENT_USER() is empty. This occurs because the server authenticates james as an anonymous user. If james connects from pluto.example.com instead, USER() and CURRENT_USER() have these values: mysql> SELECT USER(), CURRENT_USER(); +-------------------------+----------------+ | USER() | CURRENT_USER() | +-------------------------+----------------+ | james@pluto.example.com | james@% | +-------------------------+----------------+ Here the host part of CURRENT_USER() is % because the server authenticates james using the user table entry that has % as the Host value. For connection attempts that the server denies, an error message results:
blog comments powered by Disqus |