Mac上用 homebrew 装 mysql,解决‘/tmp/mysql.sock‘ 报错

mac上用homebrew安装mysql(Mac自带homebrew)

brew install mysql

安装后如下:

If you need to have first in your PATH run:
echo ‘export PATH="/usr/local/opt//bin:$PATH"‘ >> /Users/chenyang/.bash_profile

For compilers to find you may need to set:
export LDFLAGS="-L/usr/local/opt//lib"
export CPPFLAGS="-I/usr/local/opt//include"

To have launchd start now and restart at login:
brew services start
Or, if you don‘t want/need a background service you can just run:
/usr/local/opt//bin/mysql.server start

做好my.cnf的设置,我这里的路径为:/usr/local/etc/my.cnf,内容如下:

[client]
default-character-set=utf8
#password = your_password
port = 3306
socket = /tmp/mysql/mysql.sock
[mysqld]
character-set-server=utf8
init_connect=‘SET NAMES utf8
port = 3306
socket = /tmp/mysql/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
character-set-server=utf8
init_connect=‘SET NAMES utf8‘

#skip-networking
replication
log-bin=mysql-bin

binlog_format=mixed

server-id = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
#safe-updates
default-character-set=utf8

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

此时在控制台运行会报错:

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

google各教程教程,发现就是mysql.sock这个文件的路径问题,最后解决办法:

修改/usr/local/etc/my.cnf文件(如果没有自创建),将文件里面socket的路径指向你mysql的安装路径,使用了很多网上的方式,结果还是不行,应该是安装路径不一样,你必须找到你的路径,(mysql版本5.7.20)路径是在/usr/local/var/mysql/

[client]
default-character-set=utf8
#password = your_password
port = 3306
socket = /usr/local/var/mysql/mysql.sock
#/tmp/mysql/mysql.sock

[mysqld]
character-set-server=utf8
init_connect=‘SET NAMES utf8
port = 3306
socket = /usr/local/var/mysql/mysql.sock
#/tmp/mysql/mysql.sock

修改后问题解决,数据库正常连接

相关推荐