Mac下Mysql啟動異常["ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)"]


在mac下使用brew安裝mysql,之前沒有使用過,今天啟動的時候發現啟動不了

huijundeMacBook-Pro:bin huijunzhang$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

查看mysql的安裝位置:

huijundeMacBook-Pro:bin huijunzhang$ which mysql
/usr/local/bin/mysql

說明mysql還是安裝過的

huijundeMacBook-Pro:bin huijunzhang$ brew info mysql
mysql: stable 5.7.13 (bottled)
Open source relational database management system
https://dev.mysql.com/doc/refman/5.7/en/
Conflicts with: mariadb, mariadb-connector-c, mysql-cluster, mysql-connector-c, percona-server
/usr/local/Cellar/mysql/5.7.13 (13,344 files, 445.0M) *
  Poured from bottle on 2016-06-30 at 15:05:40
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/mysql.rb
==> Dependencies
Build: cmake ✘
Required: openssl ✔
==> Options
--with-archive-storage-engine
    Compile with the ARCHIVE storage engine enabled
--with-blackhole-storage-engine
    Compile with the BLACKHOLE storage engine enabled
--with-debug
    Build with debug support
--with-embedded
    Build the embedded server
--with-local-infile
    Build with local infile loading support
--with-test
    Build with unit tests
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

To connect run:
    mysql -uroot

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

有可能是mysql服務沒有啟動

huijundeMacBook-Pro:bin huijunzhang$ mysql.server start
Starting MySQL
. SUCCESS! 

哈哈,mysql服務啟動了,好開心呀!

接下來要試試能否進入mysql了

huijundeMacBook-Pro:bin huijunzhang$ mysql
ERROR 1045 (28000): Access denied for user 'huijunzhang'@'localhost' (using password: NO)

這個提示應該很明顯了吧,權限問題,mysql下沒有huijunzhang這個用戶,裝過mysql的朋友應該知道,安裝mysql默認的用戶名是root,端口是3306,別問我為什么知道。。。

那就使用root用戶啟動:

huijundeMacBook-Pro:bin huijunzhang$ sudo mysql
Password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.13 Homebrew

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.

mysql>
mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

哈哈,問題解決了,好開心呀!

總結:出現“ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)”的根本原因就是mysql服務沒有啟動,下面總結下mysql在mac下的幾個常用的命令吧!

$ mysql.server start
$ mysql.server restart
$ mysql.server stop
$ mysql.server status
$
mysql -u root -p
 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM