Recipe 1.3. Installing MySQL


Problem

You want to install a MySQL relational database server to be used by your Rails applications.

Solution

Windows

If you're a Windows user, download and unzip mysql-5.0.18-win32.zip from http://dev.mysql.com/downloads. Depending on which version of MySQL you download, you should see either a setup.exe file or a .msi file. Click on one of these to start the installation wizard. For most cases, you can select the standard configuration, which includes the mysql command-line client and several other administration utilities, such as mysqldump.

By default, the installation wizard sets up MySQL as a service that starts automatically. Another option is to have the installer include MySQL's binary directory in the Windows PATH, allowing you to call the MySQL utilities from the Windows command line. Once the installation is complete, you can start up mysql as the root user at the command prompt as shown in Figure 1-1.

Figure 1-1. Interaction with MySQL from the command prompt


You can stop and start MySQL from the Windows command prompt using the net command:

C:\> net start mysql                

C:\> net stop mysql                

Lastly, install the MySQL gem for maximum performance:

C:\> gem install mysql                

The gem installer will present you with a list of versions and prompt you for the one you wish to install. Be sure to choose the highest version of the gem that ends with (mswin32).

Linux

To install MySQL on a Debian GNU/Linux system, start by making sure your sources.list file contains the appropriate archive locations:

$ cat /etc/apt/sources.list deb http://archive.progeny.com/debian/ etch main deb-src http://archive.progeny.com/debian/ etch main deb http://security.debian.org/ etch/updates main deb-src http://security.debian.org/ etch/updates main

Then run apt-get update to resynchronize the package index files from the repository sources:

$ sudo apt-get update                

To install MySQL 5.0, install the mysql-server-5.0 package. Installing this package installs a number of dependencies, including mysql-client-5.0.

$ sudo apt-get -s install mysql-server-5.0                 

Debian's package manager, dpkg, installs dependencies and deals with configuration and setup of the server. Once the installation is complete, start the MySQL server by running /etc/init.d/mysql as root:

$ /etc/init.d/mysql --help Usage: /etc/init.d/mysql start|stop|restart|reload|force-reload|status $ sudo /etc/init.d/mysql start                

After the server is running, you can connect to it using mysql

as the root user with no password:

$ mysql -u root -p Enter password:  Welcome to the MySQL monitor.  Commands end with ; or \g. Your MySQL connection id is 7 to server version: 5.0.18-Debian_7-log Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> show databases; +--------------------+ | Database           | +--------------------+ | information_schema | | mysql              | | test               | +--------------------+ 3 rows in set (0.00 sec) mysql> 

You should probably modify your startup scripts so that MySQL starts automatically when the system boots. Lastly, you'll want to install the MySQL gem to gain the performance benefits of the native bindings. The following command should do the trick:

$ sudo gem install mysql                

The gem installer will present you with a number of versions and prompt you for the one you wish to install. Select the latest version of the gem that ends with (ruby).

Mac OS X

Mac users should download the appropriate disk image file (.dmg) for their OS version and chip architecture from http://dev.mysql.com/downloads/mysql/5.0.html. Mount the disk image and double-click the package file (.pkg) to begin the installation wizard. You should also install MySQL.prefPane and MySQLStartupItem.pkg , which gives you an easy way to start and stop the MySQL server, and configure it to launch on startup, respectively.

Once the server is installed, you should add the location of the MySQL command-line tools to your PATH environment variable. Here's an example:

                   ~/.profile                

export PATH=/usr/local/mysql/bin:$PATH

The final step is to install the Ruby/MySQL bindings RubyGem. For best results, use the mysql_config option:

$ sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config                

The gem installer will present you with a number of versions and prompt you for the one you wish to install. While version numbers may change, your best strategy is to select the highest numbered version of the gem that ends with (ruby).

Discussion

The recommended way to install MySQL on Linux is to use your distribution's package management system. On a Debian GNU/Linux system, package management is handled by dpkg, which is similar to the RPM system used by Red Hat distributions. The easiest way to administer dpkg is with the apt suite of tools, which includes apt-cache and apt-get.

Once you've got the MySQL server installed, you need to create one or more databases and users. While it's convenient to create a database from a script, to make it easy to recreate there are also a number of GUI tools for setting up and administering MySQL databases. Get the official MySQL GUI tools from http://dev.mysql.com/downloads. Even if you create a database from the command line or a GUI tool, you can always use mysqldump to generate a creation script for your database.

See Also

  • Section 1.4"




Rails Cookbook
Rails Cookbook (Cookbooks (OReilly))
ISBN: 0596527314
EAN: 2147483647
Year: 2007
Pages: 250
Authors: Rob Orsini

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net