我有一个大问题试图连接到mysql。当我跑步时:

/usr/local/mysql/bin/mysql start

我有以下错误:

Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)

我有mysql。Sock在/var/mysql目录下。

在/etc/my.cnf中,我有:

[client]
port=3306
socket=/var/mysql/mysql.sock

[mysqld]
port=3306
socket=/var/mysql/mysql.sock
key_buffer_size=16M
max_allowed_packet=8M

在/etc/php.ini中有:

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysql.default_socket = /var/mysql/mysql.sock

我已经使用sudo /opt/local/apache2/bin/apachectl restart重启apache

但还是有误差。

否则,我不知道这是否相关,但当我执行mysql_config——sockets时,我得到

--socket         [/tmp/mysql.sock]

当前回答

我有这个套接字错误,它基本上归结为MySQL没有运行。如果您运行一个新的安装,请确保您安装了1)系统包和2)面板安装程序(mysql.prefPane)。面板安装程序将允许你进入你的系统首选项并打开MySQL,然后让一个实例运行。

请注意,在重新安装时,我需要重置我的计算机以使更改正确生效。在重新启动之后,我得到了一个新的实例,并且能够毫无问题地打开到localhost的连接。

同样值得注意的是,我显然已经安装了以前版本的MySQL,但已经删除了面板,这使得它很容易为mac用户运行MySQL实例。

关于这个重新安装的过程有一个很好的链接:http://www.coolestguyplanettech.com/how-to-install-php-mysql-apache-on-os-x-10-6/

其他回答

当您使用localhost连接到MySQL时,操作系统使用套接字连接器。但是,如果您使用127.0.0.1 IP地址,操作系统将使用TCP/IP连接器。因此,当您遇到套接字连接器的问题时,一个可能的解决方案是通过指定127.0.0.1 IP地址而不是localhost来尝试使用TCP/IP建立连接。

我今天遇到了这个问题。这些答案都不能解决问题。我需要做以下命令(在这里找到https://stackoverflow.com/a/20141146/633107)为我的mysql服务启动:

sudo /etc/init.d/mysql stop
cd /var/lib/mysql/
ls ib_logfile*
mv ib_logfile0 ib_logfile0.bak
mv ib_logfile1 ib_logfile1.bak
... etc ...
/etc/init.d/mysql restart

这部分是由/var/log/mysql/error.log中的以下错误指示的:

140319 11:58:21 InnoDB: Completed initialization of buffer pool
InnoDB: Error: log file ./ib_logfile0 is of different size 0 50331648 bytes
InnoDB: than specified in the .cnf file 0 5242880 bytes!
140319 11:58:21 [ERROR] Plugin 'InnoDB' init function returned error.
140319 11:58:21 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
140319 11:58:21 [ERROR] Unknown/unsupported storage engine: InnoDB
140319 11:58:21 [ERROR] Aborting

我还看到磁盘已满错误,但只有在运行没有sudo的命令时。如果权限检查失败,则报告磁盘已满(即使您的分区还没有接近满)。

你可以通过指定mysql服务器的位置来启动mysql服务器。Sock文件使用——socket选项

mysql --socket=/var/mysql/mysql.sock 

即使套接字文件的位置指定在my.cnf文件的不同位置,这也可以工作。

如果有人在寻找初始化错误

无法连接数据库/var/run/mysql/mysql.sock

当运行民事管理和drupal时,只需在

settings。php和civil。settings。php

只有在你试图建立数据库连接的地方

Localhost到127.0.0.1 #本地安装

当我试图启动服务器时,我也遇到了这个问题,这里的许多答案只是说启动服务器不起作用。你可以做的第一件事是执行以下命令,看看是否有任何配置错误:

/usr/sbin/mysqld --verbose --help 1>/dev/null

我确实出现了一个错误:

160816 19:24:33 [Note] /usr/sbin/mysqld (mysqld 5.5.50-0ubuntu0.14.04.1-log) starting as process 9461 ...
160816 19:24:33 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead.
160816 19:24:33 [Note] Plugin 'FEDERATED' is disabled.
160816 19:24:33 [ERROR] /usr/sbin/mysqld: unknown variable 'innodb-online-alter-log-max-size=4294967296'
160816 19:24:33 [ERROR] Aborting

简单的grep -HR "innodb-online-alter-log-max-size" /etc/mysql/显示了包含违规行文件的文件,所以我从文件中删除了这一行。

然后,检查我的/var/log/mysql/error.log文件:

InnoDB: Error: log file ./ib_logfile0 is of different size 0 5242880 bytes
InnoDB: than specified in the .cnf file 0 671088640 bytes!
160816 22:46:46 [ERROR] Plugin 'InnoDB' init function returned error.
160816 22:46:46 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
160816 22:46:46 [ERROR] Unknown/unsupported storage engine: InnoDB
160816 22:46:46 [ERROR] Aborting

基于这个问题,接受的解决方案不会工作,因为我甚至无法启动服务器,所以我按照一些评论说的,删除了我的/var/lib/mysql/ib_logfile0和/var/lib/mysql/ib_logfile1文件。

这允许服务器启动,我能够连接和执行查询,但是检查我的错误日志文件,它很快就被这样的数万行填满了:

160816 22:52:15  InnoDB: Error: page 1415 log sequence number 82039318708
InnoDB: is in the future! Current system log sequence number 81640793100.
InnoDB: Your database may be corrupt or you may have copied the InnoDB
InnoDB: tablespace but not the InnoDB log files. See
InnoDB: http://dev.mysql.com/doc/refman/5.5/en/forcing-innodb-recovery.html
InnoDB: for more information.

根据这里的建议,为了解决这个问题,我做了一个mysqldump并恢复了所有数据库(参见其他几个解决方案的链接)。

$ mysqldump -u root -p --allow-keywords --add-drop-database --comments --hex-blob --opt --quote-names --databases db_1 db_2 db_3 db_etc > backup-all-databases.sql
$ mysql -u root -p < backup-all-databases.sql

现在一切似乎都在按预期进行。