1、本地環境
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數據庫
use mysql
4、添加用戶
//只允許指定ip連接
create user '新用戶名'@'localhost' identified by '密碼';
//允許所有ip連接(用通配符%表示)
create user '新用戶名'@'%' identified by '密碼';
5、為新用戶授權
//基本格式如下
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 '新用戶密碼' ;
6、設置用戶操作權限
//設置用戶擁有所有權限也就是管理員
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 '新用戶名';
7、刪除用戶
DROP USER username@localhost;
8、修改后刷新權限
FLUSH PRIVILEGES;