数据库选择

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

  1. # 切换到mysql数据库
  2. use mysql;

image.png

mysql运行原理

image.png

mysql权限级别

image.png
image.png

用户权限列表

对应的权限字段。
image.png

  1. # 查看 root 用户权限
  2. select * from mysql.user where user='root';

user 表的字段。这里我们首先熟悉增删改查等权限,其他的权限了解即可。
image.png

创建用户

数据库默认的 root 账号是超级管理员账户,而我们的操作不应该使用超级管理员账户,需要创建其他普通账户。

  1. # 创建用户
  2. create user 'admin'@'localhost' identified by 'admin123';
  3. # 使用新账号查询
  4. select * from mysql.user;

新账户登录并运行查询,可以看到没有权限。
image.png

通过 mysql 管理工具登录,只有 information_schema 表。
image.png

删除用户

  1. # 删除用户,需要切换root账户
  2. delete from mysql.user where user='admin';
  3. # 需要刷新权限,否则重新创建会报如下错误
  4. # ERROR 1396 (HY000): Operation CREATE USER failed for 'admin'@'localhost'
  5. flush privileges;

image.png

修改用户名

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

image.png

修改用户密码

  1. # 将用户user1密码修改为123456,需要切换root账户
  2. alter user 'user1'@'localhost' identified by '123456';

为了方便查看,这里新开了个窗口来测试登录,如下右图。
image.png

用户授权

  1. # 授权查询权限
  2. grant select on table *.* to 'user1'@'localhost';
  3. # 授权插入权限
  4. grant insert on table *.* to 'user1'@'localhost';
  5. # 授权所有权限
  6. grant all on table *.* to 'user1'@'localhost';

给 user1 授予 查询 权限,授权完成后需要重新登录。
image.png

撤销用户权限

  1. # 撤销查询权限
  2. revoke select on table *.* from 'user1'@'localhost';
  3. # 撤销插入权限
  4. revoke insert on table *.* from 'user1'@'localhost';
  5. # 撤销所有权限
  6. revoke all on table *.* from 'user1'@'localhost';

给 user1 撤销 查询 权限,同样需要重新登录。
image.png

关于忘记root密码问题

image.png

to be continue…