如何在CentOS7上更改MySQL根帐户密码? [英] How to change the MySQL root account password on CentOS7?

查看:120
本文介绍了如何在CentOS7上更改MySQL根帐户密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 found I have no idea what else to do. Any tips/ideas would be greatly appreciated!

推荐答案

您正在使用哪个版本的mySQL?我使用的是5.7.10,并且以root身份登录时遇到相同的问题

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

有2个问题-为什么我不能以root身份登录,为什么不能使用'mysqld_safe`来启动mySQL重置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密码,但是这是您要重置root密码的操作

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

编辑,可以通过运行

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错误-未安装的原因)

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

user表结构已更改.

因此,要重置根密码,您仍然可以使用--skip-grant-tables选项启动mySQL并更新user表,但是操作方式已更改.

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

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在 几个Linux平台.在这些平台上,mysqld_safe不再是 安装,因为它是不必要的.有关更多信息,请参见部分. 2.5.10,使用systemd管理MySQL服务器".

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=.

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

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

这篇关于如何在CentOS7上更改MySQL根帐户密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