1. 程式人生 > >原來如此簡單:解決Mac上的Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

原來如此簡單:解決Mac上的Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

mac os上第一次使用mysql,果然出現了問題,安裝好mysql的dmg後,在終端輸入mysql,結果出現

hrfpc:~ hrf$ mysql

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

這個錯誤

在網上搜到很多方法,不好使,然後查詢了官方文件

http://dev.mysql.com/doc/refman/5.7/en/can-not-connect-to-server.html

下面寫道

Here are some reasons the Can't connect to local MySQL server

 error might occur:

  • mysqld is not running on the local host. Check your operating system's process list to ensure the mysqld process is present.

  • 。。。
沒想到第一條就符合,然後我嘗試輸入mysqld

結果

hrfpc:~ hrf$ mysqld

mysqld: Can't change dir to '/usr/local/mysql-5.7.16-osx10.11-x86_64/data/' (Errcode: 13 - Permission denied)

等一大串錯誤之類的 ,最後退出了

最後,正確的方法:

hrfpc:~ hrf$ sudo mysqld_safe

2016-10-14T11:47:17.6NZ mysqld_safe Logging to '/usr/local/mysql/data/hrfpc.err'.

2016-10-14T11:47:17.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

然後mysqld服務就執行啦

此時,終於能夠開啟mysql:

hrfpc:etc hrf$ mysql -u root -p

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 13

Server version: 5.7.16

Copyright (c) 2000, 2016, 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.

有時事情總是用最簡單的辦法解決