我用2台机器操作数据库,使用不同的帐号(root,test)登录数据库
create trigger insert_user before insert on question
for each row
set new.user = current_user();不同的帐号在插入数据时,出现的账户是同一个;第一次insert的用户是root则,使用test账户新增那个字段也是显示的root@localhost
重启服务,如果第一次insert的用户是test,使用 root新增user字段显示为test@%
为什么?怎么解决

解决方案 »

  1.   

    这个sql是跟你mysql客户端登陆的账号保持一致的。
    可能你2次登陆的不是同一个账号。你仔细检查下执行sql前登陆的账号。
    应该不会出现你描述的情况。
      

  2.   


    其实很简单,楼主在准备insert 的时候 看看select current_user() 这个是什么用户,那个新增字段就是什么用户了;
      

  3.   

    你的用户账号是什么?
    不会是你自己的应用程序中的用户账号吧。这里current_user()是MYSQL数据库的账号,你连接数据库时用的账号。
      

  4.   


    mysql> select current_user;
    +--------------+
    | current_user |
    +--------------+
    | wangw@%      |
    +--------------+
    1 row in setmysql> use question;
    Database changed
    mysql> insert into 问题(回复) values ('测试');
    Query OK, 1 row affectedmysql> select * from 问题;
    +------+----------+--------+----------+------+--------+----------+------+----------------+
    | 序号 | 问题类型 | 提出人 | 问题描叙 | 回复 | 记录人 | 登记时间 | 图片 | user           |
    +------+----------+--------+----------+------+--------+----------+------+----------------+
    |    9 | NULL     | NULL   | NULL     | 测试 | NULL   | NULL     | NULL | root@localhost |
    +------+----------+--------+----------+------+--------+----------+------+----------------+
    1 row in setmysql> 
      

  5.   

    贴一下你的SHOW CREATE TRIGGER insert_user  语句,你就知道问题在哪儿了。
      

  6.   

    MySQL takes the DEFINER user into account when checking trigger privileges, as follows: At CREATE TRIGGER time, the user who issues the statement must have the TRIGGER privilege. At trigger activation time, privileges are checked against the DEFINER user. This user must have these privileges: The TRIGGER privilege. The SELECT privilege for the subject table if references to table columns occur via OLD.col_name or NEW.col_name in the trigger definition. The UPDATE privilege for the subject table if table columns are targets of SET NEW.col_name = value assignments in the trigger definition. Whatever other privileges normally are required for the statements executed by the trigger. Within a trigger, the CURRENT_USER() function returns the account used to check privileges at trigger activation time. This is the DEFINER user, not the user whose actions caused the trigger to be activated. For information about user auditing within triggers, see Section 5.5.9, “Auditing MySQL Account Activity”. 
      

  7.   

    比如:
    DELIMITER $$DROP TRIGGER /*!50032 IF EXISTS */ `x`.`insert_user`$$CREATE
        /*!50017 DEFINER = 'root'@'localhost' */
        TRIGGER `insert_user` BEFORE INSERT ON `ttr` 
        FOR EACH ROW set new.name = current_user();
    $$DELIMITER ;这个是我的用root建立的触发器,
    把红色标的删除了,就OK了;
      

  8.   


    mysql> use question;
    Database changed
    mysql> show create trigger insert_user;
    ERROR 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'trigger insert_user' at line 1
      

  9.   

    mysql> use question;
    Database changed
    mysql> show triggers;
    +-------------+--------+-------+-------------------------------+--------+---------+----------+----------------+
    | Trigger     | Event  | Table | Statement                     | Timing | Created | sql_mode | Definer        |
    +-------------+--------+-------+-------------------------------+--------+---------+----------+----------------+
    | insert_user | INSERT | 问题  | set new.user = current_user() | BEFORE | NULL    |          | root@localhost |
    +-------------+--------+-------+-------------------------------+--------+---------+----------+----------------+
    1 row in setmysql> 
      

  10.   

    create trigger insert_user before insert on question
    for each row
    set new.user = user();
      

  11.   

    也只有把current_User() 改成user()了;
      

  12.   

    set new.user = user();终于OK啦,又是2位大侠