Get the App
SLTechnology News&Howtos  ›  Database  › 

Modify the password of the user (root) of MySQL under Linux

Shulou Source: shulou.com Published: 2022-06-01 14:02:32 09月29日 Update

Modify the password of the user (root) of MySQL under Linux

1 root password method 1 with old MySQL

Outside of the mysql system, use mysqladmin

# mysqladmin-u root-p password "test123" Enter password: [enter the original password] method 2

By logging in to the mysql system

# mysql-uroot-pEnter password: [enter the original password] mysql > use mysql

Set mode; setting never expires

Mysql > set password=password ('rootroot'); # set set mysql > alter user' root'@'localhost' password expire never;mysql > flush privileges;mysql > exit

Update mode

Mysql > update user set password=password ("test") where user='root'; # update Update mysql > flush privileges;mysql > exit; 2 forget myql's root password

First of all, you must have root permission for the operating system.

Similar to safe mode login system

2.1 close the currently running mysqld service program service mysqld stop (to add mysqld as a system service first) 2.2 mysqld_safe starts the reset password in safe mode

Use the mysqld_safe script to start the mysqld service in safe mode (without loading the authorization table)

Mysqld_safe-- skip-grant-tables & or mysqld_safe-- defaults-file=/etc/my.cnf-- skip-grant-tables &

&, which means to run in the background, but not in the background, then open another terminal.

Log in to the database with a root user with an empty password and reset the password for the ROOT user

# mysql#5.6 and previous mysql > UPDATE mysql.user SET password=password ('test123') WHERE user='root';# 5.7 * * mysql.user table authentication_string field replaced password field; mysql > UPDATE mysql.user SET authentication_string=password (' test123') WHERE user='root'; mysql > flush privileges;mysql > exit

Originally, mysql is case-insensitive, but this is the specific value of the mysql database in the modified mysql, which should be noted.

2.3 reset password by modifying configuration file

Modify the configuration file (/ etc/my.cnf) and add a line under the [mysqld] section: skip-grant-tables=1, so that the password is not verified when mysqld starts.

Vim / etc/my.cnf add: skip-grant-tables=1

Restart the mysql service takes effect. The modification method is the same as above.

After the modification is completed, drop "skip-grant-tables=1" in the [mysqld] section.

2.4 mysql 5.7 change password through alter command

Mysql5.7 generates random passwords by default, which are generated when initializing the mysql configuration

Mysql 5.7Log in to the MySQL service with a temporary password, you must use the alter command to change the password

Mysql > alter user 'root'@'localhost' identified by' rootroot';mysql > flush privileges

Tags: Password system service user security method mode login run configuration background command field section data database file method input incorrect Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL Redmi OPPO Reno macOS