How to change the MySQL root account password on CentOS7?

前端 未结 7 746
故里飘歌
故里飘歌 2020-12-12 08:58

I have installed mySQL on a Centos7 vm but I have problems logging in with root. I tried logging in without password or tried any default ones (like mysql, admin etc) I look

7条回答
  •  隐瞒了意图╮
    2020-12-12 09:36

    I used the advice of Kevin Jones above with the following --skip-networking change for slightly better security:

    sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking"
    
    [user@machine ~]$ mysql -u root
    

    Then when attempting to reset the password I received an error, but googling elsewhere suggested I could simply forge ahead. The following worked:

    mysql> select user(), current_user();
    +--------+-----------------------------------+
    | user() | current_user()                    |
    +--------+-----------------------------------+
    | root@  | skip-grants user@skip-grants host |
    +--------+-----------------------------------+
    1 row in set (0.00 sec)
    
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'sup3rPw#'
    ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'sup3rPw#'
    Query OK, 0 rows affected (0.08 sec)
    
    mysql> exit
    Bye
    [user@machine ~]$ systemctl stop mysqld
    [user@machine ~]$ sudo systemctl unset-environment MYSQLD_OPTS
    [user@machine ~]$ systemctl start mysqld
    

    At that point I was able to log in.

提交回复
热议问题