mariadb(MySql)匿名添加root权限账户或者重置root账户密码

880 查看

运行环境

  • 操作系统:Centos7
  • mariadb 版本:mysql Ver 15.1 Distrib 5.5.41-MariaDB, for Linux (x86_64) using readline 5.1

操作步骤

  1. 通过以下命令匿名登录mariadb

    mysqld_safe --skip-grant-tables &
    
  2. 匿名登录后输入如下命令

    user mysql; #使用mysql系统数据库
    
    insert into user(Host,User,Password) values('%','test',PASSWORD('123')); 
    #插入一个名为test的新用帐号为test,密码为123,可根据自身需要自行修改
    
    Update user set Password=PASSWORD('123') where User='root'
    #将root密码改为123,作为重置root密码之用
    
    select * from user; #查看用户是否加入
    
  3. 赋予创建用户所有权限

    此时如果现在使用赋权限语句

    grant all privileges on *.* to test@localhost identified by '123';
    

    会报执行错误,因为用户之前通过命令 --skip-grant-tables & 来登陆数据库的,因此不能使用grant相关命令来进行权限赋值。

    此时应先输入命令

    flush privileges;
    

    之后再输入命令

    grant all privileges on *.* to test@localhost identified by '123';
    

    即可执行成功

    最后输入以下命令来查看用户权限是否变更

    select * from user; #查看用户权限是否变更