首页 > Mac上MySQL报错:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'

Mac上MySQL报错:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'

为什么在Mac上装一个MySQL这么的难?据说用HomeBrew容易装,可我发现还是要一堆配置,配置过后还是没成功。我尝试过两种安装方式:

1. HomeBrew

export ARCHFLAGS="-arch i686 -arch x86_64"
brew install mysql

用brew安装好后,还需要按照这个命令输出的信息进行配置:
brew info mysql

用这种方式安装好后,MySQL没法启动。出现如下错误:
mysql -uroot -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'

2. 从MySQL官网下载64位的.dmg文件进行安装
用这种方式安装成功后,mysql.server start启动成功。但是,还是出现跟上面一样的错误。
只有通过这种方式才能正常打开MySQL:

mysql -uroot -h127.0.0.1 -p

http://coolestguidesontheplanet.com/get-apache-mysql-php-phpmyadmin-wo... 这个网址很好的解决 /tmp/mysql.sock 的问题,请仔细阅读。


同样的问题,先在网上搜了下其他人解决这个问题的方法,是去修改mysql.cnf文件添加mysql.sock文件路径,再搜了一下我的mac没有mysql.cnf,也没有mysql.sock文件。
在查看mysql的安装说明ReadMe.txt中有个启动方法


If you have installed the Startup Item, use this command:

 shell> sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
 (ENTER YOUR PASSWORD, IF NECESSARY)
 (PRESS CONTROL-D OR ENTER "EXIT" TO EXIT THE SHELL)

If you don't use the Startup Item, enter the following command sequence:

 shell> cd /usr/local/mysql
 shell> sudo ./bin/mysqld_safe
 (ENTER YOUR PASSWORD, IF NECESSARY)
 (PRESS CONTROL-Z)
 shell> bg
 (PRESS CONTROL-D OR ENTER "EXIT" TO EXIT THE SHELL)

使用sudo的时候会让你输入你的登陆密码。
这样就可以启动
[chopin:~:22:47:07]$ sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
Password:
Starting MySQL database server

[chopin:~:22:52:06]$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.63 MySQL Community Server (GPL)

Copyright (c) 2000, 2011, 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 -uroot -p 不是启动mysql的命令,是通过客户端去连接mysql服务器
你已经正常安装了mysql,但是没有正常启动。运行 mysqld 命令,把服务起来
然后再 mysql -uroot 去连接


好使两个小时,各种尝试,终于解决,ps:下的版本 5.6.24,每个各个ReadMe.txt,愁死,楼主好运,已整合为问题解决日志


你可以直接在mysql下载页 http://www.mysql.com/downloads/mysql/ 下载到dmg的打包版,包括32和64位版本。对于每一个想要在mac上编译安装mysql练手的人我都会这么说,因为它非常麻烦,而且这对于你想积累在linux平台上的安装经验毫无用处。


mysql.server start


正确的安装步骤:

1. 执行:

$ brew install mysql

2. 在 /usr/local/etc/ 下创建或修改 my.cnf,示例:

[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8

[mysqld]
collation-server = utf8_unicode_ci
character-set-server = utf8
init-connect ='SET NAMES utf8'
max_allowed_packet = 64M
bind-address = 127.0.0.1
port = 3306
socket = /tmp/mysql.sock
innodb_file_per_table=1

[mysqld_safe]
timezone = '+0:00'

3. 赞安装完 mysql 后,他会提示你 init database,并提供下面这两句让你执行,很可能你没执行这两句

$ unset TMPDIR
$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

4. 然后就可以指向 mysql.server start 来启动了,为了安全,你还可以执行安全设置向导,根据提示一步一步配置

$ mysql_secure_installation

原先遇到类似问题剪辑的内容,出处忘了

【热门文章】
【热门文章】