MySQL - 使用存储过程设置用户密码

时间:2017-08-31 02:13:26

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

我想使用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';

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

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

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

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 ;

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

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

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