PHP is not connected to MySQL problem resolution _php tutorial

Source: Internet
Author: User
PHP is not connected to MySQL for a number of reasons commonly used may be the function is not opened or MySQL database configuration problems, let me introduce you to the PHP is not connected to MySQL some of the problems of analysis and solutions.

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:

Grabbed the packet through the MySQL database and found no exception. The target is also transferred to the PHP server.

BTW:
Linux open selinux connection mysql in the test basically belongs to 1ms+, disable SELinux after about 0.96. SELinux is still going to be banned.
Since you suspect that PHP is a problem, write a program test (after you disable SELinux):
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 again to 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 the procedure is aborted, 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 that the local network may not be registered, it is no wonder that the MySQL grab packet 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;

The secondary test problem does not exist. Touch it on this one and change it by the way./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 Yes: Windows XP SP2 en, APACHE2.2,MYSQL5.1RC.P hp5.28.

This hint, could it be that PHP was not loaded into the library file connected to MySQL? After you start Apache server. I try to remove the ' php5ts.dll ' and ' Libmysql.dll '. The prompt cannot be removed. Indicates that a program is using both 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. Also the AP is also said to be php.ini setup has a problem. Then I don't look at anything else. I'll focus on the php.ini configuration.

In the absence of superstition php.ini is correct. Finally found that the php.ini in this line.

Phpinidir "Your PHP directory"

# (ex: Phpinidir "c:/php")

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


The following is the referenced content:

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


Let's test it.


Summarize these questions

1. First to troubleshoot network problems and firewall issues


This is necessary, if you don't even have a MySQL server, what do you have to visit? How do you check it? Ping the ping 192.168.0.11 ping, then go to check whether the 3306 port is blocked by the firewall ping 192.168.0.11:3306 or simply turn off the firewall, service iptables stop (REDH at) or UFW disable (Ubuntu) This step is no problem, start next:


2. To troubleshoot any access rights

When it comes to access, MySQL assigns a user a host, such as my host designated as 192.168.0.5, then this account can only. 5 This one machine access, other machines with this account access will prompt no permissions. The host specified as% indicates that all machine access is allowed. Generally due to security considerations, follow the principle of least privilege, the problem of permission is not much to say, do not check the manual. The next step is to make sure the permissions are fine:

3. To troubleshoot the configuration of MySQL

Check the MySQL configuration file, Linux under MySQL configuration file called my.cnf Windows under the call My.ini, check this configuration entry: –bind-address=ip

Quote a passage from the manual:

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

Bound IP, only one IP can be bound, and if more than one IP is bound, the last binding will prevail. If there is no binding or binding 0.0.0.0, the server listens to all clients.


I have been this thing has been miserable, once engaged in an afternoon did not fix, check the network pass, check the permission is not a problem, the client is dead or alive even, a look at the manual understand. So if there's any problem, read the manual.

http://www.bkjia.com/PHPjc/632926.html www.bkjia.com true http://www.bkjia.com/PHPjc/632926.html techarticle PHP is not connected to MySQL for a number of reasons commonly used may be the function is not opened or MySQL database configuration problems, let me introduce you to the PHP is not connected to MySQL some of the problems of analysis and solution ...

  • 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.