-- 1.创建用户blog,以远程方式登录数据库
create user blog@'10.0.0.%' identified by '12366';
grant all on *.* to blog@'%';
-- 2.只能以本地方式登录数据库
create user blog@'localhost' identified by '12366';
-- 3.查看所有用户信息
select user,host,authentication_string from mysql.user\G;
-- 4.查看当前登录用户信息
select user();
-- 5.修改用户密码
alter user blog@'%' identified by '123456';
flush privileges;
-- 6.锁定用户/解锁
alter user blog@'localhost' account lock;
alter user blog@'localhost' account unlock;
2.重置root密码
-- 1.跳过授权表的方式启动数据库
-- 先停止数据库服务
[root@db01 ~]# /etc/init.d/mysqld stop
-- 数据库启动不加载授权表
[root@db01 ~]# mysqld_safe --skip-grant-tables --skip-networking &
-- 2.刷新授权表然后重置密码
[root@db01 ~]# mysql -uroot
db01 [(none)]> flush privileges;
db01 [(none)]> alter user root@'localhost' identified by '123456';
-- 3.重新启动数据库
-- 先将MySQL服务kill掉,然后以正常的方式启动数据库
[root@db01 ~]# pkill mysql
[root@db01 ~]# /etc/init.d/mysqld start
二、MySQL权限管理
1.查看权限信息
-- 查看所有权限信息
MySQL> show privileges;
-- 查看指定用户权限信息
MySQL> show grants for blog@'%';
2.常见授权表
序号
授权表名称
解释说明
01
user
主要用于存储用户、主机域、密码、加密插件等信息,同时还存储了*.*级别的权限(全局权限)
02
db
主要用于存储用户、主机域等信息,同时还存储了单库级别的权限(某个库权限)
03
tables_priv
主要用于存储用户、主机域等信息,同时还存储了单表级别的权限(某个表权限)
3.用户授权
-- 进行用户授权操作:
MySQL> grant all on * . * to blog@'10.0.0.%';
授权 权限 库信息 表信息 用户 主机域
-- 进行用户权限回收:
MySQL> revoke delete on * . * from blog@'10.0.0.%';
回收 权限 库信息 表信息 用户 主机域
-- 1.表示对所有的数据库和所有的表进行授权
MySQL> grant all on *.*
grant all on * . * to liux@'10.0.0.%';
grant Insert,Delete,Update,Select on * . * to liux@'10.0.0.%';
-- 2.表示对指定的数据库中所有的表进行授权
MySQL> grant all on liux.*
-- 3.表示对指定的数据库中指定的表进行授权
MySQL> grant all on liux.t1
-- 授权等价于root管理用户
MySQL> grant all on *.* to liux@'localhost' identified by '123' with grant option;
MySQL> flush privileges;