<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[疑难问题]PHP下使用mysqli的函数连接mysql出现warning: mysqli::real_connect(): (hy000/1040): too many connections in]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Fri, 12 Feb 2016 17:48:56 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：把mysql换成mysqli时出现，连接数过多，其实际上并不是，原因是我挪动了一下php的sock文件位置导致，因这几个socket修改没有修改完全，于是出现了too many connections ,从mysql里show processlist并没有发现真的有连接，其实用tshark抓下包估计能看到(http://jackxiang.com/post/7458/)，并没有发出请求，而估计是mysqli的客户端自己报出来的，别看这个问题小，搞了老半天，都想重新安装Php了，发现原来是路径写错了同时mysqli的客户端提示连接数过多的误提示导致方向走错了。如下：<br/><textarea name="code" class="php" rows="15" cols="100">
[root@iZ25z0ugwgtZ etc]# grep -r &quot;mysql.sock&quot; ./
./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&nbsp;&nbsp;//这个位置被挪动到,/data/runsock/mysqlsock/mysql.sock导致。
</textarea><br/><br/>修改后记得重启动php-fpm：<br/>[root@iZ25z0ugwgtZ etc]# service php-fpm restart<br/>Gracefully shutting down php-fpm . done<br/>Starting php-fpm&nbsp;&nbsp;done<br/><br/>还会出现在localhost时连接找不到sock时提示诡异，如下：Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in <br/>开始的回答有点不严谨，估计也没有解决问题，修改了答案，解决办法如下，或配置php.ini，参考：http://jackxiang.com/post/8499/<br/><textarea name="code" class="php" rows="15" cols="100">
$db = new MySQLi(&#039;localhost&#039;, &#039;root&#039;, &#039;root&#039;, &#039;my_db&#039;, &#039;3306&#039;, &#039;/var/run/mysqld/mysqld.sock&#039;)
</textarea><br/>问题出现的原因：<br/>当主机填写为localhost时MySQL会采用 unix domain socket连接，当主机填写为127.0.0.1时MySQL会采用TCP/IP的方式连接。使用Unix socket的连接比TCP/IP的连接更加快速与安全。这是MySQL连接的特性，可以参考官方文档的说明4.2.2. Connecting to the MySQL Server：<br/><br/>On Unix, MySQL programs treat the host name localhost specially, in a way that is <br/>likely different from what you expect compared to other network-based programs. <br/>For connections to localhost, MySQL programs attempt to connect to the local server <br/>by using a Unix socket file. This occurs even if a --port or -P option is given to <br/>specify a port number. To ensure that the client makes a TCP/IP connection to the <br/>local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP <br/>address or name of the local server. You can also specify the connection protocol <br/>explicitly, even for localhost, by using the --protocol=TCP option.<br/>这个问题有以下几种解决方法：<br/><br/>使用TCP/IP代替Unix socket。即在连接的时候将localhost换成127.0.0.1。<br/>修改MySQL的配置文件my.cnf，指定mysql.socket的位置：<br/>/var/lib/mysql/mysql.sock (你的mysql.socket路径)。<br/>直接在php建立连接的时候指定my.socket的位置（官方文档：mysqli_connect）。比如：<br/>$db = new MySQLi(&#039;localhost&#039;, &#039;root&#039;, &#039;root&#039;, &#039;my_db&#039;, &#039;3306&#039;, &#039;/var/run/mysqld/mysqld.sock&#039;)<br/>如果哪里没有说清楚或者说错了，欢迎提出了～～<br/>______________________排查要点如下_______________________________<br/>warning: mysqli::real_connect(): (hy000/1040): too many connections in:<br/>出现场景 ：手动编译安装mysql，并制定安装位置,php以localhost方式连接mysql <br/>原因分析 ：手动编译安装制定位置后所有的mysql文件都在制定的目录或者data目录下面，php默认只会寻找/temp/mysql.sock找这个sock文件，所以会导致sock文件无法找到。 <br/>解决方法 ： <br/>1.给sock文件做个软链<br/><br/><br/> ln -s /data/mysqldb/mysql.sock /tmp/mysql.sock;<br/>或者 <br/>2.修改php的默认mysql.sock连接地址<br/><br/><br/>mysql.default_socket=/data/mysqldb/mysql.sock<br/>3.使用tcp socket的方式进行连接<br/><br/><br/>mysql(&#039;127.0.0.1&#039;,&#039;username&#039;,&#039;passwod&#039;);<br/><br/>来自：http://ju.outofmemory.cn/entry/144221
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [疑难问题]PHP下使用mysqli的函数连接mysql出现warning: mysqli::real_connect(): (hy000/1040): too many connections in]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>