mysql创建用户
新建普通用户有两种方法:
- 使用create user或grant语句
- 直接操作授权表(不建议用这个)
1.create user创建用户
使用create创建的用户没有任何权限,还需要用grant赋予权限
mysql> create user tom; #创建的tom用户没密码,默认登录是'%'
mysql> create user bob@'localhost' identified by '1'; #创建bob用户仅在本地登录
2.grant创建用户
grant不仅可以创建用户还能赋权
使用grant语句的时候,用户需要有grant的权限凹
语法如下:
grant privileges on db.table to user@host [identified by '密码'] [with grant option];
例如:使用grant创建用户testUser,密码是testpwd,并授予该用户对所有数据表select和update权限
mysql> grant select,update on *.* to 'testUser'@'localhost' identified by '1';
mysql> show grants for testUser@localhost;
+-------------------------------------------------------+
| Grants for testUser@localhost |
+-------------------------------------------------------+
| GRANT SELECT, UPDATE ON *.* TO 'testUser'@'localhost' |
+-------------------------------------------------------+
mysql> select Host,User,Select_priv,Update_priv from mysql.user where user='testUser';
+-----------+----------+-------------+-------------+
| Host | User | Select_priv | Update_priv |
+-----------+----------+-------------+-------------+
| localhost | testUser | Y | Y |
+-----------+----------+-------------+-------------+
3.直接操作授权表
添加用户相当于向mysql.user下插入了一条记录而已,这里掠过
4.删除用户
例如:使用delete删除用户’tom’@‘localhost’
mysql> drop user tom;
mysql> delete from mysql.user where host='localhost' and user='tom';