我在Ubuntu 12.04 LTS (Precise穿山甲)上安装了LAMP,然后在phpMyAdmin上设置根密码。我忘记密码了,现在无法登录。当我试图通过终端更改密码时,我得到:

错误2002 (HY000):无法通过套接字连接到本地MySQL服务器 ' / var /运行/ mysqld / mysqld。袜子”(2)

我该如何解决这个问题?我无法打开LAMP,卸载它或重新安装它。


当前回答

这个答案与在内存较小的机器上更新到MySQL 5.6有关

我在Debian 8 (Jessie)上从MySQL 5.5升级到5.6时遇到了同样的问题。MySQL没有启动(状态显示为active/exited),简单地让服务MySQL启动不起作用,因为我从/var/logs/ MySQL /error.log日志文件中发现:

InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(136019968 bytes) failed; errno 12
Cannot allocate memory for the buffer pool

内存不够:我只有256 MB的RAM。

在MySQL中有一个设置,performance_schema。默认情况下,它在MySQL 5.5中是关闭的。

https://dev.mysql.com/doc/refman/5.5/en/performance-schema-startup-configuration.html

但在MySQL 5.6中,默认是开启的,只需在/etc/mysql/my.cnf文件中添加以下一行并重新启动,就可以工作了。

performance_schema = off

警告:关闭此设置可能会遇到性能问题,但我猜在开发环境中这不会是问题。

此外,这里有一篇文章,可能有助于配置MySQL使用最小内存,配置MySQL使用最小内存(web存档链接,以防原来的链接在未来过期https://web.archive.org/web/20201112041608/http://www.tocker.ca/2014/03/10/configuring-mysql-to-use-minimal-memory.html)。

其他回答

以我为例,我做了一些研发工作:

我能够连接到MySQL使用

root-debian#mysql -h 127.0.0.1 -u root -p

但是在mysql中-u root -p行不通。

我在my.cnf中没有找到任何绑定地址。所以我注释了参数socket=/var/lib/mysql/mysqld。袜子在我的。cnf导致我登录的问题。

重新启动服务后,一切正常:

root@debian:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.19 MySQL Community Server (GPL)

在我的案例中,这只是因为mysql停止了,因为/etc/mysql/my.cnf中定义的/var/log/mysql文件夹丢失了创建后,我可以启动mysql,它像往常一样运行。

这个答案很可能会被淹没在这里,但也许有人会偶然发现它。

在我的例子中,SELinux阻止用户/应用程序连接到MySQL (MariaDB)服务器套接字。在RHEL上,如果启用了SELinux,请检查/var/log/audit/audit.log。

我也遇到了这个问题,但我刚刚遇到了:

sudo service mysql restart 

这对我很管用。

我看到所有这些答案,但没有一个提供重置密码的选项,没有接受的答案。实际的问题是他忘记了密码,所以他需要重置,而不是像大多数答案所暗示的那样,看看它是否正在运行(安装与否)。


重置密码

遵循以下步骤(如果你真的忘记了密码,这可能会有帮助,你可以随时尝试,即使你现在没有这种情况):

Stop mysql sudo /etc/init.d/mysql stop Or for other distribution versions: sudo /etc/init.d/mysqld stop Start MySQL in safe mode sudo mysqld_safe --skip-grant-tables & Log into MySQL using root mysql -u root Select the MySQL database to use use mysql; Reset the password -- MySQL version < 5.7 update user set password=PASSWORD("mynewpassword") where User='root'; -- MySQL 5.7, mysql.user table "password" field -> "authentication_string" update user set authentication_string=password('mynewpassword') where user='root'; Flush the privileges flush privileges; Restart the server quit Stop and start the server again Ubuntu and Debian: sudo /etc/init.d/mysql stop ... sudo /etc/init.d/mysql start

在CentOS、Fedora和RHEL上:

    sudo /etc/init.d/mysqld stop
    ...
    sudo /etc/init.d/mysqld start

使用新密码登录 Mysql -u root -p 输入新密码,再次享受您的服务器,就像什么都没有发生一样

这是从重置MySQL根密码。