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

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

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


当前回答

我也有同样的问题。我找到了这个。

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’

这是因为在启动MySQL客户端之前没有运行mysqld守护进程。文件/var/lib/mysql/mysql.在运行MySQL的第一个实例时,将自动创建sock。

修复:

首先启动MySQL守护进程,然后输入MySQL:

/etc/init.d/mysqld start
mysql

修改MySQL Root密码

默认情况下,MySQL数据库的root密码为空。从安全角度考虑,将MySQL根密码更改为新密码是一个好主意。

mysql> USE mysql;
mysql> UPDATE user SET Password=PASSWORD('newpassword') WHERE user='root';
mysql> FLUSH PRIVILEGES;

完成后,通过登录进行检查:

mysql -u root -p
Enter Password: <your new password>

其他回答

为了防止出现这个问题,您必须从命令行优雅地关闭服务器,而不是直接下电服务器。

shutdown -h now

这将在关闭机器之前停止正在运行的服务。

基于Centos,当你遇到这个问题时,一个额外的方法是移动mysql.sock:

mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak

service mysqld start

重新启动服务将创建一个名为mqsql.sock的新条目

This error can also occur if you try to change the directory where the database is stored, but imput the wrong directory in the configuration file (like a typo in the second drive as D instead of the accurate D_). Instead of telling you the typo directory does not exist, it will tell you that you lack permission to access it (leading you to try to change the permissions for the typo directory, which it will let you do). So if you get this error while changing directories, double check the configuration file and make sure you don't have a typo.

打开终端,输入:

sudo apt-get purge mysql-client-core-5.6

sudo apt-get autoremove

sudo apt-get autoclean

sudo apt-get install mysql-client-core-5.5

sudo apt-get install mysql-server  

MySQL数据库核心客户端和MySQL服务器包将是相同的5.5版本。MySQL Client 5.5和MySQL Server 5.5是Ubuntu 14.04中这些包的当前“最佳”版本,由包维护者决定。

如果你想安装MySQL Client 5.6和MySQL Server 5.6,你也可以在Ubuntu软件中心找到MySQL - Client -core-5.6和MySQL - Server -5.6包。重要的是客户端和服务器版本号在两种情况下都要匹配。

这对我很管用。

试试这个:

mysql -h 127.0.0.1 -P 3306 -u root -p <database>

另外(看看它是否在运行):

telnet 127.0.0.1 3306 

可能只是my.cnf文件中的错误配置,在/etc/某处(取决于Linux发行版)。

在我的情况下,我不能访问mysql,经过3天的研究,我有一个解决方案。这是一个完美的解决方案,因为我已经搜索了/var/run/mysqld/mysqld.我和袜子没有找到文件夹。可以在putty上执行以下命令。

sudo mkdir /var/run/mysqld/
sudo chown -R mysql:mysql /var/log/mysql
sudo mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --socket=/var/run/mysqld/mysqld.sock
sudo /etc/init.d/mysql restart

你会节省宝贵的时间。