运维工程师常用的Mysql语句——2.mysql修改root密码以及密码重置

  1. 用SET PASSWORD命令
首先登录MySQL。 
格式:mysql> set password for 用户名@localhost = password('新密码'); 
例子:mysql> set password for root@localhost = password('123456'); 
  1. 用mysqladmin
格式:mysqladmin -u用户名 -p旧密码 password 新密码 
例子:mysqladmin -uroot -p123456 password 123 
  1. 用UPDATE直接编辑user表
首先登录MySQL。 
mysql> use mysql; 
mysql> update user set password=password('123') where user='root' and host='localhost'; 
mysql> flush privileges; 
  1. windows服务器上忘记root密码
关闭正在运行的MySQL服务。 
打开DOS窗口,转到mysql\bin目录。 
输入mysqld --skip-grant-tables 回车。--skip-grant-tables 的意思是启动MySQL服务的时候跳过权限表认证。 
再开一个DOS窗口(因为刚才那个DOS窗口已经不能动了),转到mysql\bin目录。 
输入mysql回车,如果成功,将出现MySQL提示符 >。 
连接权限数据库: use mysql; 。 
改密码:update user set password=password("123") where user="root";(别忘了最后加分号) 。 
刷新权限(必须步骤):flush privileges; 。 
退出 quit。 
注销系统,再进入,使用用户名root和刚才设置的新密码123登录。
  1. Linux服务器上忘记root密码

首先停止mysql服务进程:

service mysqld stop

然后编辑mysql的配置文件my.cnf

vim /etc/my.cnf

找到 [mysqld]这个模块:
在最后面添加一段代码

skip-grant-tables   ##忽略mysql权限问题,直接登录

然后保存 :wq!退出
启动mysql服务:

service mysqld start

直接进入mysql数据库:

Starting MySQL. SUCCESS! 
 [root@web1 ~]# mysql
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 1
 Server version: 5.6.34 Source distribution
  
 Copyright (c) 2000, 2016, 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> 

使用mysql表,然后进行修改mysql的root密码:

mysql> use mysql; ##使用mysql数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=password("123456") where user="root";##更新密码
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0
mysql> flush privileges;##刷新权限
Query OK, 0 rows affected (0.00 sec)

注意:最后将mysql服务进程关闭,把添加过的跳过验证信息删除,要不然对数据库安全威胁太大

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值