在 Centos7 上更改 mysql root 密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33510184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:22:44  来源:igfitidea点击:

Change mysql root password on Centos7

mysqlcentos

提问by KeykoYume

I have installed mySQL on a Centos7 vm but I have problems logging in with root. I tried logging in without password or tried any default ones (like mysql, admin etc) I looked in the my.cnf file and there's no password. I tried changing the password by stopping the service and restarting it with mysqld_safe --skip-grant-tables &but I get that mysqld_safe:command not foundI have no idea what else to do. Any tips/ideas would be greatly appreciated!

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

回答by Kevin Jones

What version of mySQL are you using? I''m using 5.7.10 and had the same problem with logging on as root

您使用的是哪个版本的 mySQL?我使用的是 5.7.10 并且在以 root 登录时遇到了同样的问题

There is 2 issues - why can't I log in as root to start with, and why can I not use 'mysqld_safe` to start mySQL to reset the root password.

有两个问题 - 为什么我不能以 root 身份登录开始,为什么我不能使用 'mysqld_safe` 来启动 mySQL 来重置 root 密码。

I have no answer to setting up the root password during installation, but here's what you do to reset the root password

我在安装过程中没有设置 root 密码的答案,但这里是您如何重置 root 密码

Editthe initial root password on install can be found by running

编辑安装时的初始 root 密码可以通过运行找到

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

http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html

http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html



  1. systemdis now used to look after mySQL instead of mysqld_safe(which is why you get the -bash: mysqld_safe: command not founderror - it's not installed)

  2. The usertable structure has changed.

  1. systemd现在用于管理 mySQL 而不是mysqld_safe(这就是您收到-bash: mysqld_safe: command not found错误的原因 - 它未安装)

  2. user表结构发生了变化。

So to reset the root password, you still start mySQL with --skip-grant-tablesoptions and update the usertable, but how you do it has changed.

因此,要重置 root 密码,您仍然可以使用--skip-grant-tables选项启动 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


Reference

参考

As it says at http://dev.mysql.com/doc/refman/5.7/en/mysqld-safe.html,

正如它所说的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”.

笔记

从 MySQL 5.7.6 开始,对于使用 RPM 发行版的 MySQL 安装,服务器启动和关闭由多个 Linux 平台上的 systemd 管理。在这些平台上,不再安装 mysqld_safe,因为它是不必要的。有关更多信息,请参阅第 2.5.10 节,“使用 systemd 管理 MySQL 服务器”。

Which takes you to http://dev.mysql.com/doc/refman/5.7/en/server-management-using-systemd.htmlwhere it mentions the systemctl set-environment MYSQLD_OPTS=towards the bottom of the page.

这会将您带到http://dev.mysql.com/doc/refman/5.7/en/server-management-using-systemd.html,其中提到systemctl set-environment MYSQLD_OPTS=了页面底部的 。

The password reset commands are at the bottom of http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

密码重置命令位于http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html的底部

回答by user48918

I used the advice of Kevin Jones above with the following --skip-networking change for slightly better security:

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

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

[user@machine ~]$ mysql -u root

Then when attempting to reset the password I received an error, but googling elsewhere suggested I could simply forge ahead. The following worked:

然后在尝试重置密码时我收到一个错误,但在其他地方使用谷歌搜索表明我可以简单地继续前进。以下工作:

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

At that point I was able to log in.

那时我能够登录。

回答by Arvind

Use the below Steps to reset the password.

使用以下步骤重置密码。

$ sudo systemctl start mysqld

Reset the MySql server root password.

重置 MySql 服务器 root 密码。

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

Output Something like-:

输出类似-:

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

Use the above password during reset mysql_secure_installation process.

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

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

   Enter password for user root: 

You have successfully reset the root password of MySql Server. Use the below command to check the mysql server connecting or not.

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

$ mysql -u root -p

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

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

回答by Narendra Kumar

All,

全部,

Here a little bit twist with mysql-community-server 5.7 I share some steps, how to reset mysql5.7 root password or set password. it will work centos7 and RHEL7 as well.

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

step1. 1st stop your databases

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

service mysqld stop

service mysqld stop

step2. 2nd modify /etc/my.cnf file add "skip-grant-tables"

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

vi /etc/my.cnf

[mysqld] skip-grant-tables

[mysqld] 跳过授权表

step3. 3rd start mysql

第三步。第三次启动mysql

service mysqld start

service mysqld start

step4. select mysql default database

第四步。选择mysql默认数据库

mysql -u root

mysql>use mysql;

step4. set a new password

第四步。设置新密码

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

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

step5 restart mysql database

step5 重启mysql数据库

service mysqld restart

 mysql -u root -p

enjoy :)

请享用 :)

回答by lauxjpn

For CentOS 7and MariaDB 10.4, I had success with the following commands:

对于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

回答by sumit Jaiswal

Please stop all services MySQL with following command /etc/init.d/mysqld stop After it use this

请使用以下命令停止 MySQL 的所有服务 /etc/init.d/mysqld stop 使用此命令后

mysqld_safe --skip-grant-tables

mysqld_safe --skip-grant-tables

its may work properly

它可以正常工作

回答by Dragos Alexe

For me work like this: 1. Stop mysql: systemctl stop mysqld

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

  1. Set the mySQL environment option systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"

  2. Start mysql usig the options you just set systemctl start mysqld

  3. Login as root mysql -u root

  4. After login I use FLUSH PRIVILEGES; tell the server to reload the grant tables so that account-management statements work. If i don't do that i receive this error trying to update the password: "Can't find any matching row in the user table"

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

  2. 使用刚才设置的选项启动mysql systemctl start mysqld

  3. 以 root 身份登录 mysql -u root

  4. 登录后我使用 FLUSH PRIVILEGES; 告诉服务器重新加载授权表,以便帐户管理语句工作。如果我不这样做,我会在尝试更新密码时收到此错误:“在用户表中找不到任何匹配的行”