一、重设密码
方法一:
mysql>use mysql
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;#刷新一下
方法二:
#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(‘新密码');//普通用户
二、忘记密码
修改mysql的配置文件:
vi /etc/my.cnf
[mysqld] 模块下
末尾添加
skip_grant_tables 就是在启动mysql时不启动grant-tables,授权表,跳过表的加载
重启数据库服务
systemctl restart mysqld
默认密码恢复为空,进入数据库,再修改密码字段
[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
修改完密码后,再删除添加的配置文件那一行,重启服务,
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 #可登录成功



