mysql的使用者存取權限規劃

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   sp   for   on   檔案   

看了mysql的關於使用者權限和安全的管理,感覺看起來還是不太好理解,首先mysql的串連方式是需要核對三個要素:user、passwd、 host。而且這個user和passwd並不是o上面的user和passwd,這個僅僅只是串連到mysql server的一個使用者名稱和密碼,而後根據這個user、passwd、 host許可權來訪問相應的database。這個讓接觸了快兩年o的同學還是很不習慣的。

Mysql登入方式,注意如果不寫-h 主機則預設就是-h localhost的主機驗證。
C:\Users\Administrator>mysql -h localhost -u xiaoyu -p
Enter password: ******

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| xiaoyu             |

Mysql的許可權可以規劃為global level,database level,table level,column level,routine level五種,每個level層級有相應的許可權分類,不過生產環境中還是很少進行太細的許可權劃分,一般是針對database level層級的,單個應用例如tomcat weblogic有相應的使用者串連到mysql的database,而root等管理使用者則擁有global level的all privileges。

例如可以用如下的方式授權使用者
mysql> grant all on xiaoyu.* to [email protected] identified by ‘xiaoyu_mys
ql‘;
Query OK, 0 rows affected (0.00 sec)

此時就可以用如下的方式登入mysql server
C:\Users\Administrator>mysql -h localhost -u xiaoyu_mysql -p
Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
| xiaoyu             |
+--------------------+
看出此時用-h localhost -u xiaoyu_asm -p xiaoyu_asm可以串連到mysql server去操作xiaoyu database,而別的database則無法操作了。

那麼如果新制定的使用者不制定密碼會如何了。
mysql> grant all on xiaoyu.* to [email protected];
ERROR 1133 (42000): Can‘t find any matching row in the user table
mysql> grant all on xiaoyu.* to [email protected];
ERROR 1133 (42000): Can‘t find any matching row in the user table
mysql> grant all on xiaoyu.* to xiaoyu;
ERROR 1133 (42000): Can‘t find any matching row in the user table
看出如果新建立的使用者如果不指定密碼無論是localhost主機,IP地址,還是%的萬用字元主機都無法建立成功,這個是因為如果不指定密碼而mysql.user表中又沒有此username和host的記錄row,就會報出上面的1133錯誤。

而加上密碼則可以正常授權了,這點需要注意了。
mysql> grant all on xiaoyu.* to [email protected] identified by ‘xiaoyu‘;
Query OK, 0 rows affected (0.00 sec)

刪除使用者也要聯合host一起刪除。
mysql> drop user [email protected]
-> ;
Query OK, 0 rows affected (0.00 sec)

如果不指定主機drop user預設用的是萬用字元的形式
mysql> grant all on xiaoyu.* to xiaoyu identified by ‘xiaoyu‘;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for xiaoyu;
+-------------------------------------------------------------------------------
------------------------+
| Grants for [email protected]%
                        |
+-------------------------------------------------------------------------------
------------------------+
| GRANT USAGE ON *.* TO ‘xiaoyu‘@‘%‘ IDENTIFIED BY PASSWORD ‘*2E8F2ECC253867BAF7
FB6E8A7B17CFE2EB3DA337‘ |
| GRANT ALL PRIVILEGES ON `xiaoyu`.* TO ‘xiaoyu‘@‘%‘

不指定主機刪除user後,原來[email protected]%記錄已經刪除了,不指定IP或者主機刪除使用者刪除的其實是[email protected]%的使用者,一般建議不要這個刪除使用者,最好帶著主機名稱或者IP地址
mysql> drop user xiaoyu;
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for xiaoyu;
ERROR 1141 (42000): There is no such grant defined for user ‘xiaoyu‘ on host ‘%‘

修改密碼:
那麼如何修改密碼了,其實mysql的使用者 密碼 主機登入三要素都儲存在mysql.user和mysql.db等五個表中,而上面的授權grant和revoke也只是操作mysql下的這五張表而已。

例如如果我們要修改[email protected] 的密碼為mysql
直接修改mysql.user的表中的記錄:
mysql> update mysql.user set password=PASSWORD(‘mysql‘) where user=‘xiaoyu‘ and
host=‘172.16.0.216‘;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
重新整理許可權表到記憶體中,而後就可以用新的密碼登入了,當然[email protected] 以前的密碼也就作廢了。
C:\Users\Administrator>mysql -h 172.16.0.216 -u xiaoyu -pmysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 5.5.25 MySQL Community Server (GPL)

用grant/revoke語句去修改密碼:
mysql> grant all on xiaoyu.* to [email protected] identified by ‘xiaoyu‘;
Query OK, 0 rows affected (0.00 sec)

C:\Users\Administrator>mysql -h 172.16.0.216 -u xiaoyu -pxiaoyu
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.5.25 MySQL Community Server (GPL)

資料庫系統安全:
作為一個資料庫是供應用訪問和資料存放區的,那麼相應的資料庫的安全措施則是非常重要的,相應的mysql資料庫可以用如下三個方面去構造安全的mysql資料庫系統。

1 網路: 封閉外界網路連接,這個也就基本杜絕外來串連造成的安全威脅,不過資料庫大多是作為應用訪問和資料存放區的,通訊一般是必須的,可以部署應用在mysql server本機直連mysql 資料庫,當然這個要看系統的歸檔,需要提供網路訪問的可以讓mysql server在一個規定的區域網路來提供訪問,即使提供外網訪問的也要控制相應的主機來源,這個在資料庫層級也可以控制。

2 主機:減少主機不必要的服務,關閉不必要的連接埠,還要限制登入主機的使用者的許可權,對mysqld的資料檔案和記錄檔許可權規劃要盡量嚴格,避免外來使用者侵入通過資料檔案記錄檔恢複。

3 資料庫:嚴格控制來源主機和使用者權限,主要是來源主機需要嚴格控制,盡量不要出現萬用字元的來訪主機訪問mysql server。

 

http://www.dbaxiaoyu.com/archives/577

mysql的使用者存取權限規劃

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.