Installing MySQL 5 on PHP 5 on Windows

December 18, 2008

This tutorial explains how to install MySQL server and get it to work with PHP.

MySQL is a free database management system that is used by the majority of web applications today. You can install MySQL by itself, but much of its usefulness comes from using it together with PHP.

This tutorial assumes you have already installed and configured Apache web server and PHP.

Start off by downloading MySQL community server from the official website.

download-mysql-link

Choose the Windows download (Windows x64 if applicable) and select the “pick a mirror” for Windows ZIP/Setup.EXE.

You will be prompted to register with MySQL, you can skip that by clicking the “No thanks” link underneath.
mysql-download-nothanks

Select the mirror closest to you and download.

Once downloaded, extract the setup.exe file and run.
mysql-install-welcome

Next you will be prompted to choose the installation type. For this tutorial we will choose typical, which contains all the files and features you should need. Custom lets you pick and choose which items to install and complete includes files that MySQL developers may need. Click next.
mysql-install-type

Now click install.
mysql-install-copying-files

Some more information about MySQL will appear, click next.

After MySQL is done installing we will need to configure our MySQL server. Click finish to start the configuration wizard.
mysql-install-configure-now

At The configuration welcome screen, click next
mysql-configure-welcome

You will be prompted to choose a configuration type. For this tutorial we will use the standard configuration. The detailed configuration is for setting up servers for specific purposes to increase performance. Click next.
mysql-configure-type

Next a dialog will prompt the user to install MySQL as a service, this means that MySQL server will run even if no one is logged on (For example, f the computer is sitting at the login screen). It also asks whether to include the bin directory in the Windows path. It is highly recommended you check this option as it makes running MySQL commands from the command line much easier (otherwise you have to change to the MySQL bin directory every time).
mysql-configure-service

Next you will set a password for the root account. Make sure the “Modify Security Settings” checkbox is checked and enter a password. This is the password you have to supply any time you want to connect to MySQL. It is recommended you not create an anonymous account for security reasons. Click next.
mysql-configure-account-pw

Now we are ready to apply the configuration settings, click execute. If you have Antivirus or firewall software installed, you may be prompted to allow MySQL to use this port, allow.
mysql-configure-firewall

Once completed, MySQL is installed and ready to use. However, PHP may not yet be configured to use MySQL. To test whether it is, open up notepad and type the following lines of code

$con = mysql_connect('localhost', 'root', 'yourpassword');
if (!$con)
die('Could not connect: ' . mysql_error());
}
else
{
echo “It Works”;
}
?>

mysql-test-script

Make sure “All files” is selected as notepad likes to save everything with a .txt extension if you have text file selected.
mysql-save-testscript

Save the file as testmysql.php and save it in the document root for your web server (ex: htdocs in the apache install directory). Make sure your web server is running and navigate your browser to http://localhost/testmysql.php.

If you see “It works” on the page, then everything is setup and we are done!
If you see the actual code then PHP is not configured correctly for Apache.
If you see a “Could not connect” message, it likely means you mistyped your password or the MySQL service is not running.
If you see a blank page, it means that PHP has not been configured to work with MySQL.

If you see the blank page, it means you did not check the MySQL item when installing PHP (it is not enabled on the default installation). If this is the case, we have a few more steps to configure PHP.

Open the php.ini file located in your PHP installation directory (ex: C:\Program Files\PHP\php.ini) in notepad or any text editor. Search for the line ‘Dynamic Extensions’ and below that add following line

extension=php_mysql.dll

mysql-php-enable-extension
Make sure to save the file.

Go to the download section of the official PHP download page. Download the PHP 5.x zip package (not the installer). Once downloaded open the zip file and find two DLL files: libmysql.dll in the main directory of the zip file and php_mysql.dll in the ext directory.
mysql-php-extention

mysql-php-lib-file

Copy both files into your PHP install directory (this is the default PHP extensions directory, search for the line ‘extension_dir’  in your php.ini file to verify where your extension directory is located).

Now that both DLLs are copied and the extension is enabled in the php.ini configuration file, restart Apache either from the icon in the system tray or from the services manager in the control panel.

Try to open the testmysql.php page in your browser again. If you still see a blank page, make sure you followed all the steps and that you copied the DLL files into the right extension directory. If you are still having problems, look at the Apache error log in the Apache logs directory (ex: C:\Program Files\Apache Software Foundation\Apache2.2\logs\error.log).

Conclusion and further reading.

Apache, PHP, and MySQL together form a powerful combination. With the three, you can create powerful web applications without any expensive commercial tools. Not only that, but having the three allows you to install useful web applications such as Wordpress, Drupal, and Zencart.

For more information on programming with PHP and MySQL check out our PHP programming tutorials.

del.icio.us Digg Facebook Google reddit SlashDot StumbleUpon Technorati

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Comments

One Response to “Installing MySQL 5 on PHP 5 on Windows”
  1. johnny says:

    Thanks for the good post