MySql使用者管理:添加使用者、授權、刪除使用者

來源:互聯網
上載者:User

標籤:root使用者   本地   nts   oca   nbsp   update   strong   blog   修改使用者密碼   

添加使用者

以root使用者登入資料庫,運行以下命令:

create user zhangsan identified by ‘zhangsan‘;

上面的命令建立了使用者zhangsan,密碼是zhangsan。在mysql.user表裡可以查看到新增使用者的資訊:

授權

命令格式:grant privilegesCode on dbName.tableName to [email protected] identified by "password";

grant all privileges on zhangsanDb.* to [email protected]‘%‘ identified by ‘zhangsan‘;flush privileges;

上面的語句將zhangsanDb資料庫的所有操作許可權都授權給了使用者zhangsan。

在mysql.db表裡可以查看到新增資料庫許可權的資訊:

也可以通過show grants命令查看許可權授予執行的命令:

show grants for ‘zhangsan‘;

privilegesCode表示授與權限類型,常用的有以下幾種類型[1]:

  • all privileges:所有許可權。
  • select:讀取許可權。
  • delete:刪除許可權。
  • update:更新許可權。
  • create:建立許可權。
  • drop:刪除資料庫、資料表許可權。

dbName.tableName表示授予許可權的具體庫或表,常用的有以下幾種選項:

  • .:授予該資料庫伺服器所有資料庫的許可權。
  • dbName.*:授予dbName資料庫所有表的許可權。
  • dbName.dbTable:授予資料庫dbName中dbTable表的許可權。

[email protected]表示授予的使用者以及允許該使用者登入的IP地址。其中Host有以下幾種類型:

  • localhost:只允許該使用者在本地登入,不能遠程登入。
  • %:允許在除本機之外的任何一台機器遠程登入。
  • 192.168.52.32:具體的IP表示只允許該使用者從特定IP登入。

password指定該使用者登入時的面。

flush privileges表示重新整理許可權變更。

文章首發於【部落格園-陳樹義】,請尊重原創保留原文連結。修改密碼

運行以下命令可以修改使用者密碼

update mysql.user set password = password(‘zhangsannew‘) where user = ‘zhangsan‘ and host = ‘%‘;flush privileges;
刪除使用者

運行以下命令可以刪除使用者:

drop user [email protected]‘%‘;

drop user命令會刪除使用者以及對應的許可權,執行命令後你會發現mysql.user表和mysql.db表的相應記錄都消失了。

常用命令組

建立使用者並授予指定資料庫全部許可權:適用於Web應用建立MySQL使用者

create user zhangsan identified by ‘zhangsan‘;grant all privileges on zhangsanDb.* to [email protected]‘%‘ identified by ‘zhangsan‘;flush  privileges;

建立了使用者zhangsan,並將資料庫zhangsanDB的所有許可權授予zhangsan。如果要使zhangsan可以從本機登入,那麼可以多賦予localhost許可權:

grant all privileges on zhangsanDb.* to [email protected]‘localhost‘ identified by ‘zhangsan‘;

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.