1.
Installing MySQL

In this chapter, we will see how to install MySQL and set it up for normal use. In this chapter, we will cover the following:

• Installing on Linux

• Installing on Windows

• Installing on OS X

• Configuring your system

• Setting the root password

• Deleting anonymous accounts

We will begin by installing MySQL on your system. If you already have a copy of MySQL 4.1 or above installed, you can skip ahead to the section “Setting the Root Password

You should first download the version of MySQL that is appropriate for your system. MySQL is available either as source code or as a binary distribution for most systems. If you are learning to use MySQL, we recommend that you download the binary for your system because it is much easier to install. Additionally, MySQL's binaries are tuned for optimal, stable performance.

For each platform, MySQL is available in three versions: Standard, Max, and Debug. For the examples in this book, you will need the Max version. This comes with various useful options enabled, such as InnoDB transaction-safe tables, which we will use extensively.

MySQL is available in several versions at a time: the current version, the most recent version, and a couple of development versions in different stages. This book was written for MySQL 4.1.

You can download MySQL from

www.mysql.com/downloads/index.html

We will discuss how to install MySQL binaries on Linux, Windows, and OS X. MySQL is available for many other systems, but if you have one of these others, you will have to consult the MySQL manual for details on installation.

Installing on Linux

Depending on your Linux version and how recently you installed it, you will quite likely already have a version of MySQL on your system, but it is also very likely that you won't have the newest version.

We recommend that you install MySQL on Linux from a MySQL-provided RPM—assuming that your system supports this package management format. We suggest that you use the versions of the RPMs available from MySQL, instead of any that come from the vendor of your Linux distribution, particularly if you are just beginning with MySQL. The MySQL binaries are updated more promptly than any distribution, so they include newer improvements and bug fixes. Also, your directory structure will be set up as documented in the MySQL manual, which can make finding things easier to begin with.

Several RPM files come in the distribution. The ones you will need in order to run the MySQL server and client are MySQL-server-VERSION.i386.rpm, MySQL-Max-VERSION.i386.rpm, and MySQL-client-VERSION.i386.rpm. (The word VERSION will be replaced with the appropriate version number.)

You can install the server and client by typing this in your shell:


rpm -i MySQL-server-VERSION.i386.rpm MySQL-client-VERSION.i386.rpm


This will also start mysqld, the MySQL server, and create the appropriate entries in /etc/init.d/ to start the server automatically for you when your system is started.

If you want to install MySQL in some other way—that is, not from an RPM—refer to the manual for detailed instructions.

Please understand that installing MySQL can be quite complex, and your experiences may differ due to previously installed versions of MySQL or your version of Linux.

Installing on Windows

To install MySQL on recent versions of Windows, you will need to be logged in as an administrator.

If you already have a version of MySQL installed, you will need to stop the server before installing a new version. You can do this with the following:


mysqladmin -u root -p shutdown


If you have MySQL installed as a service, you will need to remove the service. You can do this from the command prompt by typing this:


mysqld --remove


You may need to replace mysqld with the name of the server binary you originally installed as a service.

If you prefer a graphical tool, you can also remove the service from the Services Manager, located under Administrative Tools Services on Control Panel, or on some systems under Microsoft Management Console.

Please understand that installing MySQL can be quite complex, and your experiences may differ due to previously installed versions of MySQL or your version of Windows. Begin installing MySQL by unzipping your download file.

Depending on when you read this, the contents of this file will vary. At the time of writing, MySQL was installed by running setup.exe. However, MySQL AB, the company that makes MySQL, is planning an MSI installer. If you see a .msi file instead of setup.exe, you can install MySQL by double-clicking on the MSI file.

The setup program will give you the option to install MySQL in the default location (c:mysql) or elsewhere. This book assumes that you'll install it in the default location.

After MySQL is installed, you will need to create an options file to set up your initial MySQL configuration. You should do this before starting the server for the first time. Details of the configuration can be found in the section “Configuring Your System,” later in this chapter.

