PHP is not connected to the MySQL problem solution

Source: Internet
Author: User

Phenomenon 1

Found in PHP error log:
PHP Warning:mysqli::mysqli (): (hy000/2003): Can ' t connect to MySQL server on ' XXX.XXX.XXX. XXX ' (In/u1/www/xxxx.php on line 10
PHP Warning:mysqli::close (): couldn ' t fetch mysqli in/u1/www/xxxx.php on line 11
inference: Only in high concurrency environments

Diagnostic Analysis:

Grab the package through the MySQL database, no exception found. The target is also transferred to the PHP server.

BTW:
Linux open selinux connection mysql in the test basically belong to 1ms+, Ban SELinux after about 0.96. The SELinux is still to be banned.
Since I suspect that PHP is a problem, write a program test (after the SELinux is banned):
Cat tconn.php

The code is as follows Copy Code

function Microtime_float ()
{
List ($usec, $sec) = Explode ("", Microtime ());
Return ((float) $usec + (float) $sec);
}

$time _start = Microtime_float ();
For ($i =0 $i <30000; $i + +) {
$DBH =new mysqli ("XXX.XXX.XXX.XXX", "WUBX", "WUBXWUBX", "UserDB", 3308);
$DBH->close ();
}
$time _end= microtime_float ();
$time _use= ($time _end-$time _start)/30000;
Print "$time _usen";
#php tconn.php
0.00090954260031382

Run it again and start a large number of errors.

PHP warning:mysqli::mysqli (): (hy000/2003): Can ' t connect to MySQL server on ' XXX.XXX.XXX. XXX ' (In/u1/www/xxxx.php on line 10
PHP Warning:mysqli::close (): couldn ' t fetch mysqli in/u1/www/xxxx.php on line 11
After aborting the program, the

#strace PHP tconn.php Run

Get:

Connect (3, {sa_family=af_inet, sin_port=htons (3308), sin_addr=inet_addr ("XXX.XXX.XXX.XXX")},) =-1 Eaddrnotavail ( Cannot assign requested address)
Shutdown (3, 2/* Send and receive */) =-1 Enotconn (transport endpoint is not connected)
See this probably understand is the local network may not register, it is no wonder in the MySQL grab bag look also normal.

It seems that local TCP cannot be reused. Change the test

The code is as follows Copy Code
Sysctl-w net.ipv4.tcp_tw_reuse=1;

In the second test the problem did not exist. After touching this one, and changing the/etc/sysctl.conf add:

The code is as follows Copy Code

Net.ipv4.tcp_max_syn_backlog = 819200
Net.core.netdev_max_backlog = 400000
Net.core.somaxconn = 4096
Net.ipv4.tcp_tw_reuse=1
Net.ipv4.tcp_tw_recycle=0
#sysctl-P

Problem solving

Phenomenon 2

MySQL. Test connection mysql hint ' Fatal error:call to undefined function mysql_connect () "Environment J is: Windows XP SP2 en, APACHE2.2,MYSQL5.1RC.P hp5.28.

Does this hint that PHP is not loaded into the MySQL-connected library file? After starting Apache server. I tried to delete ' php5ts.dll ' and ' libmysql.dll '. The hint cannot be deleted. Indicates that the program is using these two library files. The description is loaded. (There are, of course, many ways to test.) For example, you can use some software to view all library files loaded by the program service. It can be, too. AP also says that there is a problem with php.ini settings. Then I don't look at anything else. I focus on the php.ini configuration.

In no superstition php.ini is right. Finally found out. The line was missing from the php.ini.

Phpinidir "Your PHP directory"

# (for example: Phpinidir "c:/php")

Restart the Apache server, and then. Use the usual methods on the Internet


The following are the referenced contents:

The code is as follows Copy Code
<?php
$link =mysql_connect (' localhost ', ' username ', ' password ');
if (! $link) echo "Failed!";
else echo "Success!";
Mysql_close ();
?>


Test it. It's okay.


Summarize these questions

1. First of all, troubleshoot network problems and firewalls


This is necessary, if you can not even MySQL server, then access to what? How to check it?           Ping Ping 192.168.0.11 Ping, then check the 3306 port is not blocked by the firewall ping 192.168.0.11:3306 Or simply turn off the firewall, service iptables stop (Redhat) or UFW disable (Ubuntu) This step is OK, start next:


2. To troubleshoot no access rights

When it comes to access, MySQL assigns a user to a host, such as my host is designated as 192.168.0.5, then this account can only. 5 This machine access, the other machine with this account access will prompt no permissions.           The host specified as% means that all machine access is allowed. Generally speaking, for security reasons, follow the principle of least privilege, the issue of permission is not much said, do not check the manual. If you've identified permissions, proceed to the next step:

3. To troubleshoot the MySQL configuration

Check the MySQL configuration file, Linux under the MySQL configuration file called my.cnf windows called My.ini, check this configuration item: –BIND-ADDRESS=IP

Reference to a passage in the manual:

The IP address to bind to. Only one address can is selected.           If This option is specified multiple times, the last address given is used. If no address or 0.0.0.0 is specified, the server listens in all interfaces.

Bound IP, can only bind to one IP, if the binding of multiple IP, the last one is bound. If there is no binding or binding 0.0.0.0, the server listens on all clients.


I have been this thing has been miserable, once engaged in an afternoon did not fix, check the network pass, check the permissions are no problem, the client is not even on, a look at the manual understand. So what's the problem or should you read the manual?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.