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;