mysql創建用戶和管理用戶權限


一、創建用戶:

mysql>create user 'username'@'host' identified by 'password';

username 創建的用戶名

host 指定該用戶在哪個主機上可以登錄,如果是本地用戶可用localhost,如果想讓該用戶可以從任意遠程主機登錄,可以使用通配符%

password 該用戶登錄密碼,密碼可以為空,如果為空該用戶可以不需要密碼登錄服務器

  

二、grant 說明:

grant給用戶添加權限,權限會自動疊加,不會覆蓋之前授予的權限,使用 grant 給用戶授權后,不需要使用 flush priveleges 刷新權限

mysql>grant 權限1,權限2...權限n on 數據庫名稱.表名稱 to 用戶名@用戶地址 identified by ‘連接密碼’ with grant option;

  1. 權限1,權限2...權限n代表select,instert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14個權限
  2. 當權限1,權限2...權限n被 all privileges 或者 all 代替,表示賦予用戶全部權限
  3. 當數據庫名稱.表名稱被 *.* 代替,表示賦予用戶操作服務器上所有數據庫所有表的權限
  4. 用戶地址可以是localhost,也可以是ip地址,機器人名字,域名。也可以用 ‘%’ 表示從任何地址連接
  5. ‘連接密碼’不能為空,否則創建失敗
  6. with grant option允許用戶將自己的權限授予其他用戶(

    如果帶了 with grant option,那么用戶testuser1可以將select ,update權限傳遞給其他用戶( 如testuser2)
    grant select,update on bd_corp to testuser2
    如果沒帶with grant option,那么用戶testuser1不能給testuser2授權

e.g:

mysql>grant select,insert,update,delete,create,drop on testdb.users  to testuser@10.240.171.103 identified by '123456'

給來自10.240.171.103的用戶testuser分配對數據庫testdb的users表進行select,insert,update,create,drop的操作權限,並設定登錄密碼為123456

 

mysql>grant all privileges on testdb.* testuser@10.240.171.103 identified by '123456'

給來着10.240.171.103的用戶testuser分配對數據庫testdb所有表進行所有操作的權限,並設定登錄密碼為123456

 

mysql>grant all privileges on *.*  to testuser@10.240.171.103 identified by '123'

給來自10.240.171.103的用戶testuser分配可對所有數據庫的所有表進行所有操作的權限,並設定登錄密碼為123

 

mysql>flush privileges   

mysql新設置用戶或更改密碼后需要用flush privileges 刷新mysql的系統權限相關表,否則會出現拒絕訪問,還有一種方法,就是重啟mysql服務器,使新設置生效

 

關於root用戶的訪問設置:

設置所有用戶可以遠程訪問mysql,修改my.cnf配置文件,將bind-address=127.0.0.1前面加“#”注釋掉,這樣就允許其他機器人遠程訪問本機mysql了;

mysql>grant all privileges on *.* to root@'%' identified by '123456';    //設置用戶root可以遠程訪問mysql

 

關閉root用戶遠程訪問權限

mysql>delete from user where user="root" and host="%";   //禁止root用戶在遠程機器上訪問mysql

 

三、revoke撤銷權限:

mysql>revoke all privileges on test.* from 'testuser'@'%'

revoke只是撤銷grant創建的權限,但是testuser用戶沒有被刪除,必須手工從user表刪除

 

四、設置與更改用戶密碼

mysql>set password for 'username'@'host'=password('newpassword')  //如果是當前登錄用戶用set password=password('newpassword')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM