我尝试访问和备份 mySql 数据库,但没有成功。我知道 root 密码,但如果我尝试使用它登录数据库,我会得到一个
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
错误。
我尝试按照不同的教程来更改 mySql 根密码,但没有成功:
- 尝试在安全模式下重新启动 mySql 并进入
ALTER
sys 表 - 尝试使用 mysqladmin 更改密码(如解释这里)
- 尝试使用配置文件,
mysqld --init-file=./mysql-init &
它们都因相同的错误而失败。我的计划是在关闭服务器之前备份数据库,但由于无法登录,我无法备份任何内容。
我发现 root 密码有问题,因为我注意到我无法使用 phpMyAdmin 或 Webmin 登录数据库。所有其他数据库用户(为不同应用程序创建的用户)似乎都在工作,因为我没有看到我的前端出现任何问题。
还有其他测试可以恢复密码吗?或者,在删除所有内容之前,有没有什么简单的方法可以备份我的数据?
系统:Ubuntu 18.04.6 LTS (GNU/Linux 4.15.0-193-generic x86_64)
Mysql 版本:mysql Ver 14.14 Distrib 5.7.39, for Linux (x86_64) using EditLine wrapper
提前谢谢你,S。
更新
尝试@user9517 在其评论中建议的方法,结果是:
# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
Error: Access denied for user 'root'@'localhost' (using password: YES)
# mysql --protocol=tcp -uroot -pPASSWORD
mysql: [Warning] Using a password on the command line interface can be insecure.
(and terminal freezed)
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (110)
更新2
--skip-grant-tables
在手动创建/var/run/mysqld
系统中缺少的文件后,我能够使用该选项登录。我已经能够更新密码并重新启动系统。但我仍然无法以 身份登录我的数据库root
。我猜问题不在于密码,而在于某些配置。如果我检查表的内容user
,我可以看到此输出:
SELECT * FROM user WHERE User = 'root'
+-----------+------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
| Host | User | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin | authentication_string | password_expired | password_last_changed | password_lifetime | account_locked |
+-----------+------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
| * | root | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | N | 2018-01-10 10:58:05 | NULL | N |
| 127.0.0.1 | root | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | N | 2018-01-10 10:58:05 | NULL | N |
| ::1 | root | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | N | 2018-01-10 10:58:05 | NULL | N |
+-----------+------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
关于我可以检查什么来解决这个问题有什么想法吗?
更新 3
奇怪的是,如果我尝试通过 ssh 使用 Sequel Pro 连接到服务器,我可以连接、查看表格并导出它们......
答案1
我使用的是 CentOS Linux 版本 7.9.2009 (核心) - MySQL Ver 14.14 Distrib 5.7.36。MYSQLD_OPTS 是关键。
systemctl stop mysqld
systemctl set-environment MYSQLD_OPTS=--skip-grant-tables
systemctl start mysqld
mysql
mysql> UPDATE mysql.user SET authentication_string = PASSWORD ('ENTER_NEW_PASSWORD') WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit
systemctl stop mysqld
systemctl unset-environment MYSQLD_OPTS
systemctl start mysqld
# test #
mysql -u root@localhost -p
希望这能在您的环境中发挥作用。
答案2
对于 AWS Linux systemctl stop mysqld systemctl set-environment MYSQLD_OPTS=--skip-grant-tables systemctl start mysqld mysql mysql> UPDATE mysql.user SET authentication_string = PASSWORD ('ENTER_NEW_PASSWORD') WHERE User = 'root' AND Host = 'localhost'; mysql> FLUSH PRIVILEGES; mysql> quit systemctl stop mysqld systemctl unset-environment MYSQLD_OPTS systemctl start mysqld
mysql -u root@localhost -p