终端mysql错误2002

时间:2016-05-18 20:26:16

标签: mysql

我用php切换器和phpmyadmin自己安装Apache / php / mysql。

我经常在phpmyadmin上遇到2002错误,然后我修复它将pma文件'pma / config.inc.php'中的主机从'localhost'更改为'127.0.0.1'。

在我的终端上,当输入mysql命令时,它总是返回我:

> mysql 

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

但是,如果我强制连接root密码,在本地IP地址127.0.0.1上,它可以工作。请注意,在Google Chrome中,phpmyadmin适用于http://127.0.0.1/pmahttp://localhost/pma

终端:

> mysql -u root -h 127.0.0.1 -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.12 MySQL Community Server (GPL)

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>

在/ tmp文件夹中,我有一个锁定的文件:

  

mysql.sock.lock

Apache版本:

> apachectl -v
Server version: Apache/2.4.18 (Unix)
Server built:   Feb 20 2016 20:03:19

PHP版本(请记住我有一个php切换器),但它也不适用于其他版本:

> php -v
PHP 7.0.6 (cli) (built: Apr 29 2016 04:21:39) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
    with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans

mysql状态:

mysql  Ver 14.14 Distrib 5.7.12, for osx10.11 (x86_64) using  EditLine wrapper

Connection id:      4
Current database:
Current user:       root@localhost
SSL:            Not in use
Current pager:      less
Using outfile:      ''
Using delimiter:    ;
Server version:     5.7.12 MySQL Community Server (GPL)
Protocol version:   10
Connection:     127.0.0.1 via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:       3306
Uptime:         1 day 3 hours 28 min 19 sec

Threads: 1  Questions: 9  Slow queries: 0  Opens: 105  Flush tables: 1  Open tables: 98  Queries per second avg: 0.000

感谢您帮助我获得稳定的配置,而无需再次安装MAMP或其他服务器配置软件。我希望能够在终端上使用mysql命令而不强制它,并且不再看到2002年的错误。

Ps:抱歉法语的第一篇文章

编辑:好的,似乎没人知道原因。但是,您可以为“mysql -u root -h 127.0.0.1 -p”创建别名。

1 个答案:

答案 0 :(得分:0)

尝试使用

添加〜/ .my.cnf
[client]
user=root
password=crazypasswordhere

也见过MySQL Utilities - ~/.my.cnf option file