MySQL修改密碼

1、查看MySQL臨時密碼

Linux安裝好MySQL後,爲了增長數據庫的安全性,在安裝時會爲root用戶生成一個臨時的隨機密碼,存放在/var/log/mysqld.log 中。mysql

[root@localhost mysql_bundle]# cat /var/log/mysqld.log | grep temp
2020-05-12T12:05:15.901037Z 1 [Note] A temporary password is generated for root@localhost: .fJJk*j&t4h6
2020-05-12T12:05:18.403371Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables

這個.fJJk*j&t4h6就是臨時密碼。第一次登陸就使用這個密碼。

sql

2、修改密碼

登陸後輸入show databases命令查看數據庫中有哪些庫,發現出現錯誤。數據庫

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>

這條錯誤語句時說必須使用ALTER USER重設密碼才能夠執行這條語句。
那麼接下來就重置密碼吧。
注意:密碼要符合複雜度規則,也就是說要包含大小寫字母、數字、特殊符號,而且長度不小於八位。不然會報錯,以下:

安全

mysql> alter user root@localhost identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>

設置一個合格的密碼。ide

mysql> alter user root@localhost identified by 'MySQLroot123#';
Query OK, 0 rows affected (0.00 sec)

mysql>
  • alter user是命令
  • root@localhost 是用戶名,這是MySQL用戶名的完整格式,@符號前面是用於登陸的用戶名,後面的是主機名,localhost表示root只能在本地登陸。
  • identified by是指定密碼的命令
    使用exit退出系統,以後就能夠使用新密碼登陸MySQL。
[root@localhost mysql_bundle]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye
[root@localhost mysql_bundle]#
相關文章
相關標籤/搜索