如何修改mysql的root用户密码

本文提供了两种重置MySQL root密码的方法。方法A通过停止服务、启用免密登录并更新密码实现;方法B利用系统预设的用户名和密码登录后修改root密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自boboidea:https://www.boboidea.com/blog/32

方法A:

// 停止mysql服务
sudo service stop mysql
// 启用免密登录
sudo mysqld_safe --skip-grant-tables --skip-networking &
// 然后使用mysql进行无密登录
mysql -uroot

// 登录后使用mysql数据
use mysql
// 更新root用户名密码,这里注意密码字段,5.7及以上版本可能变成了authentication_string了!
// 这里还要注意plugin='mysql_native_password'这个设置,新装的有可能是auth_socket,如果是这个改了密码也登录不了。
update user set password=PASSWORD('yourpass'),plugin='mysql_native_password' where user='root';
// 退出MySQL登录
exit;
// 重新启动mysql服务
sudo service mysql start
// 使用root进行登录
mysql -uroot -p yourpass

方法B:

// 在ubuntu的Debian系统上mysql会分配给系统一个
// 用户来让系统执行特定任务使用, 
// 我们也可以使用这个分配的用户名和密码进行登录,然后修改root密码。可查看/etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = sdjfjsdf
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = sdfsdf
socket   = /var/run/mysqld/mysqld.sock

参考文章:http://www.ghostchina.com/how-to-reset-mysqls-root-password/

关于auth_socket:这个需要自行查询了,看到的文章中的那个链接已经不存在了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值