標籤:
========
對於ROOT使用者的密碼操作(更改使用者密碼)========剛剛安裝完的Mysql,只一有個root使用者,密碼為空白,而且只能在本機登入!
為root加上密碼xxx123:
./bin/mysqladmin -u root password xxx123
或寫成
./bin/mysqladmin -uroot password xxx123
加下密碼之後,在本進行進入mysql:
./bin/mysql -uroot -p
更改root的密碼由xxx123改為yy1234:
./bin/mysqladmin -uroot -pxxx123 password yy1234 =======
grant 許可權 on 資料庫物件 to 使用者==========
MySQL 賦予使用者權限命令的簡單格式可概括為:
grant 許可權 on 資料庫物件 to 使用者
grant 許可權 on 資料庫物件 to 使用者 identified by "密碼" ========
使用者及許可權管理:最常用操作執行個體========
(使用者名稱:dba1,密碼:dbapasswd,登入IP:192.168.0.10)
//開放管理MySQL中所有資料庫的許可權
grant all on *.* to [email protected]‘192.168.0.10‘identified by "dbapasswd";
//開放管理MySQL中具體資料庫(testdb)的許可權
grant all privileges on testdb to [email protected]‘192.168.0.10‘identified by "dbapasswd";
或
grant all on testdb to [email protected]‘192.168.0.10‘identified by "dbapasswd";
//開放管理MySQL中具體資料庫的表(testdb.table1)的許可權
grant all on testdb.teable1 to [email protected]‘192.168.0.10‘identified by "dbapasswd";
//開放管理MySQL中具體資料庫的表(testdb.table1)的部分列的許可權
grant select(id, se, rank) on testdb.table1 to [email protected]‘192.168.0.10‘identified by "dbapasswd";
//開放管理操作指令
grant select, insert, update, delete on testdb.* to [email protected]‘192.168.0.10‘identified by "dbapasswd";
//回收許可權
revoke all on *.* from [email protected];
//查看 MySQL 使用者權限show grants;
show grants for [email protected]; ========
使用者及許可權管理:更多更詳細執行個體========
下面用執行個體來進行說明:
一、grant普通資料使用者(test1),查詢、插入、更新、刪除 資料庫(test)中所有表資料的權利。grant select on test.* to [email protected]‘%‘;
grant insert on test.* to [email protected]‘%‘;
grant update on test.* to [email protected]‘%‘;
grant delete on test.* to [email protected]‘%‘;或者,用一條 MySQL 命令來替代:
grant select, insert, update, delete on test.* to [email protected]‘%‘;
二、grant資料庫開發人員(duser),建立表、索引、視圖、預存程序、函數。。。等許可權。
grant建立、修改、刪除 MySQL 資料表結構許可權。grant create on testdb.* to [email protected]‘192.168.0.%‘;
grant alter on testdb.* to [email protected]‘192.168.0.%‘;
grant drop on testdb.* to [email protected]‘192.168.0.%‘;
grant 操作 MySQL 外鍵許可權。grant references on testdb.* to [email protected]‘192.168.0.%‘;
grant 操作 MySQL 暫存資料表許可權。grant create temporary tables on testdb.* to [email protected]‘192.168.0.%‘;
grant 操作 MySQL 索引許可權。grant index on testdb.* to [email protected]‘192.168.0.%‘;
grant 操作 MySQL 檢視、查看視圖原始碼 許可權。grant create view on testdb.* to [email protected]‘192.168.0.%‘;
grant show view on testdb.* to [email protected]‘192.168.0.%‘;
grant 操作 MySQL 預存程序、函數 許可權。grant create routine on testdb.* to [email protected]‘192.168.0.%‘;grant alter routine on testdb.* to [email protected]‘192.168.0.%‘;grant execute on testdb.* to [email protected]‘192.168.0.%‘;
三、grant 普通DBA管理某個MySQL資料庫(test)的許可權。grant all privileges on test to [email protected]‘localhost‘其中,關鍵字 “privileges” 可以省略。
四、grant 進階 DBA 管理 MySQL 中所有資料庫的許可權。grant all on *.* to [email protected]‘localhost‘
五、MySQL grant 許可權,分別可以作用在多個層次上。
1. grant 作用在整個 MySQL 伺服器上:grant select on *.* to [email protected]; -- dba 可以查詢 MySQL 中所有資料庫中的表。
grant all on *.* to [email protected]; -- dba 可以管理 MySQL 中的所有資料庫
2. grant 作用在單個資料庫上:grant select on testdb.* to [email protected]; -- dba 可以查詢 testdb 中的表。
3. grant 作用在單個資料表上:grant select, insert, update, delete on testdb.orders to [email protected];
4. grant 作用在表中的列上:grant select(id, se, rank) on testdb.apache_log to [email protected];
5. grant 作用在預存程序、函數上:grant execute on procedure testdb.pr_add to ‘dba‘@‘localhost‘
grant execute on function testdb.fn_add to ‘dba‘@‘localhost‘
六、查看 MySQL 使用者權限查看目前使用者(自己)許可權:show grants;查看其他 MySQL 使用者權限:show grants for [email protected];
七、撤銷已經賦予給 MySQL 使用者權限的許可權。revoke 跟 grant 的文法差不多,只需要把關鍵字 “to” 換成 “from” 即可:grant all on *.* to [email protected];
revoke all on *.* from [email protected];
八、MySQL grant、revoke 使用者權限注意事項1. grant, revoke 使用者權限後,該使用者只有重新串連 MySQL 資料庫,許可權才會生效。2. 如果想讓授權的使用者,也可以將這些許可權 grant 給其他使用者,需要選項 “grant option“grant select on testdb.* to [email protected] with grant option;這個特性一般用不到。實際中,資料庫許可權最好由 DBA 來統一管理。 補充: 可以用 CREATE USER 或 GRANT 建立使用者,後者還同時分配相關許可權。而 REVOKE 則用於刪除使用者權限,DROP USER 刪除賬戶。
$ mysql -u root -p
password:
mysql> create database test; # 建立資料庫
Query OK, 1 row affected (0.00 sec)
mysql> show databases; # 查看資料庫是否建立成功
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql> grant all on test.* to [email protected]‘%‘ identified by ‘123456‘ with grant option; # 建立特權系統管理使用者
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user; # 查看使用者建立是否成功
+------------------+-----------+
| user | host |
+------------------+-----------+
| user1 | % |
| root | 127.0.0.1 |
| debian-sys-maint | localhost |
| root | localhost |
| root | server |
+------------------+-----------+
5 rows in set (0.00 sec)
mysql> show grants for user1; # 查看使用者權限
+--------------------------------------------------------------------------------------------------+
| Grants for [email protected]% |
+--------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘user1‘@‘%‘ IDENTIFIED BY PASSWORD ‘*6BB...2CA2AD9‘ |
| GRANT ALL PRIVILEGES ON `test`.* TO ‘user1‘@‘%‘ WITH GRANT OPTION |
+--------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
GRANT 文法:
GRANT privileges (columns)
ON what
TO user IDENTIFIED BY "password"
WITH GRANT OPTION
許可權列表:
- ALTER: 修改表和索引。
- CREATE: 建立資料庫和表。
- DELETE: 刪除表中已有的記錄。
- DROP: 拋棄(刪除)資料庫和表。
- INDEX: 建立或拋棄索引。
- INSERT: 向表中插入新行。
- REFERENCE: 未用。
- SELECT: 檢索表中的記錄。
- UPDATE: 修改現存表記錄。
- FILE: 讀或寫伺服器上的檔案。
- PROCESS: 查看伺服器中執行的線程資訊或殺死線程。
- RELOAD: 重載授權表或清空日誌、主機緩衝或表緩衝。
- SHUTDOWN: 關閉伺服器。
- ALL: 所有許可權,ALL PRIVILEGES同義字。
- USAGE: 特殊的 "無許可權" 許可權。
使用者賬戶包括 "username" 和 "host" 兩部分,後者表示該使用者被允許從何地接入。[email protected]‘%‘ 表示任何地址,預設可以省略。還可以是 "[email protected]%"、"[email protected]%.abc.com" 等。資料庫格式為 [email protected],可以是 "test.*" 或 "*.*",前者表示 test 資料庫的所有表,後者表示所有資料庫的所有表。
子句 "WITH GRANT OPTION" 表示該使用者可以為其他使用者指派許可權。
我們用 root 再建立幾個使用者,然後由 test 資料庫的管理員 user1 為他們分配許可權。
mysql> create user user2 identified by ‘123456‘, user3 identified by ‘abcd‘;
Query OK, 0 rows affected (0.00 sec)
mysql> select user, host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| user1 | % |
| user2 | % |
| user3 | % |
| root | 127.0.0.1 |
| debian-sys-maint | localhost |
| root | localhost |
| root | server |
+------------------+-----------+
7 rows in set (0.00 sec)
好了,我們退出改用 user1 登入並針對 test 資料庫進行操作。
mysql> quit # 退出
Bye
$ mysql -u user1 -p123456 test # 使用新使用者登入
mysql> select database(); # 確認當前工作資料庫
+------------+
| database() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)
mysql> select current_user(); # 確認當前工作賬戶
+----------------+
| current_user() |
+----------------+
| [email protected]% |
+----------------+
1 row in set (0.00 sec)
繼續,建立一個資料表。
mysql> create table table1 # 建立表
-> (
-> name varchar(50),
-> age integer
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> show tables; # 查看錶是否建立成功
+----------------+
| Tables_in_test |
+----------------+
| table1 |
+----------------+
1 row in set (0.00 sec)
mysql> describe table1; # 查看錶結構
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(50) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into table1 values(‘Tom‘, 20); # 插入記錄
Query OK, 1 row affected (0.00 sec)
mysql> select * from table1; # 查詢記錄
+------+------+
| name | age |
+------+------+
| Tom | 20 |
+------+------+
1 row in set (0.00 sec)
接下來我們為 user2, user3 分配許可權。
mysql> grant select on test.* to user2; # 為 user2 分配 SELECT 許可權。
Query OK, 0 rows affected (0.00 sec)
mysql> grant select on test.* to user3; # 為 user3 分配 SELECT 許可權。
Query OK, 0 rows affected (0.00 sec)
mysql> grant insert, update on test.* to user2; # 再為 user2 增加 INSERT, UPDATE 許可權。
Query OK, 0 rows affected (0.00 sec)
好了,我們退出,切換成 user2 操作看看。
$ mysql -u user2 -p123456
mysql> use test; # 切換工作資料庫
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 database(); # 驗證當前工作資料庫
+------------+
| database() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)
mysql> select user(); # 驗證當前賬戶
+-----------------+
| user() |
+-----------------+
| [email protected] |
+-----------------+
1 row in set (0.00 sec)
mysql> show grants for user2; # 查看目前使用者許可權,顯然後來添加的 INSERT, UPDATE 被添加了。
+--------------------------------------------------------------------------------------------------+
| Grants for [email protected]% |
+--------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘user2‘@‘%‘ IDENTIFIED BY PASSWORD ‘*6BB837....2C9‘ |
| GRANT SELECT, INSERT, UPDATE ON `test`.* TO ‘user2‘@‘%‘ |
+--------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
進行操作測試。
mysql> insert into table1 values("Jack", 21); # INSERT 操作成功
Query OK, 1 row affected (0.00 sec)
mysql> update table1 set age=22 where name=‘Jack‘; # UPDATE 操作成功
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from table1; # SELECT 操作成功
+------+------+
| name | age |
+------+------+
| Tom | 20 |
| Jack | 22 |
+------+------+
2 rows in set (0.00 sec)
mysql> delete from table1 where age=22; # DELETE 操作無許可權
ERROR 1142 (42000): DELETE command denied to user ‘user2‘@‘localhost‘ for table ‘table1‘
我們切換回 user1 管理賬戶,移除 user2 的 UPDATE 許可權看看。
$ mysql -u user1 -p123456 test
mysql> revoke update on test.* from user2; # 移除 UPDATE 許可權
Query OK, 0 rows affected (0.00 sec)
再次切換回 user2。
$ mysql -u user2 -p123456 test
mysql> show grants for user2; # UPDATE 許可權被移除
+--------------------------------------------------------------------------------------------------+
| Grants for [email protected]% |
+--------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘user2‘@‘%‘ IDENTIFIED BY PASSWORD ‘*6B...2AD9‘ |
| GRANT SELECT, INSERT ON `test`.* TO ‘user2‘@‘%‘ |
+--------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> update table1 set age=23 where name=‘Jack‘; # 不在擁有 UPDATE 許可權
ERROR 1142 (42000): UPDATE command denied to user ‘user2‘@‘localhost‘ for table ‘table1‘
好了,到此我們基本完成了建立使用者和分配許可權的操作。接下來,我們回到 root 進行修改使用者密碼和刪除使用者操作。
$ mysql -u root -p123456
mysql> set password for user3=password(‘abcabc‘); # 修改使用者 user3 密碼
Query OK, 0 rows affected (0.00 sec)
mysql>flush privileges; # 重新整理許可權表(通常只在直接修改相關管理資料表後需要該操作)
Query OK, 0 rows affected (0.00 sec)
mysql> revoke all on *.* from user2; # 移除 user2 在所有資料庫上的許可權
Query OK, 0 rows affected (0.00 sec)
mysql> drop user user2; # 刪除 user2 賬戶
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user; # 驗證刪除結果
+------------------+-----------+
| user | host |
+------------------+-----------+
| user1 | % |
| user3 | % |
| root | 127.0.0.1 |
| debian-sys-maint | localhost |
| root | localhost |
| root | server |
+------------------+-----------+
6 rows in set (0.00 sec)
使用者 user2 無法再次使用。
$ mysql -u user2 -p123456 test
ERROR 1045 (28000): Access denied for user ‘user2‘@‘localhost‘ (using password: YES)
試試 user3。
$ mysql -u user3 -pabc test # 串連失敗!哦,對了,我們修改了密碼。
ERROR 1045 (28000): Access denied for user ‘user3‘@‘localhost‘ (using password: YES)
$ mysql -u user3 -pabcabc test # 新密碼成功
mysql> select * from table1; # SELECT 操作成功
+------+------+
| name | age |
+------+------+
| Tom | 20 |
| Jack | 22 |
+------+------+
2 rows in set (0.00 sec)
要修改自己的密碼直接執行 "set password = password(‘new_password‘);" 即可。
------- 摘要 --------------------------------------
建立使用者:
GRANT insert, update ON testdb.* TO [email protected]‘%‘ IDENTIFIED BY ‘password‘ WITH GRANT OPTION;
CREATE USER user2 IDENTIFIED BY ‘password‘;
分配許可權:
GRANT select ON testdb.* TO user2;
查看許可權:
SHOW GRANTS FOR user1;
修改密碼:
SET PASSWORD FOR user1 = PASSWORD(‘newpwd‘);
SET PASSWORD = PASSWORD(‘newpwd‘);
移除許可權:
REVOKE all ON *.* FROM user1;
刪除使用者:
DROP USER user1;
資料庫列表:
SHOW DATABASES;
資料表列表:
SHOW TABLES;
當前資料庫:
SELECT DATABASE();
目前使用者:
SELECT USER();
資料表結構:
DESCRIBE table1;
重新整理許可權:
FLUSH PRIVILEGES;
【轉自】:blog.chinaunix.net/uid-10697776-id-2935586.html
(轉)Mysql使用者與許可權管理