The MySQL installation process on Windows 95/98/NT/2000/XP/2003 is based on an executable setup program provided by MySQL AB. Once you download the zip file, all you have to do is extract its contents into a temporary directory and run the setup.exe application. After the setup.exe application installs the MySQL server and client programs, you're ready to start the MySQL server.
Visit the MySQL 4.0.x download page, at http://www.mysql.com/downloads/ mysql-4.0.html, and find the Windows section on the page. There are two options; you want the one that doesn't say "without installer." In other words, you want the installer version, but it is not explicitly named as such.
Clicking the Download link will take you to a page of mirror sites. Select the mirror site closest to you, and click on either the HTTP or FTP link to download the file. Using the HTTP method is usually quicker.
Once the zip file is on your hard drive, extract its contents to a temporary directory.
From the temporary directory, find the setup.exe file and double-click it to start the installation. You will see the first screen of the installation wizard, as shown in the following figure. Click Next to continue.
The second screen in the installation process contains valuable information regarding the installation location. The default installation location is C:\mysql, but if you plan to install MySQL in a different location, this screen shows you a few changes that you will have to make on your own. The information on this screen is also important for Windows NT users who want to start MySQL as a service. Read the information and note anything relevant to your situation, and then click Next to continue.
With the third screen in the installation process, you select the installation location. If you want to install MySQL in the default location, click Next to continue. Otherwise, click Browse and navigate to the location of your choice, and then click Next to continue.
The fourth screen has you select the installation method—Typical, Compact, or Custom. Select Typical, and click Next to continue.
The installation process now takes over and installs files in their proper locations. When the process is finished, you will see a confirmation of completion. Click Finish to complete the setup process.
MySQL is now installed on your system. However, you won't find any shortcuts installed in your Windows Start menu after an installation of MySQL from MySQL AB, so now you must start the process yourself. If you navigate to the C:\mysqlbin directory, you will find numerous applications ready for action. One of these applications is called WinMySQLadmin, and it's a great friend to Windows users who are just getting started with MySQL. If you double-click this file, it will start the MySQL server and place a stoplight icon in your task bar. If you right-click this icon, you can launch a graphical user interface to maintain and monitor your new server.
| Note |
The first time you access WinMySQLadmin, it will ask you to create a username and password. Go ahead and pick a username/password pair and continue. |
WinMySQLadmin will automatically interpret environment information, such as IP address, machine name, and so on. The tabs across the top allow you to view system information and also edit MySQL configuration options. To shut down the MySQL server and/or the WinMySQLadmin tool, right-click again on the stoplight icon in your task bar and select the appropriate choice. As long as the MySQL server is running, you can run additional applications through a console window, such as the MySQL monitor.
In the next section, you learn how to start MySQL manually and perform a few actions to familiarize yourself with the system.
In this section, you work with the MySQL utilities via the command line in a console window. When using MySQL with PHP, you'll issue the same types of commands, only within the context of the PHP code. Use the information in this section to familiarize yourself with the types of commands and responses you'll be working with later in the book.
To start MySQL manually (without using the GUI described previously), go to the Windows Start menu, choose Run, type c:\mysql\bin, and press Enter.
| Note |
If you installed MySQL in a different directory, substitute that directory name in the previous command. |
The MySQL process will now be running in the background. You can now connect to MySQL and create databases and tables.
Before going any further, you should know the following:
A database is a collection of tables.
A table contains a set of records, also referred to as rows.
All records have the same number of fields.
Each field categorizes a piece of a data.
In this section, you'll conquer the first element and create a database. The utility to use is the mysqladmin program, which allows you to administer MySQL from the command line.
Go to the Windows Start menu, choose Run, type mysqladmin create testDB, and press Enter.
The window will briefly flash and then close when the command has been processed. Next, you'll add a table to the testDB database.
In this section, you create a table within the database you created in the preceding section. The utility to use is the mysql program, which allows you to work within the MySQL database system from the command line.
Go to the Windows Start menu, choose Run, type mysql, and press Enter.
When the MySQL Monitor starts, it provides its own prompt. At this prompt (mysql>), you type commands to create tables, explain tables, insert data, select data, and so on. Get used to ending your commands with a semicolon (;), because it's a common instruction terminator that is used in PHP as well.
Now that you've connected to the MySQL Monitor, you need to tell it which database to use.
At the prompt, type use testDB; and press Enter.
The MySQL Monitor will respond with Database changed if the database exists and you have permission to access it.
It's time to create a test table. This table will have a column for an ID number and a column for some text.
| Note |
For more information about the specifics of creating tables, see Appendix D, "Database Normalization and SQL Reference." |
At the prompt, type create table test_table (test_id int, test_note text); and press Enter. This statement creates a table called test_table. Within the table, it creates a column called test_id of type int (integer). It also creates a column called test_note of type text.
The MySQL Monitor will respond with Query OK. It will also tell you how many rows were affected and how long it took to complete the task.
Verify the table creation by typing show tables; and pressing Enter.
The MySQL Monitor will respond with a list of all the tables in the current database.
To verify the field names and types in a specific table, use the explain command. In this case, type explain test_table; and press Enter.
The MySQL Monitor will respond with a list of all the fields and their types in the selected table. This is a very handy command to use to keep track of your table design.
It's time to insert a few rows of data into your table, because this is getting pretty boring. The first row will have an ID of 1, and the note will be "This is a note."
To insert this row, type insert into test_table values('1', 'This is a note.'); and press Enter.
The MySQL Monitor will respond with Query OK. It will also tell you how many rows were affected and how long it took to complete the task.
Insert another row by typing insert into test_table values('99', 'Look! Another note.'); and pressing Enter.
| Note |
For more information about the specifics of inserting data into tables, see Appendix D, "Database Normalization and SQL Reference." |
Now that you have some data in your table, even if it is only two rows, it's time to get familiar with selecting data. Keep the MySQL Monitor open, because you'll be using it in the next section as well.
The SELECT command is very powerful and will likely be the command you use most often when working with PHP and MySQL. You can find more information about the SELECT command in Appendix D, "Database Normalization and SQL Reference," but for now, let's do some simple data selections.
At the prompt, type select * from test_table; and press Enter.
This command simply selects all fields from all rows (that's what the * does) in the table called test_table and returns the data to the screen in a nicely formatted table. The MySQL Monitor tells you how many rows were returned and how long it took the query to run.
Impose a little order on the results. Try to order the results by ID number—largest number first.
At the prompt, type select * from test_table order by test_id desc; and press Enter.
The result now shows the row with a test_id of 99 as the first row in the table. The desc in the command stands for descending. There is another option, asc, which stands for ascending. Ascending order is the default order.
The next section is for the installation of MySQL on Linux. If you don't have two machines, skip ahead to Chapter 2, "Installing Apache," to install the Apache web server.
This section takes you through the installation process of MySQL 4.0.x (standard) on Linux, using the distribution from MySQL AB. If you're using another flavor of Unix, download the appropriate files and follow the instructions included with the distribution.
The recommended installation method for MySQL is with RPMs. There are several RPMs that make up a full distribution, but for a minimal installation you need the following:
MySQL-server-VERSION.i386.rpm—The MySQL server.
MySQL-client-VERSION.i386.rpm—The standard MySQL client programs.
To download these files, visit the MySQL 4.0.x download page at http://www.mysql.com/downloads/mysql-4.0.html and find the Linux x86 RPM Downloads section (or IA64 or AMD64, depending on your architecture). When you click on the download link for one of the packages, you will be taken to a page of mirror sites. Select the mirror site closest to you, and download the files.
When the files are downloaded to your system, perform the minimal installation by typing the following at your prompt, replacing VERSION with the appropriate version number of your downloaded files:
#prompt> rpm -i MySQL-server-VERSION.i386.rpm MySQL-client-VERSION.i386.rpm
If the RPM method doesn't work for you, you can also install MySQL from a binary distribution, which requires gunzip and tar to uncompress and unpack the distribution. It also requires the capability to create groups and users on the system.
In the first series of commands, you will add a group and a user, and then unpack the distribution, as follows:
At the prompt, type groupadd mysql.
At the prompt, type useradd -g mysql mysql.
At the prompt, type cd /usr/local.
At the prompt, type gunzip < /path/to/mysql-standard-VERSION-OS.tar.gz | tar xvf -.
| Note |
You can install MySQL in any directory. If you do not use /usr/local/ as in this example, be sure to modify subsequent commands appropriately. |
To create a link with a shorter name, type ln -s mysql-VERSION-OS mysql.
Change directories by typing cd mysql.
Once the distribution is unpacked, the README and INSTALL files will walk you through the remainder of the installation process for the version of MySQL you've chosen. In general, the next series of commands will be used:
Type scripts/mysql_install_db to run the MySQL install script.
Type chown -R root /usr/local/mysql to change ownership of the mysql directory.
Type chown -R mysql /usr/local/mysql/data to change ownership of the mysql/data directory.
Type chgrp -R mysql /usr/local/mysql to change the group of the mysql directory.
Type chown -R root /usr/local/mysql/bin to change ownership of the mysql/bin directory.
If you have any problems during the installation of MySQL, the first place you should look is the "Problems and Common Errors" chapter of the MySQL manual, which is located at http://www.mysql.com/doc/P/r/Problems.html. Some common problems include:
Incorrect permissions do not allow you to start the MySQL daemon. If this is the case, be sure you have changed owners and groups to match those indicated in the installation instructions.
If you see the message Access denied when connecting to MySQL, be sure you are using the correct username and password.
If you see the message Can't connect to server, make sure the MySQL daemon is running.
In the next section, you learn how to start MySQL and perform a few actions to familiarize yourself with the system.
In this section, you work with the MySQL utilities via the command line in a console window. When using MySQL with PHP, you'll issue the same types of commands, only within the context of the PHP code. Use the information in this section to familiarize yourself with the types of commands and responses you'll be working with later in the book.
The MySQL distribution comes with a start-up script, called safe_mysqld, found in the bin subdirectory of the MySQL installation directory. Follow these steps to start this script:
If you're not already there, enter the MySQL parent directory by typing cd /usr/local/mysql at the prompt and pressing Enter.
Start the MySQL process by typing ./bin/safe_mysqld & and pressing Enter.
The MySQL process will now be running in the background, and you can connect to MySQL and create databases and tables.
Before going any further, you should know the following:
A database is a collection of tables.
A table contains a set of records, also referred to as rows.
All records have the same number of fields.
Each field categorizes a piece of a data.
In this section, you'll conquer the first element and create a database. The utility to use is the mysqladmin program, which allows you to administer MySQL from the command line.
At the prompt, type ./bin/mysqladmin create testDB and press Enter. You will be returned to the prompt if the database called testDB has been successfully created. Next, you'll add a table to that database.
In this section, you'll create a table within the database you created in the preceding section. The utility to use is the mysql program, which allows you to work within the MySQL database system from the command line.
At the prompt, type ./bin/mysql and press Enter.
The MySQL monitor will start. The MySQL Monitor provides its own prompt. At this prompt (mysql>), you will type commands used to create tables, explain tables, insert data, select data, and so on. Get used to ending your commands with a semicolon (;), because it's a common instruction terminator that is used in PHP as well.
Now that you've connected to the MySQL monitor, you need to tell it which database to use.
At the prompt, type use testDB; and press Enter.
The MySQL Monitor will respond with Database changed if the database exists and you have permission to access it.
It's time to create a test table. This table will have a column for an ID number and a column for some text.
At the prompt, type create table test_table (test_id int, test_note text); and press Enter. This statement creates a table called test_table. Within the table, it creates a column called test_id of type int (integer). It also creates a column called test_note of type text.
| Note |
For more information about the specifics of creating tables, see Appendix D, "Database Normalization and SQL Reference." |
The MySQL Monitor will respond with Query OK. It will also tell you how many rows were affected and how long it took to complete the task.
Verify the table creation by typing show tables; and pressing Enter.
The MySQL Monitor will respond with a list of all tables in the current database.
To verify the field names and types in a specific table, use the explain command. In this case, type explain test_table; and press Enter.
The MySQL Monitor will respond with a list of all the fields and their types in the selected table. This is a very handy command to use to keep track of your table design.
It's time to insert a few rows of data in your table, because this is getting pretty boring. The first row will have an ID of 1, and the note will be "This is a note."
To insert this row, type insert into test_table values('1', 'This is a note.'); and press Enter.
The MySQL Monitor will respond with Query OK. It will also tell you how many rows were affected and how long it took to complete the task.
Insert another row by typing insert into test_table values('99', 'Look! Another note.'); and pressing Enter.
| Note |
For more information about the specifics of inserting data into tables, see Appendix D, "Database Normalization and SQL Reference. |
Now that you have some data in your table, even if it is only two rows, it's time to get familiar with selecting data. Keep the MySQL Monitor open, because you'll be using it in the next section as well.
The SELECT command is very powerful and will likely be the command you use most often when working with PHP and MySQL. You can find more information about SELECT in Appendix D, "Database Normalization and SQL Reference," but for now, let's do some simple data selections.
At the prompt, type select * from test_table; and press Enter.
This command simply selects all fields from all rows in the table called test_table and returns the data to the screen in a nicely formatted table. The MySQL Monitor tells you how many rows were returned and how long it took the query to run.
Add a little order to the results. Try to order the results by ID number— largest number first.
At the prompt, type select * from test_table order by test_id desc; and press Enter.
The result now shows the row with a test_id of 99 as the first row in the table. The desc in the command stands for descending. There is another option, asc, which stands for ascending. Ascending order is the default order.
In the next chapter, you'll install the Apache web server and be one step closer to developing dynamic, database-driven websites!