After performing your configuration, it is usually a good idea to start your server for the first time from the command line. (In a moment, we will look at how to install it as a service, but let's check whether it's working first.)

Open a command-prompt window and go to the directory where the MySQL server is located. If you have the standard installation, this will be c:mysqlin.

Type the following:


mysqld-max --standalone


This should give you a set of startup messages. Assuming that everything goes well, you can then install MySQL as a service.

Type the name of the server executable you plan to use followed by --install. For example:


mysqld-max --install


The mysqld-max executable is the one we recommend for use with this book.

There is one final thing you should do to make your MySQL installation fully functional. Some of the associated programs that come with MySQL are actually scripts written in the Perl language, so you will need a Perl engine to run them. Download and install ActivePerl from ActiveState at the following URL:

www.activestate.com/Products/ActivePerl/

ActivePerl comes as an MSI file. Download this file and install ActivePerl by double-clicking on it. You can stick with all the default options that the installer gives you for use with MySQL.

Installing on OS X

You can install MySQL on OS X from a package, assuming that you have OS X version 10.2 or newer.

If you already have a version of the MySQL server running, you will need to shut it down before installing a new one. You can do this by typing the following at a command prompt:


mysqladmin -u root -p shutdown 


To run the server, you will need to have an account called mysql. If you have OS X 10.2 or above, this account will already exist.

The file you download from MySQL is a .dmg file—that is, a disk image file. Begin by opening the Finder and double-clicking on the .dmg file.

You will now see the contents of the disk image file. Find the PKG file and double-click on it. For the purposes of this book, you can go along with all the default options that the installer gives you.

You can set up MySQL to start automatically on system startup by adding a StartupItem. You can do this by double-clicking on the file called MySQLStartupItem.pkg that should be located in your .dmg file.

After performing the steps contained in the section “Configuring Your System,” you can start the MySQL server by typing the following if you have installed the StartupItem:


sudo /Library/StartupItems/MySQL/MySQL start


If you have not installed the StartupItem, you can start the server with the following series of commands:


cd /usr/local/mysql
sudo ./bin/mysqld_safe


Now press Ctrl+Z. Finally, type the following command:


bg


If you have not installed the StartupItem, you will need to repeat these steps each time you restart your system and want to use MySQL.

Please understand that installing MySQL can be quite complex and your experiences may differ due to previously installed versions of MySQL or your version of OS X.

Configuring Your System

The initial MySQL configuration, as installed, will work as-is; however, some useful features are turned off by default, and it starts out insecure.

In this book, we recommend that you change the following configuration options:

• We will be using InnoDB tables, so we need to perform a basic setup for these tables.

• We recommend that you turn on binary logging in all situations. This is highly useful for disaster recovery.

• We recommend that you turn on slow query logging. This tracks slow queries (as you might expect from the name) and will help you optimize your applications.

MySQL uses options files to store configuration values. On Windows, your global options file can be located either in your Windows directory and named my.ini or in c:my.cnf. We recommend using my.ini because .cnf is sometimes used as a file extension by other applications.

Under Unix-like operating systems, the global options file is typically located in/etc/my.cnf. If you want to run more than one server per machine, you can keep server-specific information in your data directory in my.cnf for each server. You can also allow individual users to have separate options in their own account—these should be located in ~/.my.cnf (note the . before the filename).

Depending on your setup, you may or may not have an options file to begin with. Open up the file in your favorite text editor—Notepad will do if you are using Windows—or create it if it does not exist.

We suggest an initial options file as shown in Listing 1.1.

Listing 1.1. Suggested Options File


[mysqld]
# turn on binary logging and slow query logging
log-bin
log-slow-queries

# InnoDB config
# This is the basic config as suggested in the manual
# Datafile(s) must be able to
# hold your data and indexes.
# Make sure you have enough
# free disk space.
innodb_data_file_path = ibdata1:10M:autoextend
# Set buffer pool size to
# 50 - 80 % of your computer's
# memory
set-variable = innodb_buffer_pool_size=70M
set-variable = innodb_additional_mem_pool_size=10M
# Set the log file size to about
# 25 % of the buffer pool size
set-variable = innodb_log_file_size=20M
set-variable = innodb_log_buffer_size=8M
# Set ..flush_log_at_trx_commit
# to 0 if you can afford losing
# some last transactions 
innodb_flush_log_at_trx_commit=1


