mysql.sock的作用

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:


标题: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具体路径:
[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  --socket=/var/lib/mysql/mysql.sock  --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.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@Wonhigh-Test16 ~]#
尝试127.0.0.1 tcp/ip连接(第三方工具远程连接都可以‘连接属性会显示为TCP/IP ’)
[root@Wonhigh-Test16 ~]# 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>

恢复本地连接
[root@Wonhigh-Test16 ~]# mv /var/lib/mysql/mysql1.sock /var/lib/mysql/mysql.sock
[root@Wonhigh-Test16 ~]# 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
[root@Wonhigh-Test16 ~]# 

  【源于本人笔记】 若有书写错误,表达错误,请指正... 
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
5月前
|
关系型数据库 MySQL 数据库
cant connect to local MySQL server through socket /tmp/mysql.sock (2)
cant connect to local MySQL server through socket /tmp/mysql.sock (2)
cant connect to local MySQL server through socket /tmp/mysql.sock (2)
|
7月前
|
关系型数据库 MySQL 数据库
Can't connect to local MySQL server through socket '/tmp/mysql.sock'
Can't connect to local MySQL server through socket '/tmp/mysql.sock'
42 0
|
SQL 关系型数据库 MySQL
socket=/tmp/mysql.sock是干什么的?底层原理是什么?
socket=/tmp/mysql.sock是干什么的?底层原理是什么?
991 0
|
关系型数据库 MySQL
【技术分享】Can ‘t connect to local MySQL server through socket ‘/tmp/mysql.sock ‘解决方案
【技术分享】Can ‘t connect to local MySQL server through socket ‘/tmp/mysql.sock ‘解决方案
153 0
【技术分享】Can ‘t connect to local MySQL server through socket ‘/tmp/mysql.sock ‘解决方案
|
关系型数据库 MySQL
Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决
Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决
815 2
Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决
|
网络协议 关系型数据库 MySQL
MySQL中的pid与socket是什么?
不知道你有没有注意过,MySQL 启动时需要配置 pid 及 socket 文件路径。偶尔还会出现因 pid 文件找不到而启动失败的现象,那么 pid 与 socket 文件究竟是干什么用的呢?我们一起来看下本篇文章。
222 0
|
关系型数据库 MySQL 数据库
mysql无法重启,Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’
mysql数据库经常会遇到各种问题,这里是无法重启的一种情况,总结一遍以后再遇到相同问题及时解决,也希望能帮到像我一样的小白!
mysql无法重启,Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’
|
Oracle 网络协议 关系型数据库
mysql.sock的作用
mysql.sock的作用
3609 0