MySQL 5.6 root密码初始化
从MySQL 5.6.6以后,安装rpm之后,root用户会被分配一个随机密码,同时在mysql.user表中新增了一个password expired字段,任何客户端首次连接后必须首先修改密码才能做下面的操作。这是因为在rpm的spec脚本中,初始化数据库的脚本改为/usr/bin/mysql_install_db --rpm --user=mysql --random-passwords
·
从MySQL 5.6.6以后,安装rpm之后,root用户会被分配一个随机密码,同时在mysql.user表中新增了一个password expired字段,任何客户端首次连接后必须首先修改密码才能做下面的操作。这是因为在rpm的spec脚本中,初始化数据库的脚本改为
/usr/bin/mysql_install_db --rpm --user=mysql --random-passwords
新增了--random-passwords参数
ERROR 1862 (HY000): Your password has expired. To log in you must
change it using a client that supports expired passwords.
首先提取密码
但是发现执行下面的命令时
Indicate to the server that the client is can handle sandbox mode if the account used to connect has an expired password. This can be useful for noninteractive invocations of mysql because normally the server disconnects noninteractive clients that attempt to connect using an account with an expired password. (See Section 6.3.6, “Password Expiration and Sandbox Mode”.) This option was added in MySQL 5.6.12.
执行
/usr/bin/mysql_install_db --rpm --user=mysql --random-passwords
新增了--random-passwords参数
按照官网说明,随机密码保存在~/.mysql_secret,这个由安装rpm的用户决定,使用kickstart安装时,文件放在/tmp/目录下。文件内容如下:
# The random password set for the root user at Thu Sep 25 03:30:31 2014 (local time): 9udLegnPieCdVC8m
通过客户端连接mysql服务器时,也应该使用对应的版本,老的版本可能由于不支持过期密码而无法连接,出现类似下面的错误:ERROR 1862 (HY000): Your password has expired. To log in you must
change it using a client that supports expired passwords.
通过对应的mysql客户端连接后,执行SET PASSWORD命令可以重新设置root用户密码,expired字段变为N。
首先提取密码
pass=$(awk '/password/{print $NF}' /tmp/.mysql_secret)
就是取最后一列。
但是发现执行下面的命令时
echo "SET PASSWORD = PASSWORD('');" | mysql -u root --password='u0jt49zWghGMOalT'
仍然回显上面提到的错误信息,查询man可以发现新版客户端增加了一个--connect-expired-password参数
Indicate to the server that the client is can handle sandbox mode if the account used to connect has an expired password. This can be useful for noninteractive invocations of mysql because normally the server disconnects noninteractive clients that attempt to connect using an account with an expired password. (See Section 6.3.6, “Password Expiration and Sandbox Mode”.) This option was added in MySQL 5.6.12.
执行
echo "SET PASSWORD = PASSWORD('');" | mysql -u root --password='u0jt49zWghGMOalT' -b --connect-expired-password
OK, 密码重置成功。
mysqladmin -u root --password='u0jt49zWghGMOalT' password ''
更多推荐
已为社区贡献1条内容
所有评论(0)