一. 创建用户:
命令:CREATE USER ‘username’@’localhost’ IDENTIFIED BY ‘password’;
例子: CREATE USER ‘cat’@’localhost’ IDENTIFIED BY ‘12321’;
PS:username – 你将创建的用户名,
password – 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器。
二.授权:
命令:GRANT privileges ON databasename.tablename TO ‘username’@’localhost’
PS: privileges – 用户的操作权限,如SELECT , INSERT , UPDATE 等(详细列表见该文最后面).如果要授予所的权限则使用ALL.;databasename – 数据库名,tablename-表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示, 如*.*.
例子: GRANT SELECT, INSERT ON cat.* TO ‘cat’@’localhost’;
三.创建用户同时授权
mysql> grant all privileges on cat.* to cat@localhost identified by ‘12321’;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
PS:必须执行flush privileges; 否则登录时提示:ERROR 1045 (28000): Access denied for user ‘user’@’localhost’ (using password: YES )
四.设置与更改用户密码
命令:SET PASSWORD FOR ‘username’@’localhost’ = PASSWORD(‘newpassword’);
例子: SET PASSWORD FOR ‘cat’@’localhost’ = PASSWORD(“123”);
五.删除用户
命令: DROP USER ‘username’@’localhost’;
六.查看用户的授权
mysql> show grants for cat@localhost;
+———————————————+
| Grants for cat@localhost |
+———————————————+
| GRANT USAGE ON *.* TO ‘cat’@’localhost’ |
| GRANT INSERT ON `cat`.* TO ‘cat’@’localhost’ |
+———————————————+
2 rows in set (0.00 sec)
PS:GRANT USAGE:mysql usage权限就是空权限,默认create user的权限,只能连库,啥也不能干