辛星解讀mysql的使用者管理

來源:互聯網
上載者:User

標籤:

     可能做開發的多半不太關注這方面,可是要說到做營運。那就不能不關注了。由於我們都知道,root的許可權太大了。不是隨便能用的。我們平時最好用一些比較低的許可權的使用者。這樣會讓我們的安全性大大提高,也能防止我們尋常中由於誤操作而造成不必要的損失。

      首先我們須要查看mysql中的全部賬戶,我們在mysql資料庫中的user表中查看資訊就可以。可是呢。因為我們直接select * from user的話會顯示非常多的和許可權相關的資訊,極大的影響我們的閱讀。因此我們這裡僅僅查看三個比較重要的欄位,以下是我的機器中的操作示範:

mysql>   select host,user,password from mysql.user;+-----------+------+-------------------------------------------+| host      | user | password                                  |+-----------+------+-------------------------------------------+| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B || 127.0.0.1 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B || ::1       | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |+-----------+------+-------------------------------------------+3 rows in set (0.00 sec)


     然後我們建立一個使用者,這裡我們須要制定username和串連的地址,也就是一樣的username(user)。不一樣的host。也會被當做不一樣的對象,我們能夠使用萬用字元。當中%表示匹配不論什麼多個字元,而-表示匹配一個字元。以下我建立一個使用者test。並且制定password為xin,並且能夠從不論什麼主機登入。看我操作:

mysql> create user 'test'@'%' identified by 'xin';Query OK, 0 rows affected (0.23 sec)

    這裡我們用該使用者登入的資訊顯示範範例如以下:

C:\Users\Administrator>mysql -u test -pxinmysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 14Server version: 5.7.3-m13 MySQL Community Server (GPL)Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>

 

比方我們此時想查看一下user資訊都有什麼。結果卻發現:

mysql> select host,user,password from mysql.user;ERROR 1142 (42000): SELECT command denied to user 'test'@'localhost' for table 'user'mysql>


也就是我們這裡並不用有select該user表的許可權。那麼就會涉及到許可權管理了,我們這裡能夠用show grants來查看一下自己的許可權:

mysql> show grants;+-----------------------------------------------------------------------------------------------------+| Grants for [email protected]%                                                                                   |+-----------------------------------------------------------------------------------------------------+| GRANT USAGE ON *.* TO 'test'@'%' IDENTIFIED BY PASSWORD '*76995602B7611FA37648852F235D6ECB29D844E2' |+-----------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)

      因為這裡的test使用者的許可權還是非常小的。我們還是使用root使用者登入,然後此時我們看一下使用者表,就發現多了一個新使用者:

mysql> select host,user,password from mysql.user;+-----------+------+-------------------------------------------+| host      | user | password                                  |+-----------+------+-------------------------------------------+| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B || 127.0.0.1 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B || ::1       | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B || %         | test | *76995602B7611FA37648852F235D6ECB29D844E2 |+-----------+------+-------------------------------------------+4 rows in set (0.00 sec)


       root使用者具有至高無上的許可權。因此他能夠改動password,第一種方式就是使用set  password語句,比方我們給剛才的新使用者佈建一個password。我們能夠使用例如以下命令:

mysql> set  password for 'test'@'%' = password('qian');Query OK, 0 rows affected (0.00 sec)

     此時我們就能夠使用test這個使用者登入了,以下看我代碼:

C:\Users\Administrator>mysql -u test -pqianmysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 17Server version: 5.7.3-m13 MySQL Community Server (GPL)Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>


   事實上我們還能夠直接使用update語句來改動user這個表,可是我們須要使用flush privileges;來讓它馬上生效:

mysql> update mysql.user set password = password('nan') where user = 'test';Query OK, 1 row affected (0.05 sec)Rows matched: 1  Changed: 1  Warnings: 0mysql> flush privileges;Query OK, 0 rows affected (0.03 sec)

   然後我們就能夠用‘nan‘這個password登入了,我這裡就不給出示範代碼了,畢竟非常easy的操作,我是辛星。求支援。

辛星解讀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.