MySQL如何新增用戶(權限),改密碼,刪除用戶

查看MYSQL數據庫中所有用戶
mysql>use mysql;
mysql>select host, user from user;
或者
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
查看數據庫中具體某個用戶的權限
mysql>show grants for root@localhost;

1、使用 root 管理員登陸 mysql
mysql -uroot -p123456; 或者 mysql -h192.168.1.1 -ua1 -p123456;
2、創建新用戶
create user a1@localhost identified by '123456'; #創建只能本地登陸的用戶
create user a1@% identified by '123456'; #創建可以外網登陸的用戶
'%' - 所有情況都能訪問
‘localhost’ - 本機才能訪問
111.222.33.44 - 指定 ip 才能訪問
注:修改密碼
update mysql.user set password=password('新密碼') where user='user1';
這個時候訪問,是除了默認生成的兩個數據庫,看不到任何其它的數據庫:
3、給某用戶添加權限
用update命令:
mysql>use mysql;
mysql>update user set host = '%' where user = 'a1'; #給a1增加任何ip所有權限

或者用grant 命令:
grant all on 想授權的數據庫. to a1@'%'; #給任何ip所有權限
grant all on 想授權的數據庫.
to a1@localhost; #給localhost所有權限
grant all on 想授權的數據庫. to a1@localhost identified by '123456'; #同時創建用戶並授權 注意:用以上命令授權的用戶不能給其它用戶授權,如果想讓該用戶可以授權,用以下命令:
grant on 想授權的數據庫.
to a1@localhost with grant option;
grant select on 想授權的數據庫.* to a1@localhost identified by '123456'; #給select權限
4、刪除用戶
delete from mysql.user where user='a1';
四、刷新
flush privileges;
分享:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章