简体   繁体   中英

Does selecting from different database switches user?

In the following code I get output that tells me that current user is myuser@% and then second query will give me an error saying myuser@localhost was denied INSERT permission.

$query = "SELECT CURRENT_USER();"
$result = mysql_query($query);
if(mysql_num_rows($result))
{
    $data = mysql_fetch_assoc($result);
    echo(" current user - ". $data['CURRENT_USER()'] . "\n"); //current user - myuser@%
}


$query = "INSERT INTO DIFF_DATABASE.HISTORY (DATA) VALUES (\"" . $data. "\")";
if(mysql_query($query) == false)
{
    echo(mysql_error()); // INSERT command denied to user 'myuser'@'localhost' for table 'HISTORY'
}

It looks like there is a switch of MySQL user from myuser@% to myuser@localhost How does this happen? I'm guessing this is because I'm selecting from a different database in the second query.

The value of CURRENT_USER() can differ from the value of USER().

mysql> SELECT USER();
        -> 'davida@localhost'
mysql> SELECT * FROM mysql.user;
ERROR 1044: Access denied for user ''@'localhost' to
database 'mysql'
mysql> SELECT CURRENT_USER();
        -> '@localhost'

The example illustrates that although the client specified a user name of davida (as indicated by the value of the USER() function), the server authenticated the client using an anonymous user account (as seen by the empty user name part of the CURRENT_USER() value). One way this might occur is that there is no account listed in the grant tables for davida.

See http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_current-user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM