_ 表示任意單個字符
% 表示任意長度的任意字符
查詢用戶
#切換到mysql庫,查詢user表,得到當前庫的用戶名和其能夠遠程登錄時所在的host
> use mysql;
> select user,host,password from user;
創(chuàng)建用戶
1. 方法一
> create user 'kcxg'@'127.0.0.1';
> create user 'kcxg'@'127.0.0.1' identified by '123456';
2. 方法二(使用較少,設(shè)置嚴格的sql-mode后,無法使用)
> insert into mysql.user(user,host,password) values('kcxg','192.168.%.%',password('123456'));
3.方法三(在授權(quán)數(shù)據(jù)庫時,用戶不存在將會自動創(chuàng)建)
> grant all on cuiyk.* to kcxg@127.0.0.1 identified by '123456'
刪除用戶
1. 方法一(使用此方法會有相關(guān)信息殘留)
> delete from mysql.user where user='kcxg' and host='localhost';
2. 方法二
> drop user kcxg@localhost;
重命名用戶
> rename user oldname to newname;
修改密碼
1.方法一
> mysqladmin -u用戶名 -p舊密碼 password 新密碼
2.方法二
> set password for kcxg@host = password('newpassword');
3.方法三
適用mysql5.6以及之前的版本
>update mysql.user set password = password('密碼') where user='用戶名';
適用mysql5.7
> update mysql.user set authentication_string = password('密碼') where user='用戶名';
4.方法四(重置root密碼)
- [1] 停止當前mysql進程
- [2] mysqld_safe --skip-grant-tables &
- [3] mysql -uroot
- [4] mysql> UPDATE mysql.user SET password=PASSWORD("new password") WHERE user='root';
- [5] mysql> FLUSH PRIVILEGES;
- [6] 停止數(shù)據(jù)庫以后,按照正常的方式重啟數(shù)據(jù)庫,使用新密碼登錄即可
授權(quán)管理
授權(quán)命令
> GRANT ALL [PRIVILEGES] ON db.tbl TO 'username'@'host' IDENTIFIED BY 'password';
#給本地用戶授權(quán)某個數(shù)據(jù)庫的所有權(quán)限
> grant all privileges on zsythink.* to zsy@localhost identified by 'zsythink';
#給遠程用戶授權(quán)
> grant all privileges on zsythink.* to zsy@'%' identified by 'zsythink';
> FLUSH PRIVILEGES
#授權(quán)某些權(quán)限
> grant select privileges on zsythink.* to zsy@'192.168.%.%';
> grant insert,delete,update,select on zsythink.* to zsy@'192.168.%.%';
#授權(quán)于多個用戶
> grant select on hellodb.* to zsy@localhost,zsythink@localhost;
#只對某張表的某個字段授權(quán)
> grant select (name,age) on zsythink.students to zsy@localhost;
#function指明被操作對象為函數(shù)
> grant execute on function zsythink.test to zsy@'192.168.%.%';
#procedure 指明備操作的對象為存儲過程
> grant execute on procedure zsythink.test to zsy@'192.168.%.%';
#usage權(quán)限用于登錄用戶
> grant usage on *.* to 'zsy'@'222.222.222.222' require ssl;
> grant usage on *.* to 'zsy'@'222.222.222.222' require none;
#如果在授權(quán)zsy用戶時,搭配grant選項,則zsy用戶有權(quán)將擁有的用戶授予其他用戶
> grant select on zsythink.* to zsy@'192.168.%.%' with grant option;
MAX_QUERIES_PER_HOUR:限制用戶每小時執(zhí)行的查詢語句數(shù)量
MAX_UPDATES_PER_HOUR:限制用戶每小時執(zhí)行的更新語句數(shù)量
MAX_CONNECTIONS_PER_HOUR:限制用戶每小時連接數(shù)據(jù)庫的次數(shù)
MAX_USER_CONNECTIONS:限制用戶使用當前賬號同時連接服務器的連接數(shù)量
> grant select on *.* to zsy@'192.168.%.%' identified by '123456' with MAX_QUERIES_PER_HOUR 20;
查看授權(quán)
> show grants for 用戶名;
> show grants for zsy@localhost
> select * from mysql.db where Db="你要查看的數(shù)據(jù)庫"
刪除授權(quán)
> revoke "要移除的權(quán)限" on 數(shù)據(jù)庫.表 from 用戶@host;
|