MySQL“ERROR 1698(28000):拒绝访问用户'root'@'localhost'”

5

我已经安装了以下版本的MySQL:

kurt@kurt-ThinkPad:~$ mysql -V
mysql  Ver 14.14 Distrib 5.7.15, for Linux (x86_64) using  EditLine wrapper

我记得在安装过程中没有为root用户设置密码。但是,如果我尝试使用mysql命令启动它,会出现以下错误:

kurt@kurt-ThinkPad:~$ mysql
ERROR 1045 (28000): Access denied for user 'kurt'@'localhost' (using password: NO)

同样地,作为超级用户,
kurt@kurt-ThinkPad:~$ mysql -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

我尝试按照重置根密码:Unix和类Unix系统上的说明操作;我在我的主目录中创建了文本文件,然后尝试使用--init-file选项启动MySQL服务器。然而,我再次遇到权限错误:

kurt@kurt-ThinkPad:~$ mysqld_safe --init-file=mysql-init &
[1] 18340
kurt@kurt-ThinkPad:~$ /usr/bin/mysqld_safe: 548: /usr/bin/mysqld_safe: cannot create /var/lib/mysql/mysqld_safe.pid: Permission denied
2016-09-20T14:57:04.753720Z mysqld_safe Logging to '/var/log/mysql/error.log'.
cat: /var/run/mysqld/mysqld.pid: Permission denied
rm: cannot remove '/var/run/mysqld/mysqld.pid': Permission denied
2016-09-20T14:57:04.780206Z mysqld_safe Fatal error: Can't remove the pid file:
/var/run/mysqld/mysqld.pid
Please remove it manually and start /usr/bin/mysqld_safe again;
mysqld daemon not started
/usr/bin/mysqld_safe: 135: /usr/bin/mysqld_safe: cannot create /var/log/mysql/error.log: Permission denied
rm: cannot remove '/var/lib/mysql/mysqld_safe.pid': Permission denied

我在Cant connect to Mysql server ; Can't create/write the pid file中读到了一个类似的错误,其中建议的解决方案是将MySQL用户分配到/var/run/mysqld目录。然而,如果我尝试这样做,我会收到“操作不允许”的提示:

kurt@kurt-ThinkPad:~$ chown mysql:mysql /var/run/mysqld
chown: changing ownership of '/var/run/mysqld': Operation not permitted

有没有关于如何让MySQL正常工作的想法?

你必须以root身份运行系统命令,而不是kurt的身份;-) - Álvaro González
3个回答

8

我注意到一个解决方案是使用sudo运行mysql

kurt@kurt-ThinkPad:~$ sudo mysql
sudo: unable to resolve host kurt-ThinkPad
[sudo] password for kurt: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.15 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

我从https://bbs.archlinux.org/viewtopic.php?id=154010得到了这个建议。


2

在默认安装后,我遇到了同样的错误代码。我正在Ubuntu 18.0.4上运行MariaDB 10.4.7。以下是我如何绕过它:

最初的回答:

sudo mysql -u root
...
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';
Query OK, 0 rows affected (0.005 sec)
MariaDB [(none)]> set password for 'root'@'localhost'=password('');
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> exit

之后,我可以像以前一样运行mysql命令:

最初的回答:

$ mysql -u root
...
MariaDB [(none)]>

请注意,这将覆盖MariaDB 10.4.7的默认安全设置。在测试或开发环境中,这是完全可以的。但在生产环境中,您可能需要三思而后行。我建议创建不同的用户并为其设置密码。

"Original Answer"翻译成中文为"最初的回答"。


0

注意你的插件。如果它是unix-socket并且你遵循选项2,你必须知道谁在使用shell。因为shell的用户是唯一的身份验证方式。即使你使用$ mysql -u root -p,当你的shell用户是其他用户名时,你也无法访问数据库。请参见此处的示例https://mariadb.com/kb/en/authentication-plugin-unix-socket/。我建议按照选项2创建一个用户,但使用插件“mysql_native_password”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接