我已经在Centos7虚拟机上安装了mySQL,但是我用root登录时遇到了问题。我尝试了不输入密码或使用任何默认密码(如mysql、admin等),我查看了my.cnf文件,发现没有密码。我尝试通过停止服务并使用 mysqld_safe --skip-grant-tables &
重新启动来更改密码,但是我得到了 mysqld_safe:command not found
的错误信息。
我不知道还有什么可以做的。非常感谢任何提示/想法!
我已经在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
现在使用 systemd
来管理 mySQL,而不是 mysqld_safe
(这就是为什么会出现 -bash: mysqld_safe: command not found
错误 - 它没有被安装)
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所述,
注意
从 MySQL 5.7.6 开始,对于使用 RPM 发行版进行的 MySQL 安装,在多个 Linux 平台上,服务器的启动和关闭由 systemd 管理。在这些平台上,不再安装 mysqld_safe,因为它是不必要的。有关更多信息,请参见第2.5.10节“使用 systemd 管理 MySQL 服务器”。
这将带您到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的底部。
skip-grant-tables
时,它又显示"ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
"。我使用的是MariaDB版本:mysql Ver 15.1 Distrib 10.3.17-MariaDB
,这是YUM在CentOS 8上安装的版本。 - PKHunter我使用了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
那时候我能够登录。
使用以下步骤重置密码。
$ 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/
对于 CentOS 7 和 MariaDB 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
10.3
之外,您在其他答案中的评论表明您还在使用CentOS 8,而这显然不是为CentOS 7编写的答案。 (仅供参考,我已经标记了您在此问题的各种答案中的大多数(而不是全部)评论,以进行审查并可能删除。有关更多信息,请阅读行为准则。根据我的个人经验,在撰写有问题的评论之前,通常最好确保血糖足够,并等待几个小时。) - lauxjpn大家好,
这里有一个关于mysql-community-server 5.7的小技巧。我会分享一些步骤,来重置mysql5.7的root密码或设置密码。这适用于centos7和RHEL7。
第一步:停止你的数据库
service mysqld stop
第二步:修改/etc/my.cnf
文件,添加 "skip-grant-tables"。
vi /etc/my.cnf
[mysqld]
skip-grant-tables
步骤3. 启动mysql
service mysqld start
步骤4. 选择mysql默认数据库
mysql -u root
mysql>use mysql;
步骤4. 设置新密码
mysql> update user set authentication_string=PASSWORD("yourpassword") where User='root'
;
步骤5. 从/etc/my.cnf
文件中删除skip-grant-tables
。
步骤6. 重新启动mysql数据库。
service mysqld restart
mysql -u root -p
尽情享受 :)
skip-grant-tables
。它是不安全的,因为它不需要密码。这不是很明智的建议。 - PKHunter对我而言,操作如下: 1. 停止mysql: systemctl stop mysqld
设置mySQL环境选项 systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
使用刚刚设置的选项启动mysql systemctl start mysqld
以root身份登录 mysql -u root
登录后,我使用FLUSH PRIVILEGES;命令告诉服务器重新加载授权表,以使帐户管理语句生效。 如果我不这样做,尝试更新密码时会收到以下错误提示: "Can't find any matching row in the user table"