Most of this options file is based on the very simple InnoDB configuration file suggested in the MySQL manual. One point to note is that if you have an existing installation of MySQL with InnoDB and are adding this options file to it, you may have to comment out the line


set-variable = innodb_log_file_size=20M


by placing a # at the start of the line. (If you have an existing log file of a different size, MySQL will become confused.)

We will look further at configuration in Chapter 12, “Configuring MySQL.”

Checking Your System Works

After you have installed MySQL, set it up to run automatically on startup, and configured it, you should be able to connect to it.

You can do this by typing the following:


mysql -u root


You may need to supply a path to the mysql executable (for example, on Windows, typically c:mysqlinmysql -u root) or change your PATH.

This should log you in as the root (administrator) user to the MySQL monitor (a command-line interface to MySQL). You should see a welcome message similar to this:


Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4 to server version: 4.1.0-alpha-max-debug-log
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql>


Things that are likely to be different on your system are the connection id and the server version number. Don't worry about this.

If you get a message similar to


ERROR 2003: Can't connect to MySQL server on 'localhost' (10061)


it means the MySQL server is not running. You may want to restart your system so that the server has a chance to start automatically, or you can start the server manually by typing


mysqld --standalone


at a command prompt. (It's easiest to do this in one window and then open another window to log in.)

To log back out of the MySQL monitor, you can type this:


q


(That's a backslash followed by q followed by Enter.)

Don't log out yet, though. (If you have already done so, just go ahead and log back in.) There are a couple more administrative things you need to do before you can really begin using your MySQL installation. These things are important, so you'll want to continue following along in this chapter.

Setting the Root Password

As you probably noticed when you logged in just now, you had to supply a username—root—but not a password. The initial installation of MySQL has no root password set. It's really important to set this password for obvious security reasons. In the MySQL monitor, type the following:


set password for root@localhost=password('your password'),


Obviously, you should replace your password with whatever you would like the root password to be.

Log out (q) and then log back in. This time, you need to log in like this:


mysql -u root -p


MySQL then prompts you for your password. The -u means username, and the -p means log in with password.

Deleting Anonymous Accounts

MySQL creates some anonymous accounts that require no username to log in. We recommend that you delete these. The reasons behind this are covered in detail in Chapter 15, “Securing Your MySQL Installation,” in the section “Deleting Anonymous Accounts.” You can get rid of these accounts by typing this:


use mysql;
delete from user where User='';
delete from db where User='';
flush privileges;


Creating an Account for Basic Use

It is usually a good idea to use some account other than root for basic day-to-day work for security reasons.

We will briefly cover how to create a separate account now. For full details of how user accounts work in MySQL, read Chapter 11, “Managing User Privileges.”

Create an account for day-to-day use by typing the following:


grant create, create temporary tables, delete, execute, index, insert,
 lock tables, select, show databases, update
on *.*
to username identified by 'password';


Obviously, you should substitute your own username and password into this command.

You should now log out and log back in with the username and password you set. You will need to use a root account for some of the things we will do in the later chapters of this book, but this account we have just created has fewer privileges than root, so it is safer for day-to-day use.

Summary

By this stage, you should have a working MySQL installation ready to follow along in the rest of the book. You should have done the following:

• Downloaded the latest binary version of MySQL for your operating system from mysql.com.

• Installed it—either via the installation program (on Windows or OS X) or via RPM.

• Set up the server to run automatically on system startup, if so desired.

• Created an options file.

• Logged in and out for the first time.

• Set the root password.

• Deleted anonymous accounts.

• Created a username and password for your own programming tasks.

Exercises

Install MySQL on a local system according to the steps in this chapter, if you have not done so already.

Next

In Chapter 2, “Quick Tour,” we will look at the components of your MySQL installation and review user interfaces and basic MySQL usage.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset