0
我想使用MySQL中的存储过程更改用户的密码。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
我会apreciate任何sugestions。 谢谢。
编辑
感谢@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
@Barmar我尝试使用'ALTER USER',但仍然有同样的问题。 – Lluxed