1、重設密碼
方法一:mysql
mysql>use mysql /*進入mysql數據庫,存放user表包括user和密碼的庫 密碼字段名稱爲authentication_string(5.7)、password(5.6)*/ mysql>update user set authentication_string=password('newpass') where user='root'; //5.7版本 mysql> update mysql.user set password=PASSWORD(’新密碼’) where User=’root’; //5.6版本 mysql>flush privileges;#刷新一下
方法二:sql
#mysqladmin -u 用戶 password ‘newpassword’ [root@server1 ~]# mysqladmin -u lisi -p password '123456'; Enter password: #這裏輸入原密碼 mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety
方法三:數據庫
以root身份進入數據庫 [root@server1 ~]# mysql -uroot -p123456 mysql> SET PASSWORD = PASSWORD('新密碼'); //root用戶 mysql> SET PASSWORD FOR 用戶 = PASSWORD(‘新密碼');//普通用戶
2、忘記密碼
修改mysql的配置文件:socket
vi /etc/my.cnf [mysqld] 模塊下 末尾添加 skip_grant_tables 就是在啓動mysql時不啓動grant-tables,受權表,跳過表的加載
重啓數據庫服務tcp
systemctl restart mysqld
默認密碼恢復爲空,進入數據庫,再修改密碼字段spa
[root@server3 ~]# netstat -anpt | grep 3306 tcp6 0 0 :::3306 :::* LISTEN 1282/mysqld [root@server3 ~]# mysql -u root -p Enter password: #回車直接登陸 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.20 Source distribution Copyright (c) 2000, 2017, 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> update user set authentication_string=password('xzf729') where user='root'; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1
修改完密碼後,再刪除添加的配置文件那一行,重啓服務,rest
vi /etc/my.cnf [client] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysql] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysqld] user = mysql basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 character_set_server=utf8 pid-file = /usr/local/mysql/mysqld.pid socket = /usr/local/mysql/mysql.sock server-id = 1 [root@server1 ~]# systemctl restart mysqld [root@server1 ~]# mysql -u root -p xzf729 #可登陸成功