How to find out the MySQL root password

后端 未结 17 2350
野趣味
野趣味 2020-11-29 15:29

I cannot figure out my MySQL root password; how can I find this out? Is there any file where this password is stored?

I am following this link but I do not have dir

相关标签:
17条回答
  • 2020-11-29 16:06

    System:

    • CentOS Linux 7
    • mysql Ver 14.14 Distrib 5.7.25

    Procedure:

    1. Open two shell sessions, logging in to one as the Linux root user and the other as a nonroot user with access to the mysql command.

    2. In your root session, stop the normal mysqld listener and start a listener which bypasses password authentication (note: this is a significant security risk as anyone with access to the mysql command may access your databases without a password. You may want to close active shell sessions and/or disable shell access before doing this):

      # systemctl stop mysqld
      # /usr/sbin/mysqld --skip-grant-tables -u mysql &

    3. In your nonroot session, log in to mysql and set the mysql root password:

      $ mysql
      mysql> flush privileges;
      Query OK, 0 rows affected (0.00 sec)

      mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
      Query OK, 0 rows affected, 1 warning (0.01 sec)

      mysql> quit;

    4. In your root session, kill the passwordless instance of mysqld and restore the normal mysqld listener to service:

      # kill %1
      # systemctl start mysqld

    5. In your nonroot session, test the new root password you configured above:

      $ mysql -u root -p
      Enter password:
      Welcome to the MySQL monitor. Commands end with ; or \g.
      ...
      mysql>

    0 讨论(0)
  • 2020-11-29 16:07

    As addition to the other answers, in a cpanel installation, the mysql root password is stored in a file named /root/.my.cnf. (and the cpanel service resets it back on change, so the other answers here won't help)

    0 讨论(0)
  • 2020-11-29 16:08

    thanks to @thusharaK I could reset the root password without knowing the old password.

    On ubuntu I did the following:

    sudo service mysql stop
    sudo mysqld_safe --skip-grant-tables --skip-syslog --skip-networking
    

    Then run mysql in a new terminal:

    mysql -u root
    

    And run the following queries to change the password:

    UPDATE mysql.user SET authentication_string=PASSWORD('password') WHERE User='root';
    FLUSH PRIVILEGES;
    

    In MySQL 5.7, the password field in mysql.user table field was removed, now the field name is 'authentication_string'.

    Quit the mysql safe mode and start mysql service by:

    mysqladmin shutdown
    sudo service mysql start
    
    0 讨论(0)
  • 2020-11-29 16:08

    I solved this a different way, this may be easier for some.

    I did it this way because I tried starting in safe mode but cannot connect with the error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

    What I did was to connect normally as root:

    $ sudo mysql -u root
    

    Then I created a new super user:

    mysql> grant all privileges on *.* to 'myuser'@'%' identified by 'mypassword' with grant option;
    mysql> quit
    

    Then log in as myuser

    $ mysql -u myuser -p -h localhost
    

    Trying to change the password gave me no errors but did nothing for me so I dropped and re-created the root user

    mysql> drop user 'root'@'localhost;
    mysql> mysql> grant all privileges on *.* to 'root'@'localhost' identified by 'mypassword' with grant option;
    

    The root user is now working with the new password

    0 讨论(0)
  • 2020-11-29 16:09

    one thing that tripped me up on a new install of mySQL and wonder why I couldn't get the default password to work and why even the reset methods where not working. well turns out that on Ubuntu 18 the most recent version of mysql server does not use password auth at all for the root user by default. So this means it doesn't matter what you set it to, it won't let you use it. it's expecting you to login from a privileged socket. so

    mysql -u root -p
    

    will not work at all, even if you are using the correct password!!! it will deny access no matter what you put in.

    Instead you need to use

    sudo mysql
    

    that will work with out any password. then once you in you need type in

     ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password you want to use';
    

    then log out and now the bloody thing will finally accept your password

    0 讨论(0)
  • 2020-11-29 16:10

    Go to phpMyAdmin > config.inc.php > $cfg['Servers'][$i]['password'] = '';

    0 讨论(0)
提交回复
热议问题