1.進入mysql命令行,輸入root及密碼mysql
[root@localhost ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 19 Server version: 5.7.22 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
2.用戶管理及權限設置
// 管理用戶sql
mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed
//查詢用戶數據庫
mysql> select host,user from user; +-----------+---------------+ | host | user | +-----------+---------------+ | % | root | | % | test | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+---------------+ 5 rows in set (0.00 sec)
//建立用戶(用戶:admin,密碼:123456)bash
mysql> create user admin identified by '123456'; Query OK, 0 rows affected (0.00 sec)
// 刪除用戶adminsession
mysql> drop user admin; Query OK, 0 rows affected (0.00 sec)
// 從新建立用戶(用戶:admins,密碼:123456)框架
mysql> create user admins identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> select host, user from user; +-----------+---------------+ | host | user | +-----------+---------------+ | % | admins | | % | root | | % | test | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+---------------+ 6 rows in set (0.00 sec) // 查看用戶admins的權限 mysql> show grants for admins; +------------------------------------+ | Grants for admins@% | +------------------------------------+ | GRANT USAGE ON *.* TO 'admins'@'%' | +------------------------------------+ 1 row in set (0.00 sec) // 賦予權限(給用戶admins,授予數據庫test的查詢權限) mysql> grant select on test.* to admins; Query OK, 0 rows affected (0.00 sec) // 查看用戶admins的權限 mysql> show grants for admins; +------------------------------------------+ | Grants for admins@% | +------------------------------------------+ | GRANT USAGE ON *.* TO 'admins'@'%' | | GRANT SELECT ON `test`.* TO 'admins'@'%' | +------------------------------------------+ 2 rows in set (0.00 sec) // 收回權限(對用戶admins,收回數據庫test的查詢權限) mysql> revoke select on test.* from admins; Query OK, 0 rows affected (0.01 sec) // 查看用戶admins的權限 mysql> show grants for admins; +------------------------------------+ | Grants for admins@% | +------------------------------------+ | GRANT USAGE ON *.* TO 'admins'@'%' | +------------------------------------+ 1 row in set (0.00 sec) // 賦予權限(給用戶admins,授予數據庫test的查詢、更新、刪除、插入等權限) mysql> grant select, update, delete, insert on test.* to admins; Query OK, 0 rows affected (0.00 sec) // 查看用戶admins的權限 mysql> show grants for admins; +------------------------------------------------------------------+ | Grants for admins@% | +------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'admins'@'%' | | GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO 'admins'@'%' | +------------------------------------------------------------------+ 2 rows in set (0.01 sec) // 賦予權限(給用戶admins,授予數據庫test的新建表、刪除表或刪除數據庫等權限) mysql> grant create,drop on test.* to admins; Query OK, 0 rows affected (0.00 sec) // 查看用戶admins的權限 mysql> show grants for admins; +--------------------------------------------------------------------------------+ | Grants for admins@% | +--------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'admins'@'%' | | GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON `test`.* TO 'admins'@'%' | +--------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) // 刷新權限(使設置的權限生效) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) // 查看root的權限 mysql> show grants for root; +-------------------------------------------+ | Grants for root@% | +-------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' | +-------------------------------------------+ 1 row in set (0.00 sec)
如下是用戶能夠享受的常見可能權限的簡短列表。ide
ALL PRIVILEGES - 正如咱們前面所看到的,這將容許MySQL用戶訪問指定的數據庫(或者若是系統中沒有選擇數據庫)
CREATE-容許他們建立新的表或數據庫
DROP-容許他們刪除表或數據庫
DELETE-容許他們從表中刪除行
INSERT-容許它們向表中插入行
SELECT-容許他們使用Select命令來讀取數據庫
UPDATE-容許他們更新錶行
GRANT OPTION - 容許他們授予或刪除其餘用戶的權限
測試
要向特定用戶提供權限,能夠使用此框架:ui
GRANT [type of permission] ON [database name].[table name] TO ‘[username]’@'localhost’;this
若是要向其授予對任何數據庫或任何表的訪問權限,請確保在數據庫名稱或表名稱的地方放置星號(*)。
每次更新或更改權限時,請務必使用Flush Privileges命令。
若是您須要撤消權限,則結構與授予的結構幾乎相同:
REVOKE [type of permission] ON [database name].[table name] FROM ‘[username]’@‘localhost’;
正如您能夠使用DROP刪除數據庫同樣,您能夠使用DROP徹底刪除用戶:
DROP USER ‘demo’@‘localhost’;
要測試您的新用戶,請經過鍵入註銷
quit
並使用此命令在終端中從新登陸: mysql -u [username]-p