Install and Configure MySQL for PHP Applications on IIS 7

  • Author: Ruslan Yakushev
  • Published on February 14, 2008 by ruslany
  • Updated on November 15, 2009 by ruslany
  • Tags: MySQL PHP

Introduction

While Microsoft® SQL Server® 2008 is the recommended database to use when hosting PHP applications on an Internet Information Services 7 (IIS 7) 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 7. 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.

Related Content

Comments

After completing steps 1 and 2 in "Change the MySQL Service Account" the service will not restart in Step 3. The error below is shown when restarting the service. I've searched the web and don't see any applicable solutions.

Server 2008/IIS7/mysql-essential-5.0.51b-winx64
The permissions look correct and I don't see any other errors.

Any ideas on how to fix the problem?
Thanks.


The error is:
C:\Program Files\MySQL\MySQL Server 5.0\bin>net start mysql
The MySQL service is starting.
The MySQL service could not be started.
A system error has occurred.
System error 1067 has occurred.
The process terminated unexpectedly.

Jun 21 2008 by EC23893

By the way... when I change the account back to "Local System Account" it works fine. So, the problem seems to be with the Local Service account.

Jun 21 2008 by EC23893

I tried all steps, it works fine for me. Which versin of mysql you tried? I installed MySql 5.0.

Jun 27 2008 by web_php

Thanks for the how-to, it was useful. However, I tried following the steps in the "Securing MySQL Server..." section, and got the same result as EC23893.

Instead of using the Local Service account, I created a local account and gave it the same local group policy rights as the Local Service account, including some others given to accounts used by my SQL 2005 Express instance. After this, I gave the local account full control of the C:\Program Files\MySQL directory tree (it already had read-access to C:\, from membership in the Users group).

Attempting to start the service after doing this yields "System error 1067", with the MySql service terminating unexpectedly. If I add the local account to the Administrators group, the MySql service starts fine. If possible, it would be very helpful to know specifically which other rights and directory permissions are needed by the MySql service, in order for it to run properly, so we don't have to try to isolate them by trial and error. The last link you posted to thewebhostinghero.com site doesn't include that kind of detail either. Any chance for an update to your article? :)

Nov 30 2008 by Pseudothink

When you install MySQL 5.1 on Windows Server 2008, the my.ini file is configured to use c:\ProgramDATA\MySQL\MySQL Server 5.1\data as the datadir. This being the case, you must change the permissions of that directory in the same way you changed the permissions of the install directory.

I did the following after step 2 in the "Change the MySQL Service Account" section above:
C:\>icacls "c:\ProgramData\MySQL\MySQL Server 5.1" /grant "NT AUTHORITY\LOCAL SERVICE":(OI)(CI)(M)

You could also change the datadir setting in the my.ini file to the installation directory instead of granting permission to the data directory created by setup.

HTH,
Joe

Apr 03 2009 by joe.davis

Submit a Comment

You must Log In to comment.

Microsoft Communities