1、本地環(huán)境
CentOS Linux release 7.5.1804 (Core)
mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper
2针姿、以root用戶登錄Mysql
mysql -uroot -proot
3融柬、切換到mysql數(shù)據(jù)庫
use mysql
4腾降、添加用戶
//只允許指定ip連接
create user '新用戶名'@'localhost' identified by '密碼';
//允許所有ip連接(用通配符%表示)
create user '新用戶名'@'%' identified by '密碼';
5拴还、為新用戶授權(quán)
//基本格式如下
grant all privileges on 數(shù)據(jù)庫名.表名 to '新用戶名'@'指定ip' identified by '新用戶密碼' ;
//示例
//允許訪問所有數(shù)據(jù)庫下的所有表
grant all privileges on *.* to '新用戶名'@'指定ip' identified by '新用戶密碼' ;
//指定數(shù)據(jù)庫下的指定表
grant all privileges on test.test to '新用戶名'@'指定ip' identified by '新用戶密碼' ;
6援奢、設(shè)置用戶操作權(quán)限
//設(shè)置用戶擁有所有權(quán)限也就是管理員
grant all privileges on *.* to '新用戶名'@'指定ip' identified by '新用戶密碼' WITH GRANT OPTION;
//擁有查詢權(quán)限
grant select on *.* to '新用戶名'@'指定ip' identified by '新用戶密碼' WITH GRANT OPTION;
//其它操作權(quán)限說明,select查詢 insert插入 delete刪除 update修改
//設(shè)置用戶擁有查詢插入的權(quán)限
grant select,insert on *.* to '新用戶名'@'指定ip' identified by '新用戶密碼' WITH GRANT OPTION;
//取消用戶查詢的查詢權(quán)限
REVOKE select ON what FROM '新用戶名';
7驻债、刪除用戶
DROP USER username@localhost;
8、修改后刷新權(quán)限
FLUSH PRIVILEGES;