国画 發表於 2025-12-6 21:57:00

Ubuntu下,MySQL密码遗失时修改密码

<h3>问题背景</h3>
<p>MySQL密码连接不上,需要重置密码</p>
<h3>解决方案</h3>
<p>由于修改密码本身需要连接到数据库,因此需要另一个系统账号来登录数据库。</p>
<p>下面是相关步骤:</p>
<p>1.&nbsp;首先执行以下命令</p>
<pre class="language-bash highlighter-hljs"><code>sudo cat /etc/mysql/debian.cnf</code></pre>
<p>获取到系统账号的密码<span style="color: rgba(224, 62, 45, 1)"><strong>(注意,禁止修改这个文件!!!)</strong></span></p>
<p><span style="color: rgba(224, 62, 45, 1)"><strong><span style="color: rgba(0, 0, 0, 1)"><img src="https://img2024.cnblogs.com/blog/2971310/202512/2971310-20251206214849924-863119277.png"></span></strong></span></p>
<p><span style="color: rgba(224, 62, 45, 1)"><span style="color: rgba(0, 0, 0, 1)">2. 再输入以下命令</span></span></p>
<pre class="language-bash highlighter-hljs"><code>mysql -u debian-sys-maint -p</code></pre>
<p>密码即刚才截图中圈出的部分,<strong>不同机器密码不同,要按照实际的填写!</strong></p>
<p>3. 此时进入mysql的命令行模式,分别输入以下命令(本文以将密码设置为root为例,第三条命令设置报错请移到文章最后)</p>
<pre class="language-bash highlighter-hljs"><code>use mysql;
update user set plugin="mysql_native_password";
update mysql.user set authentication_string=password('root') where user='root' and Host ='localhost';
flush privileges;
exit;</code></pre>
<p>4. 最后重启mysql,再连接mysql,验证是否修改成功</p>
<pre class="language-bash highlighter-hljs"><code>sudo service mysql restart
mysql -u root -p</code></pre>
<p>&nbsp;</p>
<p>若在上述第三步修改密码时返回ERROR 1819 (HY000): Your password does not satisfy the current policy requirements,则说明由于密码不符合mysql密码策略,需要临时调整策略,或者使用符合策略的密码,前者操作步骤如下:</p>
<p>1. 在mysql命令行模式下,执行以下代码,查看mysql的当前密码策略</p>
<pre class="language-bash highlighter-hljs"><code>SHOW VARIABLES LIKE 'validate_password%';</code></pre>
<p><img src="https://img2024.cnblogs.com/blog/2971310/202512/2971310-20251206215200531-1598095437.png"></p>
<p class="auto-cursor-target">其中的参数说明:</p>
<p class="auto-cursor-target">validate_password_check_user_name:默认为OFF,设置为ON的时候表示不能将密码设置成当前用户名或反向用户名,大小写敏感(即以root账号连接时不可设置密码为root或toor,但是可以设置为Root,无论被设置的是哪个账号)</p>
<p class="auto-cursor-target">validate_password_dictionary_file:默认为空,用于检查密码的字典文件的路径名</p>
<p class="auto-cursor-target">validate_password_length:默认为8,密码的最小长度,也就是说密码长度必须大于或等于该值</p>
<p class="auto-cursor-target">validate_password_mixed_case_count:默认为1,如果密码策略是中等或更强的,validate_password要求密码具有的小写和大写字符的最小数量。对于给定的这个值密码必须有那么多小写字符和那么多大写字符</p>
<p class="auto-cursor-target">validate_password_number_count:默认为1,密码必须包含的数字个数</p>
<p class="auto-cursor-target">validate_password_policy:默认为MEDIUM,密码强度检验等级,可以使用数值0、1、2或相应的符号值LOW、MEDIUM、STRONG来指定。(0/LOW:只检查长度;1/MEDIUM:检查长度、数字、大小写、特殊字符;2/STRONG:检查长度、数字、大小写、特殊字符、字典文件)</p>
<p class="auto-cursor-target">validate_password_special_char_count:默认为1,密码必须包含的特殊字符个数</p>
<p class="auto-cursor-target">注:validate_password_check_user_name和validate_password_policy的校验是相互独立的</p>
<p class="auto-cursor-target">2. 设置对应的策略值,如降低最小长度和校验强度等级</p>
<pre class="language-bash highlighter-hljs"><code>set global validate_password_length=4;
set global validate_password_policy=LOW;</code></pre>
<p class="auto-cursor-target">3. 再次执行之前失败的更改密码语句即可成功</p>
<p class="auto-cursor-target">4. 重启mysql服务之后,修改的策略会自动还原为默认值</p>

</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:SoulTraitor,转载请注明原文链接:https://www.cnblogs.com/soultraitor/p/19316697</p><br><br>
来源:https://www.cnblogs.com/soultraitor/p/19316697
頁: [1]
查看完整版本: Ubuntu下,MySQL密码遗失时修改密码