当我尝试连接mysql时,我得到以下错误:

无法通过套接字/var/lib/ MySQL / MySQL连接到本地MySQL服务器。袜子”(2)

有解决这个错误的方法吗?背后的原因可能是什么?


当前回答

我使用MyXQL访问Elixir中的数据库。我的实际错误是使用/tmp/mysql。当/etc/my.conf使用/var/lib/mysql/mysql.sock时。

我的解决方案是在Elixir设置中设置一个环境变量: 出口MYSQL_UNIX_PORT = / var / lib / mysql / mysql。sock,这样MyXQL就不会使用它的默认位置。

这种解决方案的原则是,您可以将MyXQL重定向到另一个位置,以避免这种类型的问题。

其他回答

确保你启动了服务器:

mysql.server start

然后用root用户连接:

mysql -uroot

重现此错误的一种方法:如果您打算连接到外部服务器,但却连接到不存在的本地服务器:

eric@dev ~ $ mysql -u dev -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through 
socket '/var/lib/mysql/mysql.sock' (2)
eric@dev ~ $

所以你必须像这样指定主机:

eric@dev ~ $ mysql --host=yourdb.yourserver.com -u dev -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 235
Server version: 5.6.19 MySQL Community Server (GPL)

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

mysql> show databases;
+-------------------------+
| Database                |
+-------------------------+
| information_schema      |
| mysql                   |
| performance_schema      |
+-------------------------+
3 rows in set (0.00 sec)

mysql> exit
Bye
eric@dev ~ $

确保在/var中有足够的空间。如果Mysql恶魔不能写入额外的信息到驱动器,Mysql服务器将不会启动,这将导致错误无法连接到本地Mysql服务器通过套接字'/var/lib/ Mysql / Mysql。袜子”(2)

考虑使用

expire_logs_days = 10
max_binlog_size = 100M

这将帮助您降低磁盘使用率。

我也有同样的问题,当我研究日志时

Sudo tail -f /var/log/mysqld.log

InnoDB: mmap(137363456 bytes) failed; errno 12
200610  6:27:27 InnoDB: Completed initialization of buffer pool
200610  6:27:27 InnoDB: Fatal error: cannot allocate memory for the buffer pool
200610  6:27:27 [ERROR] Plugin 'InnoDB' init function returned error.
200610  6:27:27 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
200610  6:27:27 [ERROR] Unknown/unsupported storage engine: InnoDB
200610  6:27:27 [ERROR] Aborting

很明显没有足够的内存来启动InnoDB。我意识到我从来没有设置交换文件…

我按照这个设置交换文件,它工作。

MySQL服务器没有运行,或者那不是它的套接字文件的位置(检查my.cnf)。