Php cannot connect to mysql

Source: Internet
Author: User
Tags apache log
Problem 1: install mysql and php on this book today, everything is installed normally (php@IIS7.5-fastCGI ). However, when you use phpmyadmin to connect to the test, the error quot; #2002 cannot connect quot; occurs. use phpmyadmin to connect to the test... problem 1: install mysql and php on this book today, everything is installed normally (php@IIS7.5-fastCGI ). However, the error "#2002 unable to connect" occurs when you use phpmyadmin to connect to the test. the same problem occurs with other programs.

Check that the mysql extension of php is correctly installed, and the mysql service is started normally-that's strange. The navicat connection is connected again. it seems that communication between php and mysql is faulty.

Baidu has a problem. The problem is thrown out: the mysql_connect () function database server (server) parameter is currently set to localhost, but it can be accessed again after it is changed to, the system hosts file does not provide resolution from to localhost.

Solution (take Windows 7 as an example): find the C: \ Windows \ System32 \ drivers \ etc \ hosts file. the default value is read-only. copy the file to another path, such as the desktop, open the file (open mode: notepad) and add localhost at the end of the file. if The localhost already exists, remove "#".

Problem 2: php scripts can run normally, such as phpinfo ().

As long as the "mysql_connect" function appears, no response is returned, and no error is reported, such as $ conn = mysql_connect ("", "root", "xxxx ") or die ("MYSQL database connection failed :". mysql_error ());

Error. log in the apache log Directory

PHP Warning: PHP Startup: Unable to load dynamic library 'C: \ php \ ext \ php_mysql.dll'

Solution: Copy "libmysql. dll" to the bin directory of apache. Restart and observe error. log. if the error disappears, congratulations.

In addition, you can copy "libmysql. dll" php_mysql.dll "to windows/system32.

Question 3:


The following error occurs during running: fatal error: call to undefined function mysql_connect () in var/www/html/mytest. php.

Solution: set extension_dir in 1. php. ini to "d: \ apache2 \ php \ ext" absolute path. otherwise, errors may occur in some environments.

Enable the following two functions


2. copy libmysql. dll (under the php directory) and php_mysql.dll (under the php/ext directory) to the c: \ windows \ system32 directory (libmysql. the dll must be in the php Directory, for example, libmysql in the directory D: \ apache2 \ php. dll, php_mysql.dll in D: \ apache2 \ php \ ext)

3. test

1> open the command line first:) choose Start> mysql> mysql5 server> MySQL Command Line Client, if it fails, mysql installation fails. uninstall and reinstall it.

B) enter the password // if this step is normal, the password of the root account is correct. If the password fails, the account password is incorrect.

2> create a file to the home directory and save it as testmysql. php, enter http: // localhost/testmysql in the browser. php (or http: // localhost: 81/testmysql. php). If not, check if apache has loaded the php and mysql methods.

Add the following three lines to the httpd. conf file supported by apache support for php5:

PHPIniDir "D:/ccopen/php5/"LoadModule php5_module "D:/ccopen/php5/php5apache2_2.dll"AddType application/x-httpd-php .php

Remove the following two semicolons from php. ini:


Copy php_mysql.dll and libmysql. dll to system32 and restart the apache server.

Article link:

Save this article for favorites!

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