首页 > 编程知识 正文

socket通信原理,socket 通信

时间:2023-05-04 15:28:16 阅读:17509 作者:4163

mysql连接类型和套接字通信原理说明| Rootop服务器运行时和web体系结构

原文:

MySQL连接类型的localhost与127.0.0.1,IP差异:

localhost和127.0.0.1有什么区别? 也有人说本地ip,据说使用127.0.0.1比localhost更好。 可以减少一次分析。 好像还有人不明白这个入门问题,其实这两个有区别。

no.1一般接:

localhost也称为local,正确的解释是:本地服务器

127.0.0.1windows等系统上的正确解释是:本地地址(本地服务器)

NO.2:

localhost(local )不通过网卡传输! 这很重要,不受网络防火墙和网卡相关的限制。

127.0.0.1通过网卡传输,取决于网卡,受网络防火墙和网卡的限制。

一般来说,本地服务最好使用localhost。 localhost不解析为ip,不消耗网卡、网络资源。

localhost可能可以,但如果不能是127.0.0.1,就在这里。 在localhost访问的情况下,系统持有的本机的当前用户的权限去访问,但使用ip的情况下,预计与本机通过网络再次访问本机一样,可能与网络用户的权限有关。

no.3连接MySQL时的主机类型:

1、mysql -h 127.0.0.1时,使用TCP/IP连接

mysql server认为此连接来自127.0.0.1或“localhost.localdomain”

2、对于MySQL-h本地主机,使用UNIX套接字,而不是TCP/IP连接;

在这种情况下,mysql server认为此客户端来自“本地主机”

mysql权限管理的“localhost”有特定的含义。

—— MySQL手册5.6.4 . ahostvaluemaybeahostnameoranipnumber,or‘localhost’toindicatethelocalhost。

注:虽然两者的连接方法不同,但如果localhost为默认127.0.0.1,则这两种连接方法使用的权限记录为: (因为记录在前面,先匹配)

主机: localhost

user:路由

MySQL连接类型证明:

# MySQL-h 127.0.0.1-uroot-pf ity.cn # 123

必须指定error2003(hy000 ) 3360 can’tconnecttomysqlserveron’127.0.0.1’(111 ) [端口。 相反,你会收到这个错误]

# MySQL-h 127.0.0.1-p 3307-uroot-pf ity.cn # 123

MySQL状态;

——3354

mysqlVer 14.12 Distrib 5.0.95,forredhat-Linux-GNU(I686 ) using readline 5.1

连接id :1599

current数据库:

目前用户: root @ 127.0.0.1

SSL:Not in use

Current pager:stdout

用户界面文件: "

用户定义器:

服务器版本:5.1.48-logsourcedistribution

协议版本: 10

连接:127.0.0.1 via TCP/IP//请注意此处

Server gddsn:latin1

Db gddsn:latin1

Client gddsn:latin1

Conn.gddsn:latin1

TCP port: 3307

Uptime: 5 hours 54 min 48 sec

threads :1 questions :5432 slow queries 33601619 opens :72 flush tables :1 open tables :65 queriespersecondavg 3360.255

——3354

# MySQL-h 192.168.1.246-uroot-pf ity.cn # 123

error2003(hy000 ) 3360 can’tconnecttomysqlserveron’192.168.1.246’(111 ) [必须指定端口。 相反,你会收到这个错误]

# MySQL-h 192.168.1.246-p 3307-uroot-pf ity

.cn#123

mysql> status;

————–

mysqlVer 14.12 Distrib 5.0.95, for redhat-linux-gnu (i686) using readline 5.1

Connection id:1598

Current database:

Current user: root@192.168.1.246

SSL:Not in use

Current pager:stdout

Using outfile:”

Using delimiter:;

Server version: 5.1.48-log Source distribution

Protocol version: 10

Connection: 192.168.1.246 via TCP/IP//注意这里

Server gddsn:latin1

Db gddsn:latin1

Client gddsn:latin1

Conn.gddsn:latin1

TCP port: 3307

Uptime: 5 hours 53 min 54 sec

Threads: 1Questions: 5428Slow queries: 1619Opens: 72Flush tables: 1Open tables: 65Queries per second avg: 0.255

————–

# mysql -h localhost-u root -pfity.cn#123[连接OK]

# mysql -h localhost -P 3307 -u root -pfity.cn#123

mysql> status;

————–

mysqlVer 14.12 Distrib 5.0.95, for redhat-linux-gnu (i686) using readline 5.1

Connection id:1600

Current database:

Current user: root@localhost

SSL:Not in use

Current pager:stdout

Using outfile:”

Using delimiter:;

Server version: 5.1.48-log Source distribution

Protocol version: 10

Connection: Localhost via UNIX socket//注意这里

Server gddsn:latin1

Db gddsn:latin1

Client gddsn:latin1

Conn.gddsn:latin1

UNIX socket:/var/lib/mysql/mysql.sock

Uptime: 5 hours 56 min 18 sec

Threads: 1Questions: 5436Slow queries: 1619Opens: 72Flush tables: 1Open tables: 65Queries per second avg: 0.254

————–

从这里看以看出,通过通过unix domain socket方式连接mysql时,你不需要特意指定端口,按照mysql官方所说通过unix domain socket方式连接mysql较快于TCP/IP方式。不过,如果通过socket方式连接mysql,当mysql.sock文件不再是默认的名称或存放路径时,你将会收到下面这个错误信息,当然,你可以需要在php代码config中声明一个变量指定unix domain socket路径或者在php.ini配置文件中定义也可。错误信息如下:

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

默认情况下我们PHP实例和mysql通信是走TCP/IP的,但如果你加载了socket模块,默认会走sock文件连接,但如果你的linux主机上没有指定默认的socket文件,这样如果你使用mysql_connect连接的话,经常我们使用mysql_connect()或socket方式连接mysql时会收到错误:

No such file or directory、Can’t connect to local MySQL server through socket… ,如图:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。