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


   1.創建用戶

  #test表示你要建立的用戶名,后面的123456表示密碼,   

  #localhost限制在固定地址localhost登陸   

  CREATE USER test@localhost IDENTIFIED BY '123456';

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

 


免責聲明!

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



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