Install and Configure MySQL for PHP Applications on IIS 7

By Ruslan Yakushev

February 14, 2008

Introduction

While Microsoft® SQL Server® 2008 is the recommended database to use when hosting PHP applications on an Internet Information Services 7 (IIS 7) and above Web server, you can also use MySQL as the database. Currently, many popular PHP applications use MySQL Server for data storage. Using MySQL requires hosting providers to include MySQL database support with the hosting packages.

MySQL cannot currently be installed with the Microsoft® Web Platform Installer (Web PI). This article provides guidance for installing MySQL manually.

Install MySQL Server on Windows Server 2008 or Windows Server 2008 R2

It is recommended that you install MySQL on a dedicated server rather than installing MySQL on the same server that is running IIS. The separation of database server and Web server makes overall installation more secure and manageable and avoids resource contentions between the database and Web server processes.

1. Download MySQL Community Server.

a. We recommend downloading Windows® Installer.

2. Start Windows Installer, or extract all the files from the archive, and then start Setup.exe.

3. You can use a Typical Setup or customize the installation to suit your needs.

4. Once the installation wizard is completed, it is recommended that you leave the Configure the MySQL Server now check box selected.

Configure a MySQL Instance

1. Run the MySQL Server Instance Configuration Wizard, and then choose the configurations options that most closely match your environment.
For more information, see the Server Instance Configuration Wizard.
Best practice recommendations are as follows:

a. Click Next in the Instance Configuration Wizard.

b. Select Detailed Configuration, and then click Next.

c. Select a server type that best suits your environment. It is recommended to set up a separate MySQL server; when prompted to select a server type, select Dedicated MySQL Server Machine, and then click Next.

d. Select a database option, and then click Next.

Select either the Multifunctional Database or Transactional Database Only options if you are using the InnoDB storage engine or the high-speed MyISAM storage engine (for example, if the Web applications on your server require multi-statement transactions, advanced isolation levels and row-level locking, foreign key constraints, or atomic, consistent, isolated, and durable [ACID] features). These options provides fully ACID transactional capabilities, but at the cost of more aggressive usage of disk space and memory.

Otherwise, use the Non-Transactional Database Only option, which is optimized for high-performance SELECT operations. It has low overhead, in terms of memory usage and disk utilization, but at the cost of not supporting transactions.

e. Choose the option that sets the number of concurrent connections you need.

Note: Connections require memory; if the number you choose is too big, your server may not have enough memory.

f. You may adjust networking settings to suit your environment or accept defaults, and then click Next.

g. Select the default character set that best suits you, and then click Next.

h. We recommend enabling both Windows options here. Select both check boxes, and then click Next.

i. Type the password you want to use for the root account, and then click Next.

j. Click Execute to apply your settings.

k. Click Finish to close the wizard.

2. For PHP to work with MySQL, it is necessary to perform the following modifications to the Php.ini file:

a. Confirm that the extension_dir points to the folder where all PHP loadable extensions are located, frequently in the Ext folder (for example, extension_dir=”.\ext”).

b. Enable dynamic extension for MySQL by uncommenting the corresponding line for the MySQL extension: extension=php_mysql.dll

c. Save and close the Php.ini file.

Secure MySQL

1. Remove the anonymous database account (if it exists). Open the MySQL command prompt by clicking Start -> All Programs -> MySQL -> MySQL Server 5.1 -> MySQL Command Line Client:

2. Enter the password for the root account.

3. Once logged on to MySQL, use the following sequence of commands:

mysql> use mysql;
Database changed
mysql> DELETE FROM user WHERE user = '';
Query OK, 2 rows affected (0.03 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.05 sec)

4. Next, restrict the root account to log on only from localhost. Open a MySQL command prompt, and use the following sequence of commands:

mysql> use mysql;
Database changed
mysql> DELETE FROM user WHERE user = 'root' AND host = '%';
Query OK, 2 rows affected (0.03 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.05 sec)

5. Change the name of the root user with the following sequence of commands from the command prompt:

mysql> USE mysql;
Database changed
mysql> UPDATE user SET user='johndoe' WHERE user='root';
Query OK, 1 row affected (0.19 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.23 sec)

Provision the User and Database

1. To provision a new user, type the following command from the MySQL command prompt:

mysql>CREATE USER ‘some_username’ IDENTIFIED BY ‘some_password’;
Query OK, 0 rows affected (0.00 sec)

2. The newly created user does not have any privileges on the MySQL server by default. To create a new database, type the following command:

mysql>CREATE DATABASE IF NOT EXISTS some_database_name;
Query OK, 1 row affected (0.00 sec)

3. To grant access to this database for a particular user, type the following command:

mysql> GRANT ALTER,
-> ALTER ROUTINE,
-> CREATE,
-> CREATE ROUTINE,
-> CREATE TEMPORARY TABLES,
-> CREATE VIEW,
-> DELETE,
-> DROP,
-> EXECUTE,
-> INDEX,
-> INSERT,
-> LOCK TABLES,
-> SELECT,
-> UPDATE,
-> SHOW VIEW
ON some_database_name.* TO 'some_username';

Configure PHP to Access MySQL

1. Open the c:\php\php.ini file with your favorite text editor.

2. Uncomment the following lines by removing the semicolon:

extension=php_mysqli.dll
extension=php_mbstring.dll
extension=php_mcrypt.dll

3. Restart the IIS service by clicking on Start, selecting the Search Field, typing iisreset, and then pressing ENTER.

4. If all went well, you should see the mysqli section on the PHP information page created earlier (http://localhost/phpinfo.php).

Figure 1: The mysqli section on the PHP information page

Best Practices for MySQL

  • Enable TCP/IP Networking
    This is the default. Keep the TCP port that MySQL uses to listen at 3306. If the database will be running on a separate system from the Web server, select the Add firewall exception for this port check box.
  • Include Bin Directory in Windows PATH
    This makes the MySQL utilities available from the command prompt or from Windows PowerShell™.
  • Create an Anonymous Account
    The default is to keep this disabled. Adding anonymous user support may create a security risk for the database; additionally, enabling anonymous users causes the GRANT statements used to set up database to be unreliable.

Links for Further Information

MySQL Server Web site.

Using the MySQL Installation Wizard.

Installing MySQL from a Noinstall Zip Archive.

MySQL Windows Installation.