gpt4 book ai didi

mysql - 我安装了mysql并设置了root密码但无法登录,为什么?

转载 作者:行者123 更新时间:2023-11-29 16:42:52 26 4
gpt4 key购买 nike

我搜索了很多类似的问题,并且我已经重置了密码。我很好奇,想知道为什么会发生这种情况。大多数问题都与远程登录和匿名用户有关,我遇到的情况有所不同。

安装后,我执行了

$ mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

$ sudo service mysql start
* Starting MySQL database server mysqld
No directory, logging in with HOME=/

$ sudo mysql_secure_installation
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: n
Please set the password for root here.

New password:

Re-enter new password:
...


Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

然后,我尝试登录并得到了

~$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

我确信我使用了正确的密码。

这是用户表:

+------------------+-----------+-----------------------+
| User | host | plugin |
+------------------+-----------+-----------------------+
| root | localhost | auth_socket |
| mysql.session | localhost | mysql_native_password |
| mysql.sys | localhost | mysql_native_password |
| debian-sys-maint | localhost | mysql_native_password |
+------------------+-----------+-----------------------+

有人可以解释一下吗?

最佳答案

只需按照以下步骤重置密码即可。

来源 - MYSQL Reference Manual

1.) 以运行 MySQL 服务器的 Unix 用户身份登录系统(例如 mysql)。

  • 如果 MySQL 服务器正在运行,请将其停止。找到包含服务器进程 ID 的 .pid 文件。该文件的确切位置和名称取决于您的发行版、主机名和配置。常见位置是/var/lib/mysql/、/var/run/mysqld/和/usr/local/mysql/data/。通常,文件名的扩展名为 .pid,并以 mysqld 或系统的主机名开头。
  • 通过向 mysqld 进程发送正常终止(不是终止 -9)来停止 MySQL 服务器。在以下命令中使用 .pid 文件的实际路径名:

    shell> kill cat /mysql-data-directory/host_name.pid1

    在 cat 命令中使用反引号(而不是正引号)。这些导致 cat 的输出被替换为kill 命令。

  • 创建一个文本文件,其中单行包含以下语句。将密码替换为您要使用的密码。
  • SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');1

  • 保存文件。此示例假设您将文件命名为/home/me/mysql-init。该文件包含密码,因此请勿将其保存在其他用户可以读取的位置。如果您没有以 mysql(服务器运行的用户)身份登录,请确保该文件具有允许 mysql 读取它的权限。

  • 使用特殊的 --init-file 选项启动 MySQL 服务器:

  • shell> mysqld --init-file=/home/me/mysql-init &1

    服务器在启动时执行--init-file选项命名的文件内容,更改'root'@'localhost'帐户密码。

    其他选项可能也是必要的,具体取决于您通常启动服务器的方式。例如,在 --init-file 之前可能需要 --defaults-file。

  • 服务器成功启动后,删除/home/me/mysql-init。
  • 您现在应该能够使用新密码以 root 身份连接到 MySQL 服务器。停止服务器并正常重新启动。

    关于mysql - 我安装了mysql并设置了root密码但无法登录,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53256015/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com