Mysql database installation and password modification and the method after forgetting the password

  • Prerequisite: MySQL version 5.7, Linux version Red Hat centos7
  • Goal: Build a MYSQL database server and set the password for the local login of the database administrator to 123456
  • 1. Installation is omitted
    2. Check installation
    rpm -qa | grep 'mysql'
    Check whether to install
    rpm -qc mysqld 

     

  • View all configuration files of the service
    cat /etc/my.cnf 
    (Different versions, various parameters defined in the configuration file will be different) 

  • datadir=/var/lib/mysql ----find the default data storage location
    log-error=/var/log/mysqld.log ----find the default log
    ...

3. Start the service 

systemctl start mysqld


4. View the password (version 5.7 is stored in the log by default) 

grep 'password' /var/log/mysqld.log
password is generated for root@localhost: y2d>EdirOfVs

Use the initial password to log in, remember that the password is random

password is generated for root@localhost: y2d>EdirOfVs

5. Change the password (if you remember the initial password, at least the security is high, haha)
Method 1:

The policy is set to 0, which means only check the password length

mysql> set global validate_password_policy=0;

The default password length is 6

mysql> set global validate_password_length=6;

Modification completed

mysql> alter user root@"localhost" identified by '123456'; 

Method 2: This method to set a new password must follow the policy 1,
0/LOW: only check the length;
1/MEDIUM: check the length, numbers, uppercase and lowercase, special characters;
2/STRONG: check the length, numbers, uppercase and lowercase, special characters Dictionary file.

mysqladmin -hlocalhost -uroot -p password "新密码" enter password: 原密码

6. Forgot your password? Don't worry and look down

Turn off the service first

systemctl stop mysqld

 Modify the configuration file

vim /etc/my.cnf
[mysqld]
skip-grant-tables   添加这行

Restart service

systemctl restart mysqld 

7. Log in and set a new password
to go directly in

#mysql
mysql> update mysql.user set authentication_string=password('654321') where user='root' and Host = 'localhost';
mysql> flush privileges;
mysql> quit;

Exit mysql, modify the configuration file again

# vim /etc/my.cnf

[mysqld]
#skip-grant-tables 把之前修改的注释掉,或删除

Modify the configuration file and remember to restart

#systemctl restart mysqld 

log in

#mysql -uroot -p'654321'
mysql>

I saw a blog on 51cto. It feels like it is well written. Share it with you to prevent it from being found when you use it in the future.

Attach the link: http://blog.51cto.com/13582190/2066088

Guess you like

Origin blog.csdn.net/qq_41419761/article/details/85052158