Mysql添加用戶並授權


Mysql添加用戶並授權

1.使用root用戶登錄mysql數據庫,切換到mysql數據庫。

mysql -u用戶名  -p密碼
use mysql

 

 

 2.添加用戶

//只允許指定ip連接
create user '新用戶名'@'localhost' identified by '密碼';
//允許所有ip連接(用通配符%表示)
create user '新用戶名'@'%' identified by '密碼';

 3.為用戶授權

//基本格式如下
grant all privileges on 數據庫名.表名 to '新用戶名'@'指定ip' identified by '新用戶密碼' ;
//示例
//允許訪問所有數據庫下的所有表
grant all privileges on *.* to '新用戶名'@'指定ip' identified by '新用戶密碼' ;
//指定數據庫下的指定表
grant all privileges on test.test to '新用戶名'@'指定ip' identified by '新用戶密碼' ;

 

4.設置用戶操作權限

//設置用戶擁有所有權限也就是管理員
grant all privileges on *.* to '新用戶名'@'指定ip' identified by '新用戶密碼' WITH GRANT OPTION;
//擁有查詢權限
grant select on *.* to '新用戶名'@'指定ip' identified by '新用戶密碼' WITH GRANT OPTION;
//其它操作權限說明,select查詢 insert插入 delete刪除 update修改
//設置用戶擁有查詢插入的權限
grant select,insert on *.* to '新用戶名'@'指定ip' identified by '新用戶密碼' WITH GRANT OPTION;
//取消用戶查詢的查詢權限
REVOKE select ON what FROM '新用戶名';

 

5.刪除用戶

DROP USER username@localhost;

 

6.修改后刷新權限

FLUSH PRIVILEGES;

 


免責聲明!

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



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