标题:[实践OK]mysqli连接出现问题之解决mysql 1040错误Too many connections的方法,及出现PHP Warning:  mysqli::mysqli(): (HY000/2002): No such file or directory的问题。 出处:向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除 时间:Fri, 12 Feb 2016 21:05:38 +0000 作者:jackxiang 地址:http://jackxiang.com/post/8499/ 内容: 背景:mysqli连接数据库,pdo连接mysql都有,这块很多老代码都是用的mysql,为此,伴随PHP7的强制升级,对mysql不再支持了。So,对mysqli这块可以研究研究是很有必要的:-),demo代码如下: close(); ?> 运行结果: [root@iZ25z0ugwgtZ mysql]# php test.php Array ( [0] => 001 [1] => jackX ) Array ( [0] => 001 [1] => jackX ) Array ( [0] => 001 [1] => jackX ) —————————————————————————————————————————————————————————————— 问题一:当用localhost时出现 mysql数据库 Too many connections 出现这种错误明显就是 mysql_connect 之后忘记 mysql_close; 当大量的connect之后,就会出现Too many connections的错误,mysql默认的连接为100个,而什么情况下会出现这种错误呢? 正常的mysql_connect 之后调用 mysql_close()关闭连接 但在连接错误时,会者mysql_real_query()出现错误退出时,可能忘记mysql_close(); 所以在程序return 之前一定要判断是否close(),最稳妥的方法就是在写任何函数时都只有一个出口! 还有可以通过修改mysql配置文件来加大允许连接的数量! 有时你的服务器是经常出现这样的错误呢: 错误信息如下: Can not connect to MySQL server Error: Too many connections Errno.: 1040 Similar error report has beed dispatched to administrator before. 从官方文档知道linux上面编译安装的mysql默认的连接为100个 文档:http://dev.mysql.com/doc/refman/5.0/en/too-many-connections.html mysql官方告诉我们需要修改max_connections的值,那么我们怎么去修改呢?有两种方法 1、修改配置文件文件 修改/etc/my.cnf这个文件,在[mysqld] 中新增max_connections=N,如果你没有这个文件请从编译源码中的support-files文件夹中复制你所需要的*.cnf文件为到 /etc/my.cnf。我使用的是my-medium.cnf,中型服务器配置。例如我的[mysqld]的内容如下 [mysqld] port = 3306 socket = /tmp/mysql.sock skip-locking key_buffer = 160M max_allowed_packet = 1M table_cache = 64 sort_buffer_size = 512K net_buffer_length = 8K read_buffer_size = 256K read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8M max_connections=1000 由于对mysql还不是很熟悉,所以很多参数没有修改。哈哈。。 2、非使用mysqld脚本自动启动的用户。 修改$MYSQL_HOME/bin/mysqld_safe文件 例如:/usr/local/mysql/bin/mysqld_safe这个文件 grep -n ‘max_connection’ $MYSQL_HOME/bin/mysqld_safe 修改对应行号的max_connections参数值 来自:http://www.2cto.com/database/201306/218126.html 问题二:当在测试时出现[root@iZ25z0ugwgtZ mysql]# php test.php PHP Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /data/codesdev/testdemo/mysql/test.php on line 2 Connect failed: No such file or directory 将'localhost'修改为'127.0.0.1'之后链接正常! 当主机填写为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: On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option. 这个问题有以下几种解决方法: 使用TCP/IP代替Unix socket。即在连接的时候将localhost换成127.0.0.1。 修改MySQL的配置文件my.cnf,指定mysql.socket的位置: /var/lib/mysql/mysql.sock (你的mysql.socket路径)。 直接在php建立连接的时候指定my.socket的位置(官方文档:mysqli_connect)。比如: $db = new MySQLi('localhost', 'root', 'root', 'my_db', '3306', '/var/run/mysqld/mysqld.sock') 如果哪里没有说清楚或者说错了,欢迎提出了~~ 来自:https://segmentfault.com/q/1010000000328531 Generated by Jackxiang's Bo-blog 2.1.1 Release