CentOS7如何修改MySQLroot账号密码?

How to change the MySQL root account password on CentOS7?

我已经在 Centos7 虚拟机上安装了 mySQL,但我无法使用 root 登录。我尝试不使用密码登录或尝试使用任何默认密码(如 mysql、admin 等)我查看了 my.cnf 文件,但没有密码。我尝试通过停止服务并使用 mysqld_safe --skip-grant-tables & 重新启动它来更改密码,但我得到 mysqld_safe:command not found 我不知道还能做什么。任何 tips/ideas 将不胜感激!

请使用以下命令停止所有服务 MySQL /etc/init.d/mysqld 停止 在它使用这个

之后

mysqld_safe --skip-grant-tables

它可能会正常工作

您使用的 mySQL 是什么版本?我正在使用 5.7.10 并且以 root

身份登录时遇到同样的问题

有 2 个问题 - 为什么我不能以 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 table 结构已更改。

因此,要重置 root 密码,您仍然使用 --skip-grant-tables 选项启动 mySQL 并更新 user table,但操作方式已更改。

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

的底部

使用以下步骤重置密码。

$ sudo systemctl start mysqld

重置 MySql 服务器 root 密码。

$sudo grep 'temporary password' /var/log/mysqld.log

输出类似-:

 10.744785Z 1 [Note] A temporary password is generated for root@localhost: o!5y,oJGALQa

在重置 mysql_secure_installation 过程中使用上述密码。

<pre>
    $ sudo mysql_secure_installation
</pre>
   Securing the MySQL server deployment.

   Enter password for user root: 

您已成功重置 MySql 服务器的 root 密码。使用以下命令检查 mysql 服务器是否连接。

$ mysql -u root -p

http://gotechnies.com/install-latest-mysql-5-7-rhelcentos-7/

对我来说是这样工作的: 1.停止mysql: systemctl 停止 mysqld

  1. 设置mySQL环境选项 systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"

  2. 开始mysql使用您刚刚设置的选项 systemctl 启动 mysqld

  3. 以根用户身份登录 mysql -u root

  4. 登录后我使用FLUSH PRIVILEGES;告诉服务器重新加载授权表,以便 account-management 语句起作用。 如果我不这样做,我会在尝试更新密码时收到此错误: "Can't find any matching row in the user table"

全部,

这里对 mysql-community-server 5.7 进行了一些改动 我分享一些步骤,如何重置 mysql5.7 root 密码或设置密码。它也适用于 centos7 和 RHEL7。

第一步。第一次停止你的数据库

service mysqld stop

第二步。第二次修改 /etc/my.cnf 文件添加 "skip-grant-tables"

vi /etc/my.cnf

[mysqld] 跳过授权表

第三步。第三次开始 mysql

service mysqld start

第四步。 select mysql 默认数据库

mysql -u root

mysql>use mysql;

第四步。设置新密码

mysql> update user set authentication_string=PASSWORD("yourpassword") where User='root'

step5 重启mysql数据库

service mysqld restart

 mysql -u root -p

尽情享受吧:)

我使用了上面 Kevin Jones 的建议和以下 --skip-networking 更改以获得更好的安全性:

sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking"

[user@machine ~]$ mysql -u root

然后,当我尝试重置密码时收到错误消息,但谷歌搜索其他地方表明我可以继续前进。以下工作:

mysql> select user(), current_user();
+--------+-----------------------------------+
| user() | current_user()                    |
+--------+-----------------------------------+
| root@  | skip-grants user@skip-grants host |
+--------+-----------------------------------+
1 row in set (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'sup3rPw#'
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'sup3rPw#'
Query OK, 0 rows affected (0.08 sec)

mysql> exit
Bye
[user@machine ~]$ systemctl stop mysqld
[user@machine ~]$ sudo systemctl unset-environment MYSQLD_OPTS
[user@machine ~]$ systemctl start mysqld

那时我可以登录了。

对于 CentOS 7MariaDB 10.4,我成功地执行了以下命令:

su -
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --user=mysql"
systemctl restart mariadb
mysql -u root

flush privileges;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
flush privileges;
quit

systemctl unset-environment MYSQLD_OPTS
systemctl restart mariadb