mysql - 如何在 CentOS7 上更改 MySQL root 帐户密码?

标签 mysql centos

我在 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


  1. systemd 现在用于管理 mySQL 而不是 mysqld_safe (这就是为什么你得到 -bash: mysqld_safe: command not found 错误 - 未安装)

  2. 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/

相关文章:

mysql - 在设置具有外部表引用的数据库时,我应该注意哪些注意事项?

linux - Gradle 不显示任何控制台输出

bash - 使用 sed 将条件字符串附加到文件

php - 如何让服务器从另一个目录加载php?

node.js - ./configure 在 node.js tar 中不存在

php - SQL/PHP - 计算结果并根据 HAVING 子句显示它们

php - 日期未插入Android应用程序的数据库中

PHP循环充当cronjob[确保只有一个实例运行]

mysql - 检查一个数据是否在mysql中两个日期字段的范围内

mysql - 如何在 Linux 终端中获取 sql 查询的输出?