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

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

问题描述

我在 Centos7 虚拟机上安装了 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

有两个问题 - 为什么我不能以 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

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

Edit the initial root password on install can be found by running

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 表结构已更改.

所以要重置 root 密码,您仍然使用 --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 on 管理几个 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 root 帐户密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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