我在 Centos7 虚拟机上安装了 mySQL,但我在使用 root 登录时遇到了问题。我尝试在没有密码的情况下登录或尝试任何默认的(如 mysql、admin 等)我查看了 my.cnf 文件并且没有密码。我尝试通过停止服务并使用 mysqld_safe --skip-grant-tables &
重新启动它来更改密码,但我得到了 mysqld_safe:command not found
我不知道还能做什么。任何提示/想法将不胜感激!
最佳答案
您使用的是什么版本的 MySQL?我正在使用 5.7.10 并且以 root 身份登录时遇到了同样的问题
有两个问题:无法以root
身份登录,无法使用mysqld_safe
启动MySQL重置root
密码。
我无法在安装过程中设置 root 密码,但这是重置 root 密码的方法
编辑安装时的初始root密码可以通过运行找到
grep 'temporary password' /var/log/mysqld.log
http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html
systemd
现在用于管理 mySQL 而不是mysqld_safe
(这就是为什么你得到-bash: mysqld_safe: command not found
错误 - 未安装)user
表结构已更改。
所以要重置 root 密码,您仍然使用 --skip-grant-tables
选项启动 mySQL 并更新 user
表,但是您的操作方式已经改变.
1. Stop mysql:
sudo systemctl stop mysqld
2. Set the mySQL environment option
sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
3. Start mysql usig the options you just set
sudo 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
sudo systemctl stop mysqld
7. Unset the mySQL envitroment option so it starts normally next time
sudo systemctl unset-environment MYSQLD_OPTS
8. Start mysql normally:
sudo 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 所说,
Note
As of MySQL 5.7.6, for MySQL installation using an RPM distribution, server startup and shutdown is managed by systemd on several Linux platforms. On these platforms, mysqld_safe is no longer installed because it is unnecessary. For more information, see Section 2.5.10, “Managing MySQL Server with systemd”.
这会将您带到 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 的底部
关于mysql - 如何在 CentOS7 上更改 MySQL root 帐户密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33510184/