Mac上用 homebrew 裝 mysql,解決'/tmp/mysql.sock' 報錯

mac上用homebrew安裝mysql(Mac自帶homebrew)mysql

brew install mysqlsql

安裝後以下:數據庫

If you need to have mysql@5.7 first in your PATH run:
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> /Users/chenyang/.bash_profilebash

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

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

作好my.cnf的設置,我這裏的路徑爲:/usr/local/etc/my.cnf,內容以下:ui

[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'google

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

binlog_format=mixedorm

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

修改後問題解決,數據庫正常鏈接

相關文章
相關標籤/搜索