【问题标题】:MySQL - Set user password with stored procedureMySQL - 使用存储过程设置用户密码
【发布时间】:2017-08-31 02:13:26
【问题描述】:

我想使用 MySQL 中的存储过程更改用户密码。

这里是创建代码:

CREATE SCHEMA `try` ;
USE try;
CREATE TABLE `users` (
`username` VARCHAR(100) NOT NULL,
`password` VARCHAR(100) NOT NULL,
PRIMARY KEY(`username`));

insert into users values('Admin','pass');

create user 'usertry'@'localhost' identified by 'pass';
grant select on try.* to 'usertry'@'localhost';
grant insert on try.* to 'usertry'@'localhost';
grant update on try.* to 'usertry'@'localhost';
grant delete on try.* to 'usertry'@'localhost';

到这里为止,一切都很好。 然后我创建了一个存储过程来更改表“用户”。

DELIMITER $$
CREATE PROCEDURE `changeusers` (
IN user VARCHAR(255),
IN pass VARCHAR(255),
IN newuser VARCHAR(255),
IN newpass VARCHAR(255),
OUT result bool)
BEGIN
if(user= Binary (Select username from users limit 1) 
and pass= Binary (Select password from users limit 1))
    then
        delete from users where username=user;
        insert into users values(newuser,newpass);
        set result=true;
    else
        set result=false;
end if;
select result;
END $$ DELIMITER ;

当我调用存储过程时,它运行良好。

call changeusers('Admin','pass','Admin','new',@result);

call changeusers('Admin','pass','Admin','new',@result)  1 row(s) returned   0.141 sec / 0.000 sec

然后我尝试更改存储过程以将用户密码设置为与用户表中的密码相同。

DELIMITER $$
CREATE PROCEDURE `changeusers` (
IN user VARCHAR(255),
IN pass VARCHAR(255),
IN newuser VARCHAR(255),
IN newpass VARCHAR(255),
OUT result bool)
BEGIN
if(user= Binary (Select username from users limit 1) 
and pass= Binary (Select password from users limit 1))
    then
        delete from users where username=user;
        insert into users values(newuser,newpass);

        /*The added line*/
        set password for 'usertry'@'localhost' = newpass;
        /*The added line*/

        set result=true;
    else
        set result=false;
end if;
select result;
END $$ DELIMITER ;

但是当我运行新脚本时,我得到了这个:

Error Code: 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 
       'newpass; 
            set result=true;         
        else             
            set result=false;' 
    at line 13  0.000 sec

我会感谢任何建议。 谢谢。

编辑

感谢@Barmar。我能够做我想做的事。 这是新的存储过程。

DELIMITER $$
CREATE PROCEDURE `changeusers` (
IN user VARCHAR(255),
IN pass VARCHAR(255),
IN newuser VARCHAR(255),
IN newpass VARCHAR(255),
OUT result bool)
BEGIN
if(user= Binary (Select username from users limit 1) 
and pass= Binary (Select password from users limit 1))
    then
        delete from users where username=user;
        insert into users values(newuser,newpass);

        /*The added line*/
        set @stm = CONCAT("set password for 'usertry'@'localhost' = ",newpass);
        prepare stmt from @stm;
        execute stmt;
        deallocate prepare stmt;
        /*The added line*/

        set result=true;
    else
        set result=false;
end if;
select result;
END $$ DELIMITER ;

非常感谢您的帮助。

【问题讨论】:

  • 尝试使用ALTER USER 语句而不是SET PASSWORD
  • @Barmar 我尝试使用ALTER USER,但仍然遇到同样的问题。

标签: mysql stored-procedures passwords mysql-error-1064


【解决方案1】:

我不认为SET PASSWORD 允许密码是变量,它必须是文字(或使用文字参数调用PASSWORD()。所以您需要使用准备好的语句:

PREPARE @stmt FROM CONCAT("SET PASSWORD FOR 'usertry'@'localhost' = '", newpass, "'");
EXECUTE @stmt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 2011-03-13
    相关资源
    最近更新 更多