Mac mySql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的解决办法

我的环境:Mac 10.11.6 ,mysql  5.7.14  。

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

这个错是链接时报的错,要链接必须启动。修复的时候首先要启动mysql。

首先来了解一下 mysql.sock的作用:

Mysql有两种连接方式:

(1),TCP/IP

(2),socket

对mysql.sock来说,其作用是程序与mysqlserver处于同一台机器,发起本地连接时可用。

例如你无须定义连接host的具体IP得,只要为空或localhost就可以。

在此种情况下,即使你改变mysql的外部port也是一样可能正常连接。

因为你在my.ini中或my.cnf中改变端口后,mysql.sock是随每一次 mysql server启动生成的。已经根据你在更改完my.cnf后重启mysql时重新生成了一次,信息已跟着变更。

那么对于外部连接,必须是要变更port才能连接的。

mysql有两种连接方式:

1、TCP/IP

2、socket

mysql.sock的作用是server和client在同一台服务器,并且使用localhost进行链接的时候,就会使用socket来进行连接——仅此而已

也就是:为主机名为localhost建立的MySQL连接,该连接过程通过一个套接字文件mysql.socket实现的。所以该文件被删后,用localhost用户是连接不到MySQL服务器的。

必须建立一条tcp/ip连接,即使用127.0.0.1而不是localhost作为-h的参数去连接MySQL服务器,如:mysqladmin -h 127.0.0.1 -u root -p shutdown,强制地建立一条tcp/ip连接;

关闭MySQL服务器,再重新以localhost为主机名启动MySQL服务器,它就会重新创建一个套接字文件。

注意:mysql.sock是一个临时文件,启动mysql后才会生成。

报错的原因:MySQL将其放在/tmp目录,而OSX将其放在/var/mysql目录。所以我们只需要创建一个软链接,输入以下两个命令即可:

如果/var/下没有mysql目录,则需创建

创建目录:sudo mkdir /var/mysql

创建软链接:sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

参考文章:http://www.jianshu.com/p/2fb9a3bb12f6,http://blog.csdn.net/sarahhuangzht/article/details/51508112

一下内容来自:http://blog.itpub.net/28602568/viewspace-1797619/

mysql.sock的作用2015-09-11 22:16:00

分类: MySQL


链接:http://blog.itpub.net/28602568/viewspace-1797619/

标题:mysql.sock的作用

作者:lōττéry©版权所有[文章允许转载,但必须以链接方式注明源地址,否则追究法律责任.]


注释:

前段时间出现过一种情况,localhost本地登录mysql数据库提示不能连接mysql.sock,第三方工具sqlyog可以登录,具体原因如下。

原创内容如下  mysql.sock的作用


mysql有两种连接方式:

1、TCP/IP

2、socket

mysql.sock的作用是server和client在同一台服务器,并且使用localhost进行链接的时候,就会使用socket来进行连接——仅此而已

也就是:为主机名为localhost建立的MySQL连接,该连接过程通过一个套接字文件mysql.socket实现的。所以该文件被删后,用localhost用户是连接不到MySQL服务器的。

必须建立一条tcp/ip连接,即使用127.0.0.1而不是localhost作为-h的参数去连接MySQL服务器,如:mysqladmin -h 127.0.0.1 -u root -p shutdown,强制地建立一条tcp/ip连接;

关闭MySQL服务器,再重新以localhost为主机名启动MySQL服务器,它就会重新创建一个套接字文件。


对上文加以测试深入了解;

查看mysql.sock具体路径:

[[email protected] ~]# ps -ef | grep mysql.sock|grep -v "grep"
mysql    31108 30650  0 Sep10 ?        00:03:17 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/Wonhigh-Test16.err --pid-file=/var/lib/mysql/Wonhigh-Test16.pid --socket=/var/lib/mysql/mysql.sock --port=3306
[[email protected] ~]#

转移套接字文件 mysql.sock
[[email protected] ~]# mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql1.sock

确认本地登录情况
[[email protected] ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2)
[[email protected] ~]#

尝试127.0.0.1 tcp/ip连接(第三方工具远程连接都可以‘连接属性会显示为TCP/IP ’)

[[email protected] ~]# mysql -uroot -p123456 -h127.0.0.1

Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 127
Server version: 5.6.19-log
Copyright (c) 2000, 2014, 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>

恢复本地连接

[[email protected] ~]# mv /var/lib/mysql/mysql1.sock /var/lib/mysql/mysql.sock

[[email protected] ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 121
Server version: 5.6.19-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> exit
Bye
[[email protected] ~]#

Mac mySql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的解决办法

时间: 2024-10-12 08:11:43

Mac mySql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的解决办法的相关文章

mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

解决方案如下: mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory") 原文地址:https://www.cnblogs.com/youran-he/p/8182280.html

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

Mac brew 安装mysql 报错 ERROR 2002 (HY000): Can't connect to local MySQL server through socket

安装mysql 最新版 56 brew install mysql 启动报错 ben:~ soul$ which mysql /usr/local/bin/mysql ben:~ soul$ mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 解决办法 运行 ben:~ soul$ mysql.server start Starting MySQL .

mysql连接报错 ERROR 2002 (HY000): Can't connect to local MySQL server through socket

安装完mysql连接报错: [[email protected] app]# mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) [[email protected] app]# mysql -uroot -p Enter password:  ERROR 2002 (HY000): Can't connect to local M

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

我在源码编译安装mysql后,虽然mysql服务可以正常启动,但是当我直接执行mysql后,出现了错误. 错误提示如下: [[email protected] ~]# mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 出现错误原因: 由于mysql 默认的mysql.sock 是在/var/lib/mysql/mysql.sock,但linux系统总

(转)ERROR 2002 (HY000): Can't connect to local MySQL server through socket '***' (2)

有时候,当我们使用"mysql"."mysqladmin"."mysqldump"等命令管理数据库时,服务器抛出类似如下错误: 1 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 这个错误是由于什么原因导致的呢? 一.错误现场还原: 下面我们通过三种方式来连接,然后观察提示的错误信息: 1.

mac 安装mysql 报错“ERROR 2002 (HY000): Can not connect to local MySQL server through socket '/tmp/mysql.sock' (2)” 解决办法

首先安装 homebrew 再 brew install mysql 之后连接 mysql 无论是登录还是修改初始密码都会报如下的错误 ERROR 2002 (HY000): Can not connect to local MySQL server through socket '/tmp/mysql.sock' (2) 运行如下解决:参考 http://www.thinksaas.cn/group/topic/347978/ unset TMPDIR mysql_install_db --v

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

好久不用mysql,今天突然想用的时候, mysql -uroot -p 直接报了下面的错误 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) mac可以在设置里面启动mysql,设置里面启动mysql的时候提示没有/usr/local/mysql/data的权限, 给予权限 sudo chown -R mysql:mysql  /usr/local/mysql

长久不用的mysql报错ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

mac上安装过mysql: 然而,尝试连接时报错: $ mysql -u root -p Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 尝试去查看mysql.socket文件,发现没有.. $ which mysql /usr/local/bin/mysql 然后试试重启mysql: $ mysql.server start