mysql/DBI install



1. Download and install mysql (the free version is now called "Community Server").

2. Determine if you already have DBI and DBD::mysql installed. Type "perldoc DBI" and "perldoc DBD::mysql" at a command line. If you don't see documentation, you need to download and install these modules.

In windows, you can use the "ppm" (perl package manager) tool from a command line. Just type "ppm" (this application gets installed when you install ActiveState's Perl package). Once in ppm, you can search for modules, and install modules.

In *nix environments (including Mac OS X), you can use the "cpan" tool. Just type "cpan", or use this if that doesn't work:
 perl -MCPAN -e "shell"
(be sure to run as root!)
Mac OS X users: you might need to have Apple's Developer Tools installed (they come on the install CD/DVD, or see Apple's tools site).

The modules to install are "DBI" and "DBD::mysql" ("install DBD::mysql", for example).

3. Once those are installed, make sure mysql is actually running on your machine (usually called "starting the mysql daemon"). In Windows, you might need to start the mysql service (this should have been installed in your Start menu). In *nix and Mac OS X, you can start it like this:
 sudo /usr/local/mysql/bin/mysqld_safe &

Then you can connect to the database from Perl and/or the command-line interactive tool.

4. Mysql comes out-of-the-box with NO PASSWORDS on the accounts. Set up mysql user accounts and passwords. You'll do this from the mysql interactive shell.
  • To start the mysql interactive shell, type "mysql" from a command-line prompt. You might have to give the full path to the executable (for example "c:/mysql/bin/mysql") or change directory into that directory before running the command. Note you will need to also pass a username and password, if you've already password-protected your database. That command looks something like this:
     mysql -uUSERNAME -pPASSWORD
    

    Then you can run raw SQL statements against your databases.

  • Follow the instructions on this page to remove the anonymous accounts and change the root passwords. Follow the directions in the sections marked "Anonymous Account Removal" and "root Account Password Assignment".

  • Follow the instructions on this page to create a readonly and a read/write user for your database. It will look something like this (with your passwords where it says PASSWORD):
    mysql> grant select, insert, update, delete, create, drop on *.* to 'joelg_w'@'localhost' identified by 'PASSWORD'; 
    mysql> grant select, insert, update, delete, create, drop on *.* to 'joelg_w'@'%' identified by 'PASSWORD'; 
    mysql> grant select on *.* to 'joelg_r'@'localhost' identified by 'PASSWORD'; 
    mysql> grant select on *.* to 'joelg_r'@'%' identified by 'PASSWORD'; 
    

    Now you have a readonly and a read/write user. You should use these when connecting to your database from perl scripts.

5. You're ready to create tables and connect to your database from Perl. See my SQL examples page to get started.
UW Extension Perl Programming
Course Three, Perl, the Web, and Databases
March 26 - June 04, 2007 (note no class May 28)
Monday evenings
10 Sessions, 6:00 to 9:00 PM

Instructor:
Joel Grow (joelg at u.washington.edu)
links:
perl.com
cpan
perldoc online
learn.perl.org
perlmonks.org
seattle perl users group (spug)
perl.com
 
Thursday, May 24, 2012
you're number: 797