1. 程式人生 > >MySQL5.7 啟動、新增使用者、刪除使用者與授權

MySQL5.7 啟動、新增使用者、刪除使用者與授權

參考:
https://www.cnblogs.com/xujishou/p/6306765.html

MySQL5.7 mysql.user表沒有password欄位改 authentication_string;

零. 啟動mysql

啟動mysql:
在 計算機→管理→服務 中找到 mysql,並啟動。

登入mqsql:
在cmd視窗,執行
mysql -u yourUserName -p
password
登入成功!

給某個使用者重置密碼:(其中localhost,127.0.01,geely指的的ip地址)
set password for [email protected]

=password(‘12345’); –
set password for [email protected]=password(‘12345’);
set password for [email protected]=password(‘12345’);

退出:
exit

刪除匿名使用者:
delete from mysql.user where user=’’;
檢視是否還有匿名使用者:
select user,host from mysql.user;
重新整理,使以上操作生效:(不然登入時會提示:access denied for user ‘username’@‘localhost’(using password:yes))
flush privileges;

show databases;
use mmall;
show tables;

一. 建立使用者:

命令:CREATE USER ‘username’@‘host’ IDENTIFIED BY ‘password’;

例子: CREATE USER ‘dog’@‘localhost’ IDENTIFIED BY ‘123456’;

    CREATE USER 'dog2'@'localhost' IDENTIFIED BY '';

PS:username - 你將建立的使用者名稱,

host - 指定該使用者在哪個主機上可以登陸,此處的"localhost",是指該使用者只能在本地登入,不能在另外一臺機器上遠端登入,如果想遠端登入的話,將"localhost"改為"%",表示在任何一臺電腦上都可以登入;也可以指定某臺機器可以遠端登入;

password - 該使用者的登陸密碼,密碼可以為空,如果為空則該使用者可以不需要密碼登陸伺服器。

二.授權:

命令:GRANT privileges ON databasename.tablename TO ‘username’@‘host’

PS: privileges - 使用者的操作許可權,如SELECT , INSERT , UPDATE 等(詳細列表見該文最後面).如果要授予所的許可權則使用ALL.;databasename - 資料庫名,tablename-表名,如果要授予該使用者對所有資料庫和表的相應操作許可權則可用表示, 如.*.

例子:GRANT SELECT, INSERT ON mq.* TO ‘dog’@‘localhost’;

@後面如果是%,則代表為root使用者開通外網所有許可權。
例子:grant all privileges on mmall.* to ‘root’@’%’;

三.建立使用者同時授權

mysql> grant all privileges on mq.* to [email protected] identified by ‘1234’;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

PS:必須執行flush privileges;

否則登入時提示:ERROR 1045 (28000): Access denied for user ‘user’@‘localhost’ (using password: YES )

四.設定與更改使用者密碼

命令:SET PASSWORD FOR ‘username’@‘host’ = PASSWORD(‘newpassword’);

例子: SET PASSWORD FOR ‘dog2’@‘localhost’ = PASSWORD(“dog”);

五.撤銷使用者許可權

命令: REVOKE privilege ON databasename.tablename FROM ‘username’@‘host’;

說明: privilege, databasename, tablename - 同授權部分.

例子: REVOKE SELECT ON mq.* FROM ‘dog2’@‘localhost’;

PS: 假如你在給使用者’dog’@‘localhost’'授權的時候是這樣的(或類似的):GRANT SELECT ON test.user TO ‘dog’@‘localhost’, 則在使用REVOKE SELECT ON . FROM ‘dog’@‘localhost’;命令並不能撤銷該使用者對test資料庫中user表的SELECT 操作.相反,如果授權使用的是GRANT SELECT ON . TO ‘dog’@‘localhost’;則REVOKE SELECT ON test.user FROM ‘dog’@‘localhost’;命令也不能撤銷該使用者對test資料庫中user表的Select 許可權.

具體資訊可以用命令SHOW GRANTS FOR ‘dog’@‘localhost’; 檢視.

六.刪除使用者

命令: DROP USER ‘username’@‘host’;

七.檢視使用者的授權

mysql> show grants for [email protected];
±--------------------------------------------+
| Grants for [email protected] |
±--------------------------------------------+
| GRANT USAGE ON . TO ‘dog’@‘localhost’ |
| GRANT INSERT ON mq.* TO ‘dog’@‘localhost’ |
±--------------------------------------------+
2 rows in set (0.00 sec)

PS:GRANT USAGE:mysql usage許可權就是空許可權,預設create user的許可權,只能連庫,啥也不能幹