文章目錄
? 六 刪除用戶
attention:MySQL8.0中無法在創(chuàng)建用戶時待讳,直接授予權(quán)限。
必須先創(chuàng)建用戶仰剿,再給與權(quán)限
?一 創(chuàng)建用戶
? ? 1创淡、CREATE USER 'username'@'host' IDENTIFIED BY 'password';
說明:username: 創(chuàng)建的用戶名
host: 指定該用戶在哪個主機上可以登陸,如果是本地用戶可用localhost南吮,如果想讓該用戶可以從任意遠程主機登陸琳彩,可以使用通配符%
‘%’ :哪臺主機上可以登錄mysql,%是通配符指的是任意IP,也可以指定具體的IP露乏,或者localhost代表本機才可以登錄碧浊。
password: 該用戶的登陸密碼,密碼可以為空瘟仿,如果為空則該用戶可以不需要密碼登陸服務(wù)器例子:
1辉词、CREATE USER 'niuben'@'localhost' IDENTIFIED BY '123456';
2、CREATE USER 'niuben'@'192.168.1.101_' IDENDIFIED BY '123456';
3猾骡、CREATE USER 'niuben'@'%' IDENTIFIED BY '123456';
4瑞躺、CREATE USER 'niuben'@'%' IDENTIFIED BY '';
5、CREATE USER 'niuben'@'%';
例如:
mysql> create user 'u1'@'%' identified by 'dir99';
Query OK, 0 rows affected (0.01 sec)
?二 授權(quán)
GRANT privileges ON databasename.tablename TO 'username'@'host' [with grant option];
grant命令說明:
? ??priveleges (權(quán)限列表)兴想,可以是all幢哨,表?所有權(quán)限,也可以是select嫂便、update等權(quán)限捞镰,多個權(quán)限之間?逗號分開。
? ??ON ?來指定權(quán)限針對哪些庫和表毙替,格式為數(shù)據(jù)庫.表名 岸售,點號前??來指定數(shù)據(jù)庫名,點號后??來指定表名厂画,. 表?所有數(shù)據(jù)庫所有表凸丸。
? ??TO 表?將權(quán)限賦予某個?戶, 格式為username@host,@前?為?戶名袱院,@后?接限制的主機屎慢,可以是IP、IP段忽洛、域名以及%腻惠,%表?任何地?。
? ??WITH GRANT OPTION 這個選項表?該?戶可以將??擁有的權(quán)限授權(quán)給別?欲虚。注意:經(jīng)常有?在創(chuàng)建操作?戶的時候不指定WITH GRANT OPTION選項導致后來該?戶不能使?GRANT命令創(chuàng)建?戶或者給其它?戶授權(quán)集灌。 備注:可以使?GRANT重復給?戶添加權(quán)限,權(quán)限疊加复哆,?如你先給?戶添加?個select權(quán)限欣喧,然后又給?戶添加?個insert權(quán)限,那么該?戶就同時擁有了select和insert權(quán)限寂恬。
GRANT SELECT, INSERT ON test.user TO 'u2'@'%';
GRANT ALL ON *.* TO 'u3'@'%';
GRANT ALL ON mysc0530.* TO 'u4'@'%';
例如:
不規(guī)范的語法:
mysql> grant privileges on mysc0530.* to u1;
ERROR 3619 (HY000): Illegal privilege level specified for PRIVILEGES
規(guī)范的語法:
mysql> grant all privileges on mysc0530.* to u1;
Query OK, 0 rows affected (0.01 sec)
注意:
用以上命令授權(quán)的用戶不能給其它用戶授權(quán)续誉,如果想讓該用戶可以授權(quán),用以下命令:
GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;
mysql> grant all privileges on mysc0530.* to u1 with grant option;
Query OK, 0 rows affected (0.01 sec)
mysql> create user 'u3'@'%' identified by 'dir99';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all privileges on mysc0530.* to 'u3'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql>
? 授權(quán)原則說明
? 只授予能滿?需要的最?權(quán)限初肉,防??戶?壞事,?如?戶只是需要查詢饰躲,那就只給
select權(quán)限就可以了牙咏,不要給?戶賦予update臼隔、insert或者delete權(quán)限
? 創(chuàng)建?戶的時候限制?戶的登錄主機,?般是限制成指定IP或者內(nèi)?IP段
? 初始化數(shù)據(jù)庫的時候刪除沒有密碼的?戶妄壶,安裝完數(shù)據(jù)庫的時候會?動創(chuàng)建?些?戶摔握,這些?戶默認沒有密碼
? 為每個?戶設(shè)置滿?密碼復雜度的密碼
? 定期清理不需要的?戶,回收權(quán)限或者刪除?戶
?二 查看用戶權(quán)限
show grants for 'root'@'localhost';
顯示權(quán)限:
mysql> show grants for u1;
+--------------------------------------------------+
| Grants for u1@%? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO `u1`@`%`? ? ? ? ? ? ? ? ? |
| GRANT ALL PRIVILEGES ON `mysc0530`.* TO `u1`@`%` |
+--------------------------------------------------+
2 rows in set (0.00 sec)
show grants;
查看當前?戶的權(quán)限
SHOW GRANTS;
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `root`@`localhost` WITH GRANT OPTION
GRANT PROXY ON ``@`` TO `root`@`localhost` WITH GRANT OPTION
GRANT APPLICATION_PASSWORD_ADMIN,AUDIT_ABORT_EXEMPT,AUDIT_ADMIN,AUTHENTICATION_POLICY_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,CLONE_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,FLUSH_OPTIMIZER_COSTS,FLUSH_STATUS,FLUSH_TABLES,FLUSH_USER_RESOURCES,GROUP_REPLICATION_ADMIN,GROUP_REPLICATION_STREAM,INNODB_REDO_LOG_ARCHIVE,INNODB_REDO_LOG_ENABLE,PASSWORDLESS_USER_ADMIN,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_APPLIER,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SHOW_ROUTINE,SYSTEM_USER,SYSTEM_VARIABLES_ADMIN,TABLE_ENCRYPTION_ADMIN,XA_RECOVER_ADMIN ON *.* TO `root`@`localhost` WITH GRANT OPTION
? 四 設(shè)置和更改用戶密碼
SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');
方法一:直接更新
alter user '用戶名'@'主機范圍' identified by '新密碼';
測試如下:
mysql> revoke all privileges on mysc0530.* from 'u1'@'%';
Query OK, 0 rows affected (0.01 sec)
方法二:通過修改mysql.user表修改密碼
mysql8.0中沒有password加密函數(shù)丁寄,可以用sha\sha1來進行密碼的加密
use mysql;
update user set authentication_string = sha1('321') where user =
'test1' and host = '%';
flush privileges;
經(jīng)測試氨淌,沒毛病:
mysql> UPDATE user SET authentication_string=sha1("dir999") WHERE user='u1';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1? Changed: 1? Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
? 五 撤銷用戶權(quán)限
REVOKE privileges ON databasename.tablename FROM 'username'@'host';
說明:privileges:用戶的操作權(quán)限伊磺,如SELECT盛正,INSERT,UPDATE等屑埋,如果要授予所的權(quán)限則使用ALL豪筝。
如果要授予該用戶對所有數(shù)據(jù)庫和表的相應(yīng)操作權(quán)限則可用表示,如.*
例如:
mysql> alter user 'u1'@'%' identified by 'dir999';
Query OK, 0 rows affected (0.01 sec)
? 六 刪除用戶
方法一:
drop user if exists username;
drop的?式刪除?戶之后摘能,?戶下次登錄就會起效续崖。
方法二:
delete from user where user='?戶名' and host='主機';
flush privileges;
注意通過表的?式刪除的,需要調(diào)?flush privileges;刷新權(quán)限信息(權(quán)限啟動的時候在內(nèi)存中保存著团搞,通過表的?式修改之后需要刷新?下)严望。
測試如下:
mysql> delete from user where user='u3';
Query OK, 1 row affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)