您正在使用哪个版本的mySQL? 我使用的是5.7.10,并且以root身份登录时遇到了同样的问题
有两个问题-为什么我不能以root身份登录,以及为什么不能使用'mysqld_safe'启动mySQL重置root密码。
我在安装过程中没有设置根密码的答案,但是这是重置根密码的方法
通过运行可以找到安装时编辑初始root密码
grep 'temporary password' /var/log/mysqld.log
[HTTP://Dev.MySQL.com/doc/热烦满/5.7/恩/Linux-installation-郁闷-repo.HTML]
systemctl set-environment MYSQLD_OPTS=现在用于管理mySQL而不是user(这就是为什么会出现-bash: mysqld_safe: command not found错误-未安装的原因)
systemctl set-environment MYSQLD_OPTS=的表结构已更改。
因此,要重置根密码,您仍然可以使用systemctl set-environment MYSQLD_OPTS=选项启动mySQL并更新user表,但是更改方法。
1. Stop mysql:
systemctl stop mysqld
2. Set the mySQL environment option
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
3. Start mysql usig the options you just set
systemctl start mysqld
4. Login as root
mysql -u root
5. Update the root user password with these mysql commands
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword')
-> WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit
*** Edit ***
As mentioned my shokulei in the comments, for 5.7.6 and later, you should use
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
Or you'll get a warning
6. Stop mysql
systemctl stop mysqld
7. Unset the mySQL envitroment option so it starts normally next time
systemctl unset-environment MYSQLD_OPTS
8. Start mysql normally:
systemctl start mysqld
Try to login using your new password:
7. mysql -u root -p
参考
就像在[http://dev.mysql.com/doc/refman/5.7/en/mysqld-safe.html,
注意
从MySQL 5.7.6开始,用于使用RPM安装MySQL 分发,服务器启动和关闭由systemd on管理 几个Linux平台。 在这些平台上,mysqld_safe不再是 安装,因为它是不必要的。 有关更多信息,请参见部分 2.5.10“使用systemd管理MySQL服务器”。
这将带您到[http://dev.mysql.com/doc/refman/5.7/en/server-management-using-systemd.html],其中在页面底部提到了systemctl set-environment MYSQLD_OPTS=。
密码重置命令位于[http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html]的底部