【例13.1】使用root用戶登陸到本地mysql服務器的test庫中mysql
mysql -uroot -p -hlocalhost testsql
【例13.2】使用root用戶登陸到本地mysql服務器的test庫中,執行一條查詢語句數據庫
mysql -uroot -p -hlocalhost test -e "DESC person;"服務器
【例13.3】使用CREATE USER建立一個用戶,用戶名是jeffrey,密碼是mypass,主機名是localhostide
CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';工具
【例13.4】使用GRANT語句建立一個新的用戶testUser,密碼爲testpwd。用戶 testUser對全部的數據有查詢和更新權限,並授於對全部數據表的SELECT和UPDATE權限ci
GRANT SELECT,UPDATE ON *.* TO 'testUser'@'localhost' IDENTIFIED BY 'testpwd'; /*建立帳戶並授予權限*/it
SELECT Host,User,Select_priv,Update_priv, FROM mysql.user where user='testUser'; /*查看帳戶權限信息*/table
【例13.5】使用INSERT建立一個新帳戶,其用戶名稱爲customer1,主機名稱爲localhost,密碼爲customer1:class
INSERT INTO user (Host,User,Password) VALUES('localhost','customer1',PASSWORD('customer1'));
【例13.6】使用DROP USER刪除用戶'jeffrey'@'localhost'
DROP USER 'jeffrey'@'localhost';
【例13.7】使用DELETE刪除用戶'customer1'@'localhost'
DELETE FROM mysql.user WHERE host='localhost' and user='customer1';
【例13.8】使用mysqladmin將root用戶的密碼修改成「rootpwd」
mysqladmin -u root -p password "123456"
【例13.9】使用UPDATE語句將root用戶的密碼修改成「rootpwd2」:
UPDATE mysql.user set Password=password("rootpwd2")
WHERE User="root" and Host="localhost";
【例13.10】使用SET語句將root用戶的密碼修改成「rootpwd3」:
SET PASSWORD=password("rootpwd3");
【例13.11】使用SET語句將testUser用戶的密碼修改成「newpwd」:
SET PASSWORD FOR 'testUser'@'localhost'=password("newpwd");
【例13.12】使用UPDATE語句將testUser用戶的密碼修改成「newpwd2」:
UPDATE mysql.user set Password=PASSWORD("newpwd2")
WHERE User="testUser" and Host="localhost";
【例13.13】使用GRANT語句將testUser用戶的密碼修改成「newpwd3」:
GRANT USAGE ON *.* TO 'testUser'@'localhost' IDENTIFIED BY 'newpwd3';
【例13.14】testUser用戶使用SET語句將自身的密碼修改成「newpwd4」:
SET PASSWORD = PASSWORD("newpwd4");
【例13.15】使用GRANT語句建立一個新的用戶grantUser,密碼爲「grantpwd」。用戶grantUser對全部的數據有查詢、插入權限,並授於GRANT權限。GRANT語句及其執行結果以下:
MySQL> GRANT SELECT,INSERT ON *.* TO 'grantUser'@'localhost' IDENTIFIED BY 'grantpwd' WITH GRANT OPTION;
Query OK, 0 rows affected (0.03 sec)
結果顯示執行成功,使用SELECT語句查詢用戶testUser2的權限:
MySQL> SELECT Host,User,Select_priv,Insert_priv, Grant_priv FROM mysql.user where user='grantUser';
+-----------+------------+-------------+--------------+-------------+
| Host | User | Select_priv | Insert_priv | Grant_priv |
+-----------+------------+-------------+--------------+--------------+
| localhost | testUser2 | Y | Y | Y |
+-----------+------------+-------------+--------------+--------------+
1 row in set (0.00 sec)
【例13.16】使用REVOKE語句取消用戶testUser的更新權限。REVOKE語句及其執行結果以下:
MySQL> REVOKE UPDATE ON *.* FROM 'testUser'@'localhost';
Query OK, 0 rows affected (0.00 sec)
執行結果顯示執行成功,使用SELECT語句查詢用戶test的權限:
MySQL> SELECT Host,User,Select_priv,Update_priv,Grant_priv FROM MySQL.user where user='testUser';
+-----------+------+--------------+---------------+--------------+
| Host | User | Select_priv | Update_priv | Grant_priv |
+-----------+------+--------------+---------------+--------------+
| localhost | test | Y | N | Y |
+-----------+------+-------------+---------------+--------------+
1 row in set (0.00 sec)
【例13.17】使用SHOW GRANTS語句查詢用戶testUser的權限信息。SHOW GRANTS語句及其執行結果以下:
MySQL> SHOW GRANTS FOR 'testUser'@'localhost';
+------------------------------------------------------------------------------------------------------------------------------+
| Grants for testUser@localhost |
+------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT ON *.* TO 'testUser'@'localhost' IDENTIFIED BY PASSWORD
'*53835E70E1FC57BE1A455169C761A8778D307C81' WITH GRANT OPTION |
+------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
2. 案例操做過程
打開MySQL客戶端工具,輸入登陸命令,登陸MySQL。
C:\>mysql -u root -p
Enter password: **
輸入正確密碼,按回車,出現歡迎信息表示登陸成功。
選擇mysql數據庫爲當前數據庫。
MySQL> use mysql;
Database changed
出現Database changed信息代表切換數據庫成功。
建立新帳戶,用戶名稱爲newAdmin,密碼爲pw1,容許其從本地主機訪問MySQL。
使用GRANT語句建立新帳戶,建立過程以下:
MySQL> GRANT SELECT, UPDATE(id, name, age)
-> ON test_db.person_old
-> TO 'newAdmin'@'localhost' IDENTIFIED BY 'pw1'
-> WITH MAX_CONNECTIONS_PER_HOUR 30;
Query OK, 0 rows affected (0.06 sec)
提示消息能夠看到,語句執行成功。
分別從user表中查看新帳戶的帳戶信息,從tables_priv和columns_priv表中查看權限信息。
用戶帳戶建立完成以後,帳戶信息已經保存在user表,權限信息則分別保存在tables_priv和columns_priv中,查詢user名稱爲newAdmin的帳戶信息,執行過程以下:
SELECT host, user, select_priv, update_priv FROM user WHERE user='newAdmin';
SELECT host, db, user, table_name, table_priv, column_priv
FROM tables_priv WHERE user='newAdmin';
SELECT host, db, user, table_name, column_name, column_priv
FROM columns_priv WHERE user='newAdmin';
3條SQL語句的查詢結果分別以下:
MySQL> SELECT host, user, select_priv, update_priv FROM user WHERE user='newAdmin';
+-----------+--------------+-------------+--------------+
| host | user | select_priv | update_priv |
+-----------+--------------+-------------+--------------+
| localhost | newAdmin | N | N |
+-----------+--------------+-------------+--------------+
1 row in set (0.00 sec)
MySQL> SELECT host, db, user, table_name, table_priv, column_priv
-> FROM tables_priv WHERE user='newAdmin';
+-----------+-----+-------------+-------------+-------------+---------------+
| host | db | user | table_name | table_priv | column_priv |
+-----------+-----+-------------+-------------+-------------+---------------+
| localhost | test | newAdmin | person | Select | Update |
+-----------+-----+-------------+-------------+-------------+---------------+
1 row in set (0.00 sec)
MySQL> SELECT host, db, user, table_name, column_name, column_priv
-> FROM columns_priv WHERE user='newAdmin';
+-----------+-----+-------------+-------------+-----------------+----------------+
| host | db | user | table_name | column_name | column_priv |
+-----------+-----+-------------+-------------+-----------------+----------------+
| localhost | test | newAdmin | person | id | Update |
| localhost | test | newAdmin | person | name | Update |
| localhost | test | newAdmin | person | age | Update |
+-----------+-----+-------------+-------------+-----------------+----------------+
3 rows in set (0.00 sec)
使用SHOW GRANTS語句查看newAdmin的權限信息。
查看newAdmin帳戶的權限信息,輸入語句以下:
SHOW GRANTS FOR 'newAdmin'@'localhost';
執行結果以下:
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for newAdmin@localhost |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'newAdmin'@'localhost' IDENTIFIED BY PASSWORD '*2B602296
A79E0A8784ACC5C88D92E46588CCA3C3' WITH MAX_CONNECTIONS_PER_HOUR 30 |
| GRANT SELECT, UPDATE (age, id, name) ON `test`.`person` TO 'newAdmin'@'localhost' |
+-----------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
使用newAdmin用戶登陸MySQL。
退出當前登陸,使用EXIT命令,語句以下:
MySQL> exit
Bye
使用newAdmin帳戶登陸MySQL,語句以下:
C:\>MySQL -u newAdmin -p
Enter password: ***
輸入密碼正確後,出現「mysql>」提示符,登陸成功。
使用newAdmin用戶查看test_db數據庫中person_dd表中的數據。
newAdmin用戶被授予test數據庫中person表中3個字段上的查詢權限,所以能夠執行SELECT語句查看這幾個字段的值,執行過程以下:
MySQL> SELECT * FROM test_db.person_dd LIMIT 5;
+----+----------+-----+--------------+
| id | name | age | info |
+----+----------+-----+--------------+
| 1 | Green | 21 | Lawyer |
| 2 | Suse | 22 | dancer |
| 3 | Mary | 24 | Musician |
| 4 | Willam | 20 | sports man |
| 5 | Laura | 25 | NULL |
+----+----------+-----+--------------+
5 rows in set (0.00 sec)
能夠看到,查詢結果顯示了表中的前5條記錄。
使用newAdmin用戶向person_dd表中插入一條新記錄,查看語句執行結果。
插入新記錄,輸入語句以下:
INSERT INTO test_db.person_old(name, age,info) VALUES('gaga', 30);
執行結果以下:
ERROR 1142 (42000): INSERT command denied to user 'newAdmin'@'localhost' for table 'person'
能夠看到,語句不能執行,錯誤信息代表newAdmin用戶不能對person表進行插入操做。所以,用戶不能夠執行沒有被受權的操做語句。
退出當前登陸,使用root用戶從新登陸,收回newAdmin帳戶的權限。
輸入退出命令:
exit
從新以root用戶登陸MySQL,並選擇mysql數據庫爲當前數據庫。
輸入語句收回newAdmin帳戶的權限,執行過程以下:
REVOKE SELECT, UPDATE ON test.person FROM 'newAdmin'@'localhost';
執行結果以下:
MySQL> REVOKE SELECT, UPDATE ON test.person FROM 'newAdmin'@'localhost';
Query OK, 0 rows affected (0.00 sec)
刪除newAdmin的帳戶信息。
刪除指定帳戶,能夠使用DROP USER語句,輸入以下:
DROP USER 'newAdmin'@'localhost';
語句執行成功以後,tables_priv和columns_priv中相關的記錄將被刪除。
步驟1.打開MySQL客戶端工具,輸入登陸命令,登陸MySQL:
/*使用root用戶登陸mysql*/
mysql -u root -p
步驟2.將選擇mysql數據庫爲當前數據庫。
use mysql;
步驟3.建立新帳戶,用戶名稱爲newAdmin,容許其從本地主機訪問MySQL。
GRANT SELECT, UPDATE(id, name, age)
ON test.person
TO 'newAdmin'@'localhost' IDENTIFIED BY 'pw1'
WITH MAX_CONNECTIONS_PER_HOUR 30;
步驟4.分別從user表中查看新帳戶的帳戶信息,從tables_priv和columns_priv表中查看權限信息。
/* 查看user表中帳戶信息*/
SELECT host, user, select_priv, update_priv FROM user WHERE user='newAdmin';
/*查看tables_priv表中權限信息*/
SELECT host, db, user, table_name, table_priv, column_priv
FROM tables_priv WHERE user='newAdmin';
/*查看columns_priv表中權限信息*/
SELECT host, db, user, table_name, column_name, column_priv
FROM columns_priv WHERE user='newAdmin';
步驟5.使用SHOW GRANTS語句查看newAdmin的權限信息
SHOW GRANTS FOR 'newAdmin'@'localhost';
步驟6.使用newAdmin用戶登陸MySQL
/*退出命令*/
exit
/*使用newAdmin 用戶登陸mysql*/
mysql -u newAdmin -p
步驟7.使用newAdmin用戶查看test數據庫中person表中的數據
SELECT * FROM test.person LIMIT 5;
步驟8.使用newAdmin用戶向person表中插入一條新記錄,查看語句執行結果。
INSERT INTO test.person(name, age,info) VALUES('gaga', 30);
步驟9.退出當前登陸,使用root用戶從新登陸,收回newAdmin帳戶的權限。
/*退出命令*/
exit
/*使用root用戶登陸mysql*/
mysql -u root -p
REVOKE SELECT, UPDATE ON test.person FROM 'newAdmin'@'localhost';
步驟10.刪除newAdmin的帳戶信息。
刪除指定帳戶,能夠使用DROP USER語句,輸入以下:
DROP USER 'newAdmin'@'localhost';