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