查看現(xiàn)有用戶
select host,user,authentication_string from mysql.user;
新建用戶
create user "username"@"host" identified by"password";
注:host="localhost"為本地登錄用戶门烂,host="ip"為ip地址登錄库糠,host="%"汇陆,為外網(wǎng)ip登錄.
刪除用戶
drop user 'username'@'host';
用戶授權(quán)
grant privileges on databasename.tablename to 'username'@'host' IDENTIFIED BY 'PASSWORD';
GRANT命令說(shuō)明:
priveleges
(權(quán)限列表)(select
, insert
, update
, delete
, create
, drop
, index
, alter
, grant
, references
, reload
, shutdown
, process
, file
共14個(gè)權(quán)限,可被all privileges或all代替 ),也可以是select叨橱、update等權(quán)限建炫,多個(gè)權(quán)限的名詞,相互之間用逗號(hào)分開畦韭。
on
用來(lái)指定權(quán)限針對(duì)哪些庫(kù)和表。
*.*
中前面的*
號(hào)用來(lái)指定數(shù)據(jù)庫(kù)名肛跌,后面的*
號(hào)用來(lái)指定表名艺配。
to
表示將權(quán)限賦予某個(gè)用戶, 如 kuvi@'localhost'
表示kuvi用戶察郁,@
后面接限制的主機(jī),可以是IP转唉、IP段皮钠、域名以及%,%
表示任何地方赠法。注意:這里%
有的版本不包括本地
identified by
指定用戶的登錄密碼,該項(xiàng)可以省略麦轰。
WITH GRANT OPTION
將自己擁有的權(quán)限授權(quán)給別人。
備注:可以使用GRANT重復(fù)給用戶添加權(quán)限砖织,權(quán)限疊加款侵,比如你先給用戶添加一個(gè)select權(quán)限,然后又給用戶添加一個(gè)insert權(quán)限镶苞,那么該用戶就同時(shí)擁有了select和insert權(quán)限喳坠。
授予用戶通過(guò)外網(wǎng)IP對(duì)于該數(shù)據(jù)庫(kù)的全部權(quán)限
grant all privileges on `test`.* to 'test'@'%' ;/*授予用戶在本地服務(wù)器對(duì)該數(shù)據(jù)庫(kù)的全部權(quán)限*/
grant all privileges on `test`.* to 'test'@'localhost';
grant select on test.* to 'user1'@'localhost'; /*給予查詢權(quán)限*/
grant insert on test.* to 'user1'@'localhost'; /*添加插入權(quán)限*/
grant delete on test.* to 'user1'@'localhost'; /*添加刪除權(quán)限*/
grant update on test.* to 'user1'@'localhost'; /*添加權(quán)限*/
flush privileges; /*刷新權(quán)限*/
查看權(quán)限
show grants;
查看某個(gè)用戶的權(quán)限
show grants for 'user'@'host';
刪除權(quán)限
revoke privileges on databasename.tablename from 'username'@'host';
eg:revoke delete on test.* from 'kuvi'@'localhost';
修改用戶名
mysql> rename user 'kuvi'@'%' to 'momo'@'%';
修改密碼
使用setpassword
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
使用mysqladmin
mysqladmin -uroot -p123456 password 1234abcd