mysql用戶分配權限


在mysql中用戶權限是一個很重析 參數,因為台mysql服務器中會有大量的用戶,每個用戶的權限需要不一樣的,下面我來介紹如何給mysql用戶分配權限吧,有需要了解的朋友可參考。

1,mysql創建用戶

語法:

1.create user 用戶名 identified by '密碼';

例:create user xiaogang identified by '123456';

新創建的用戶,默認情況下是沒有任何權限的。

2. 如何給用戶分配權限

語法:

1.grant 權限 on 數據庫.數據表 to '用戶' @ '主機名';

例:給 xiaogang 分配所有的權限

grant all on *.* to 'xiaogang'@'%';

這個時候 xiaogang 就擁有了 所有權限了

3 如何更精准的控制用戶的權限呢?

1.grant 權限 on 數據庫.數據表 to '用戶' @ '主機名';

例:讓 xiaogang 有查詢 tmp 數據庫 tmp1 表的權限;

grant select on temp.temp1 to 'xiaogang'@'%';   //這個時候 xiaogang 就具有查詢temp小的temp1的權限了。

例如:

mysql>grant select,insert,update,delete,create,drop on vtdc.employee to joe@10.163.225.87 identified by ‘123′;

給來自10.163.225.87的用戶joe分配可對數據庫vtdc的employee表進行select,insert,update,delete,create,drop等操作的權限,並設定口令為123。

mysql>grant all privileges on vtdc.* to joe@10.163.225.87 identified by ‘123′;

給來自10.163.225.87的用戶joe分配可對數據庫vtdc所有表進行所有操作的權限,並設定口令為123。

mysql>grant all privileges on *.* to joe@10.163.225.87 identified by ‘123′;

給來自10.163.225.87的用戶joe分配可對所有數據庫的所有表進行所有操作的權限,並設定口令為123。

mysql>grant all privileges on *.* to joe@localhost identified by ‘123′;

給本機用戶joe分配可對所有數據庫的所有表進行所有操作的權限,並設定口令為123。

4. 如何收回 權限,一般指有root用戶才具有該權限

語法:

1.revoke 權限 on  數據庫.數據表 from '用戶'@'主機名';

例:收回 xiaogang的所有權限

revoke all on *.* from 'xiaogang' @'%';

好了下面我個把步驟總結一下很具體的一個過程

第一步:mysql啟動和停止

net stop mysql

net start mysql

第二步:直接登陸mysql

語法如下: mysql -u用戶名 -p用戶密碼

鍵入命令mysql -uroot -p, 回車后提示你輸入密碼,輸入123456,然后回車即可進入到mysql中了,mysql的提示符是:

mysql>

注意,如果是連接到另外的機器上,則需要加入一個參數-h機器IP

第三步:增加新用戶

格式:grant 權限 on 數據庫.* to 用戶名@登錄主機 identified by "密碼"

如,增加一個用戶user1密碼為password1,讓其可以在本機上登錄, 並對所有數據庫有查詢、插入、修改、刪除的權限。首先用以root用戶連入mysql,然后鍵入以下命令:

grant select,insert,update,delete on *.* to user1@localhost Identified by "password1";

如果希望該用戶能夠在任何機器上登陸mysql,則將localhost改為"%"。

如果你不想user1有密碼,可以再打一個命令將密碼去掉。

grant select,insert,update,delete on mydb.* to user1@localhost identified by "";

第四步: 操作數據庫

登錄到mysql中,然后在mysql的提示符下運行下列命令,每個命令以分號結束

(上面的方法本人已驗證確實可行,下面方法摘自網上)

 

 

MySql中添加用戶,新建數據庫,用戶授權,刪除用戶,修改密碼(注意每行后邊都跟個;表示一個命令語句結束):

1.新建用戶

  1.1 登錄MYSQL:

  @>mysql -u root -p

  @>密碼

  1.2 創建用戶:

  mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));

  這樣就創建了一個名為:test 密碼為:1234 的用戶。

  注意:此處的"localhost",是指該用戶只能在本地登錄,不能在另外一台機器上遠程登錄。如果想遠程登錄的話,將"localhost"改為"%",表示在任何一台電腦上都可以登錄。也可以指定某台機器可以遠程登錄。

  1.3 然后登錄一下:

  mysql>exit;

  @>mysql -u test -p

  @>輸入密碼

  mysql>登錄成功

 

2.為用戶授權

  授權格式:grant 權限 on 數據庫.* to 用戶名@登錄主機 identified by "密碼";

  2.1 登錄MYSQL(有ROOT權限),這里以ROOT身份登錄:

  @>mysql -u root -p

  @>密碼

  2.2 首先為用戶創建一個數據庫(testDB):

  mysql>create database testDB;

  2.3 授權test用戶擁有testDB數據庫的所有權限(某個數據庫的所有權限):

   mysql>grant all privileges on testDB.* to test@localhost identified by '1234';

   mysql>flush privileges;//刷新系統權限表

  格式:grant 權限 on 數據庫.* to 用戶名@登錄主機 identified by "密碼";

  2.4 如果想指定部分權限給一用戶,可以這樣來寫:

  mysql>grant select,update on testDB.* to test@localhost identified by '1234';

  mysql>flush privileges; //刷新系統權限表

  2.5 授權test用戶擁有所有數據庫的某些權限:   

  mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";

     //test用戶對所有數據庫都有select,delete,update,create,drop 權限。

  //@"%" 表示對所有非本地主機授權,不包括localhost。(localhost地址設為127.0.0.1,如果設為真實的本地地址,不知道是否可以,沒有驗證。)

 //對localhost授權:加上一句grant all privileges on testDB.* to test@localhost identified by '1234';即可。

3、刪除用戶

   @>mysql -u root -p

  @>密碼

   mysql>Delete FROM user Where User='test' and Host='localhost';

   mysql>flush privileges;

   mysql>drop database testDB; //刪除用戶的數據庫

刪除賬戶及權限:>drop user 用戶名@'%';

        >drop user 用戶名@ localhost; 

 

4. 修改指定用戶密碼

    @>mysql -u root -p

    @>密碼

    mysql>update mysql.user set password=password('新密碼') where User="test" and Host="localhost";

    mysql>flush privileges;

 

5. 列出所有數據庫

  mysql>show database;

 

6. 切換數據庫

  mysql>use '數據庫名';

 

7. 列出所有表

  mysql>show tables;

 

8. 顯示數據表結構

  mysql>describe 表名;

 

9. 刪除數據庫和數據表

  mysql>drop database 數據庫名;

  mysql>drop table 數據表名;





免責聲明!

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



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