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 數據表名;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章