数据库选择
在上一节中,当安装完 mysql 数据库时会有很多的数据库,我们要使用前需要先选择数据库后才能进行操作。
# 切换到mysql数据库use mysql;

mysql运行原理

mysql权限级别


用户权限列表
对应的权限字段。
# 查看 root 用户权限select * from mysql.user where user='root';
user 表的字段。这里我们首先熟悉增删改查等权限,其他的权限了解即可。
创建用户
数据库默认的 root 账号是超级管理员账户,而我们的操作不应该使用超级管理员账户,需要创建其他普通账户。
# 创建用户create user 'admin'@'localhost' identified by 'admin123';# 使用新账号查询select * from mysql.user;
新账户登录并运行查询,可以看到没有权限。
通过 mysql 管理工具登录,只有 information_schema 表。
删除用户
# 删除用户,需要切换root账户delete from mysql.user where user='admin';# 需要刷新权限,否则重新创建会报如下错误# ERROR 1396 (HY000): Operation CREATE USER failed for 'admin'@'localhost'flush privileges;

修改用户名
# 修改用户名admin为user1,需要切换root账户rename user 'admin'@'localhost' to 'user1'@'localhost';

修改用户密码
# 将用户user1密码修改为123456,需要切换root账户alter user 'user1'@'localhost' identified by '123456';
为了方便查看,这里新开了个窗口来测试登录,如下右图。
用户授权
# 授权查询权限grant select on table *.* to 'user1'@'localhost';# 授权插入权限grant insert on table *.* to 'user1'@'localhost';# 授权所有权限grant all on table *.* to 'user1'@'localhost';
给 user1 授予 查询 权限,授权完成后需要重新登录。
撤销用户权限
# 撤销查询权限revoke select on table *.* from 'user1'@'localhost';# 撤销插入权限revoke insert on table *.* from 'user1'@'localhost';# 撤销所有权限revoke all on table *.* from 'user1'@'localhost';
给 user1 撤销 查询 权限,同样需要重新登录。
关于忘记root密码问题

to be continue…
