MySQL:添加用戶、刪除用戶、受權、遠程訪問、修改密碼

   1.建立用戶mysql

  #test表示你要創建的用戶名,後面的123456表示密碼,   sql

  #localhost限制在固定地址localhost登錄   數據庫

  CREATE USER test@localhost IDENTIFIED BY '123456';ide

  2.受權用戶test

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

  2.1 登陸MYSQL(有ROOT權限),這裏以ROOT身份登陸:date

  @>mysql -u root -pselect

  @>密碼權限

  2.2 首先爲用戶建立一個數據庫(testDB):密碼

  mysql>create database testDB;

  2.3 受權test用戶擁有testDB數據庫的全部權限(某個數據庫的全部權限):

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

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

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

  2.4 若是想指定部分權限給一用戶,能夠這樣來寫:

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

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

  2.5 受權test用戶擁有全部數據庫的某些權限:   

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

     //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;

相關文章
相關標籤/搜索