<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.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://www.jackxiang.com/post//</link>
<title><![CDATA[[实践OK]mysqli连接出现问题之解决mysql 1040错误Too many connections的方法，及出现PHP Warning:  mysqli::mysqli(): (HY000/2002): No such file or directory的问题。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[数据库技术]]></category>
<pubDate>Fri, 12 Feb 2016 13:05:38 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：mysqli连接数据库，pdo连接mysql都有，这块很多老代码都是用的mysql，为此，伴随PHP7的强制升级，对mysql不再支持了。So，对mysqli这块可以研究研究是很有必要的：-），demo代码如下：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
$mysqli = new MySQLi(&#039;127.0.0.1&#039;, &#039;levoo&#039;, &#039;levoo2016&#039;, &#039;levoo_mysql&#039;, &#039;3306&#039;, &#039;/data/runsock/mysqlsock/mysql.sock&#039;);
/* check mysqliion */
if (mysqli_connect_error()) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Connect failed: %s&#92;n&quot;, mysqli_error($mysqli));
&nbsp;&nbsp;&nbsp;&nbsp;exit();
&#125;

$sql = &quot;select * from user&quot;;
//执行sql语句，完全面向对象的
$result = mysqli_query($mysqli,$sql)or die(mysqli_error($mysqli));
while($row = mysqli_fetch_array($result,MYSQLI_NUM))&#123;//通过循环读取数据内容&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;print_r($row);
&#125;

/* close mysqliion */
$mysqli-&gt;close();
?&gt;
</textarea><br/>运行结果：<br/>[root@iZ25z0ugwgtZ mysql]# php test.php <br/>Array<br/>(<br/>&nbsp;&nbsp;&nbsp;&nbsp;[0] =&gt; 001<br/>&nbsp;&nbsp;&nbsp;&nbsp;[1] =&gt; jackX<br/>)<br/>Array<br/>(<br/>&nbsp;&nbsp;&nbsp;&nbsp;[0] =&gt; 001<br/>&nbsp;&nbsp;&nbsp;&nbsp;[1] =&gt; jackX<br/>)<br/>Array<br/>(<br/>&nbsp;&nbsp;&nbsp;&nbsp;[0] =&gt; 001<br/>&nbsp;&nbsp;&nbsp;&nbsp;[1] =&gt; jackX<br/>)<br/><br/>——————————————————————————————————————————————————————————————<br/>问题一：当用localhost时出现 mysql数据库 Too many connections<br/>出现这种错误明显就是 mysql_connect 之后忘记 mysql_close；<br/>当大量的connect之后，就会出现Too many connections的错误，mysql默认的连接为100个，而什么情况下会出现这种错误呢？<br/><br/>正常的mysql_connect 之后调用 mysql_close()关闭连接<br/>但在连接错误时，会者mysql_real_query()出现错误退出时，可能忘记mysql_close();<br/>所以在程序return 之前一定要判断是否close(),最稳妥的方法就是在写任何函数时都只有一个出口！<br/>还有可以通过修改mysql配置文件来加大允许连接的数量！<br/><br/>有时你的服务器是经常出现这样的错误呢：<br/>错误信息如下：<br/>Can not connect to MySQL server<br/><br/>Error: Too many connections<br/>Errno.: 1040<br/><br/>Similar error report has beed dispatched to administrator before.<br/><br/>从官方文档知道linux上面编译安装的mysql默认的连接为100个<br/>文档：http://dev.mysql.com/doc/refman/5.0/en/too-many-connections.html<br/><br/>mysql官方告诉我们需要修改max_connections的值,那么我们怎么去修改呢？有两种方法<br/>1、修改配置文件文件<br/>修改/etc/my.cnf这个文件，在[mysqld] 中新增max_connections=N，如果你没有这个文件请从编译源码中的support-files文件夹中复制你所需要的*.cnf文件为到 /etc/my.cnf。我使用的是my-medium.cnf,中型服务器配置。例如我的[mysqld]的内容如下<br/>[mysqld]<br/>port = 3306<br/>socket = /tmp/mysql.sock<br/>skip-locking<br/>key_buffer = 160M<br/>max_allowed_packet = 1M<br/>table_cache = 64<br/>sort_buffer_size = 512K<br/>net_buffer_length = 8K<br/>read_buffer_size = 256K<br/>read_rnd_buffer_size = 512K<br/>myisam_sort_buffer_size = 8M<br/>max_connections=1000<br/><br/>由于对mysql还不是很熟悉，所以很多参数没有修改。哈哈。。<br/><br/>2、非使用mysqld脚本自动启动的用户。<br/>修改$MYSQL_HOME/bin/mysqld_safe文件<br/>例如：/usr/local/mysql/bin/mysqld_safe这个文件<br/>grep -n ‘max_connection’ $MYSQL_HOME/bin/mysqld_safe<br/>修改对应行号的max_connections参数值<br/><br/>来自：http://www.2cto.com/database/201306/218126.html<br/><br/><br/><br/>问题二：当在测试时出现[root@iZ25z0ugwgtZ mysql]# php&nbsp;&nbsp;test.php<br/>PHP Warning:&nbsp;&nbsp;mysqli::mysqli(): (HY000/2002): No such file or directory in /data/codesdev/testdemo/mysql/test.php on line 2<br/>Connect failed: No such file or directory<br/>将&#039;localhost&#039;修改为&#039;127.0.0.1&#039;之后链接正常！<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/>来自：https://segmentfault.com/q/1010000000328531
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]mysqli连接出现问题之解决mysql 1040错误Too many connections的方法，及出现PHP Warning:  mysqli::mysqli(): (HY000/2002): No such file or directory的问题。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>