PHP下使用mysqli的函数连接mysql出现warning: mysqli::real_connect(): (hy000/1040): ...

背景:把MysqL换成MysqLi时出现,连接数过多,其实际上并不是,原因是我挪动了一下PHP的sock文件位置导致,因这几个socket修改没有修改完全,于是出现了too many connections,从MysqL里show processlist并没有发现真的有连接,其实用tshark抓下包估计能看到(http://justwinit.cn/post/7458/),并没有发出请求,而估计是MysqLi的客户端自己报出来的,别看这个问题小,搞了老半天,都想重新安装PHP了,发现原来是路径写错了同时MysqLi的客户端提示连接数过多的误提示导致方向走错了。如下:

rush:PHP;"> [root@iZ25z0ugwgtZ etc]# grep -r "MysqL.sock" ./ ./PHP.ini:pdo_MysqL.default_socket=/data/runsock/MysqLsock/MysqL.sock ./PHP.ini:;MysqL.default_socket = /tmp/MysqL.sock ./PHP.ini:MysqL.default_socket = /data/runsock/MysqLsock/MysqL.sock ./PHP.ini:MysqLi.default_socket = /data/runsock/MysqL.sock //这个位置被挪动到,/data/runsock/MysqLsock/MysqL.sock导致。

修改后记得重启动PHP-fpm:

rush:PHP;"> [root@iZ25z0ugwgtZ etc]# service PHP-fpm restart Gracefully shutting down PHP-fpm . done Starting PHP-fpm done

______________________排查要点如下_______________________________

warning: MysqLi::real_connect(): (hy000/1040): too many connections in:

出现场景 :

手动编译安装MysqL,并制定安装位置,PHP以localhost方式连接MysqL

原因分析 :

手动编译安装制定位置后所有的MysqL文件都在制定的目录或者data目录下面,PHP认只会寻找/temp/MysqL.sock找这个sock文件,所以会导致sock文件无法找到。

解决方法

1.给sock文件做个软链

ln -s /data/MysqLdb/MysqL.sock /tmp/MysqL.sock;

或者

2.修改PHPMysqL.sock连接地址

MysqL.default_socket=/data/MysqLdb/MysqL.sock

3.使用tcp socket的方式进行连接

MysqL('127.0.0.1','username','passwod');

下面给大家介绍PHP MysqL_connect() 函数

定义和用法

MysqL_connect() 函数打开非持久的 MysqL 连接。

语法

rush:sql;"> MysqL_connect(server,user,pwd,newlink,clientflag)

ottom: rgb(170,170,170) 1px solid; text-align: left; border-left: rgb(170,170) 1px solid; padding-bottom: 0px; widows: 1; text-transform: none; background-color: rgb(249,249,249); text-indent: 0px; margin: 10px 0px 0px; padding-left: 0px; width: 709px; letter-spacing: normal; padding-right: 0px; border-collapse: collapse; font: 12px Verdana,Arial,宋体; white-space: normal; color: rgb(0,0); border-top: rgb(170,170) 1px solid; border-right: rgb(170,170) 1px solid; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px"> ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px"> ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px"> ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">
ottom: rgb(170,170) 1px solid; padding-bottom: 5px; background-color: rgb(213,213,213); margin: 0px; padding-left: 6px; padding-right: 15px; vertical-align: baseline; border-top: rgb(170,170) 1px solid; padding-top: 5px">参数 ottom: rgb(170,170) 1px solid; padding-top: 5px">描述 ottom: rgb(170,170) 1px solid; border-left: rgb(170,170) 1px solid; padding-bottom: 6px; background-color: rgb(239,239,239); margin: 0px; padding-left: 6px; padding-right: 15px; vertical-align: text-top; border-top: rgb(170,170) 1px solid; padding-top: 6px">serverottom: rgb(170,170) 1px solid; padding-top: 6px">

ottom: 0px; border-left: 0px; padding-bottom: 0px; line-height: 18px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">可选。规定要连接的服务器。

ottom: 0px; border-left: 0px; padding-bottom: 0px; line-height: 18px; margin: 12px 0px 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">可以包括端口号,例如 "hostname:port",或者到本地套接字的路径,例如对于 localhost 的 ":/path/to/socket"。

ottom: 0px; border-left: 0px; padding-bottom: 0px; line-height: 18px; margin: 12px 0px 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">如果 PHP 指令 MysqL.default_host 未定义(认情况),则认值是 'localhost:3306'。

ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">
ottom: rgb(170,170) 1px solid; padding-top: 6px">userottom: rgb(170,170) 1px solid; padding-top: 6px">可选。用户名认值是服务器进程所有者的用户名。ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px"> ottom: rgb(170,170) 1px solid; padding-top: 6px">pwdottom: rgb(170,170) 1px solid; padding-top: 6px">可选。密码。认值是空密码。ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px"> ottom: rgb(170,170) 1px solid; padding-top: 6px">newlinkottom: rgb(170,170) 1px solid; padding-top: 6px">可选。如果用同样的参数第二次调用 MysqL_connect(),将不会建立新连接,而将返回已经打开的连接标识。参数 new_link 改变此行为并使 MysqL_connect() 总是打开新的连接,甚至当 MysqL_connect() 曾在前面被用同样的参数调用过。ottom: 0px; border-left: 0px; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px"> ottom: rgb(170,170) 1px solid; padding-top: 6px">clientflagottom: rgb(170,170) 1px solid; padding-top: 6px">

ottom: 0px; border-left: 0px; padding-bottom: 0px; line-height: 18px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">可选。client_flags 参数可以是以下常量的组合:

    ottom: 0px; border-left: 0px; padding-bottom: 0px; list-style-type: none; margin: 20px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">
  • ottom: 0px; border-left: 0px; padding-bottom: 0px; list-style-type: disc; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">MysqL_CLIENT_SSL - 使用 SSL 加密
  • ottom: 0px; border-left: 0px; padding-bottom: 0px; list-style-type: disc; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">MysqL_CLIENT_COMPRESS - 使用压缩协议
  • ottom: 0px; border-left: 0px; padding-bottom: 0px; list-style-type: disc; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">MysqL_CLIENT_IGnorE_SPACE - 允许函数名后的间隔
  • ottom: 0px; border-left: 0px; padding-bottom: 0px; list-style-type: disc; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px">MysqL_CLIENT_INteraCTIVE - 允许关闭连接之前的交互超时非活动时间

返回值

如果成功,则返回一个 MysqL 连接标识,失败则返回 FALSE。

提示和注释

注释:脚本一结束,到服务器的连接就被关闭,除非之前已经明确调用 MysqL_close() 关闭了。 提示:要创建一个持久连接,请使用 MysqL_pconnect() 函数

例子

rush:PHP;">

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...