天天看点

mysql.sock的作用

注释:

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

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服务器,它就会重新创建一个套接字文件。

<b>对上文加以测试深入了解;</b>

查看mysql.sock具体路径:

[root@wonhigh-test16 ~]# 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 <b>--socket=/var/lib/mysql/mysql.sock</b> --port=3306

[root@wonhigh-test16 ~]# 

转移套接字文件 mysql.sock

[root@wonhigh-test16 ~]# mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql1.sock 

确认本地登录情况

[root@wonhigh-test16 ~]# mysql -uroot -p123456

warning: using a password on the command line interface can be insecure.

<b>error 2002 (hy000): can't connect to local mysql server through socket '/var/lib/mysql/mysql.sock' (2)</b>

[root@wonhigh-test16 ~]#

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

[root@wonhigh-test16 ~]# mysql -uroot -p123456 -h127.0.0.1

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&gt;

恢复本地连接

[root@wonhigh-test16 ~]# mv /var/lib/mysql/mysql1.sock /var/lib/mysql/mysql.sock

your mysql connection id is 121

server version: 5.6.19-log mysql community server (gpl)

mysql&gt; exit

bye

  【源于本人笔记】 若有书写错误,表达错误,请指正...