Post Reply 
 
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Install MySQL and DBD::MySQL on Mac OS X
02-02-2009, 11:38 PM (This post was last modified: 03-16-2010 09:42 AM by Daz.)
Post: #1
Install MySQL and DBD::MySQL on Mac OS X
(16 March 2010) Edit - it seems that installing MySQL and DBD::MySQL got a little more complicated in Snow Leopard... (Mac OS X 10.6) I'll put up some new instructions in the next week or so specific to Snow Leopard.

In chapter 3 we promised to keep a guide to installing DBD::MySQL on Mac OS X up to date on the website as in the past (on older versions of Mac OS X) this has been at best a tricky operation, and at other times a down right pain. Sorry it has taken a while to materialize (the book has been out several months already), but we hope this should do the trick.

All of the following was carried out on Mac OS X 10.5 (Leopard), if you have an older version of OS X the instructions should still work but if you get any problems let me know in your reply what version of OS X you are using. Smile

Right, onto the information that you need. Getting DBD::MySQL set-up and ready for use is a two step process, first we must install MySQL itself as this is needed to compile the DBD drivers against, then second we shall build and install the DBD drivers.

Installing MySQL

There are several ways that you can install MySQL on Mac OS X:
  • Install binary packages from MySQL
  • Build it from source
  • Build and install it using MacPorts
  • Install it using Fink

All have their merits and different people have their preferences for which option they would choose, but for simplicity we're going to go with the binary package install from MySQL. If you would prefer instructions for the other methods, let me know (by replying to the thread) and I'll put something together.

First head on over to the MySQL 5.1 download page (the most recent version at the time of writing), and download the 'Mac OS X (package format)' installer for the type of processor you have in your Mac.

Once you have the image, it's a simple Mac .pkg installer to install MySQL and all of its tools. In the image file you will also notice two other files: 'MySQL.prefpane' and 'MySQLStartupitem.pkg'. Double-click the first file 'MySQL.prefpane' - this will install an entry in your System Preferences to allow you to stop and start your new MySQL server. Once you have installed this preference pane, start your MySQl server.

The other package (MySQLStartupitem.pkg) is for if you would like MySQL to start on system boot - I personally prefer to leave MySQL off and only start it when I need it, but if you want to install this please go ahead.

Now we just need to do a small bit of set-up before moving on with the Perl side of things...

First we need to give the MySQL root user a password. Open up the terminal and issue the following command (ignore the '$' - that is there to signify the bash prompt) and substitute in the password you would like to use:

Code:
$ mysqladmin -u root password NEW_ROOT_PASSWORD

Now log into MySQL with the root user and your new password to check that it has worked.

Code:
$ mysql -u root -p

Finally, I prefer to use a separate account to the root user to admin my databases, if you'd like to do the same, set up your account with something like the following (ignore the 'mysql>' - that's to signify we're at the MySQL prompt):

Code:
mysql> grant all privileges on *.* to 'daz'@'localhost' identified by 'YOUR_PASSWORD';

That's MySQL set-up and running, now let's move onto the Perl!

Install DBD::MySQL

We're going to use Cpan (Cpan.pm to be exact) to do most of the hard work for us, so let's fire up cpan by typing the following in the Terminal:

Code:
$ perl -MCPAN -e 'shell'

If this is the first time you've used the Cpan module on your Mac you will be asked about configuring your Cpan set-up, something like the following will be in front of you:

Code:
Are you ready for manual configuration? [yes]

At this point what you do is up to you - you can try to configure all of your settings manually, (there's quite a lot to configure), but I normally just answer 'no' and hit return. The Cpan module does a pretty good job of auto-configuring itself.

Next we shall fetch the packages we need (ignore the 'cpan>' - it's there to signify we're at the cpan prompt):

Code:
cpan> get DBI
cpan> get DBD::mysql
cpan> exit

All we have done so far is fetch our packages, there is a reason for this - you need admin privileges to install, so let's do this now. First install DBI:

Code:
$ sudo perl -MCPAN -e 'install DBI'

Unfortunately, this is where things are not as straight-forward, DBD::mysql will not install if we try the same trick as it needs to connect to a MySQL database and perform tests to confirm things will work, but the Cpan module does not know about your connection details. So instead we are going to use the files Cpan downloaded and build DBD::mysql ourselves.

First cd into the directory that the Cpan module created when we told it to 'get DBD::mysql'.

Code:
$ cd ~/.cpan/build/DBD-mysql-4.010/

If this doesn't work for you cd into '~/.cpan/build' and have a look for the directory - your version numbers may be different.

Next we shall configure the build - in the next command substitute in your login details for your MySQL server that you have just set-up:

Code:
$ perl Makefile.PL --testuser='daz' --testpassword='YOUR_PASSWORD'

Now follow up with the next three commands:

Code:
$ make
$ make test
$ sudo make install

This completes the build and install of DBD::mysql! Not too bad in the end was it?!? Finally, let's just confirm that things are working copy the code below into a Perl script, change the login details to the details for your server and run it. If things have gone okay you should have a series of user names (mostly 'root') printed in front of you.

Code:
#! /usr/bin/perl

use strict;
use warnings;

use DBI;
use DBD::MySQL;

my $dbh=DBI->connect(
  "DBI:mysql:database=mysql;host=localhost",
  "daz",
  "YOUR_PASSWORD"
) || die "Error connecting to database: $!\n";

my $sth = $dbh->prepare("SELECT * FROM user");

$sth->execute();

while (my $ref = $sth->fetchrow_arrayref) {
  print $ref->[1] . "\n";
}

exit;
Visit this user's website Find all posts by this user
Quote this message in a reply
02-25-2009, 07:38 PM
Post: #2
RE: Install MySQL and DBD::MySQL on Mac OS X
If mysql is a x86 64-bit version, it will not work. I have to re-install mysql with a x86 32-bit version to get it work.

With 64-bit mysql, it fell over during 'make test', and I have the following errors:

# Failed test 'use DBD::mysql;'
# in t/00base.t at line 21.
t/00base....................NOK 2# Tried to use 'DBD::mysql'.
# Error: Can't load '/Users/kwok/.cpan/build/DBD-mysql-4.010/blib/arch/auto/DBD/mysql/mysql.bundle' for module DBD::mysql: dlopen(/Users/kwok/.cpan/build/DBD-mysql-4.010/blib/arch/auto/DBD/mysql/mysql.bundle, 2): Symbol not found: _is_prefix
...


Kwok
Find all posts by this user
Quote this message in a reply
03-22-2009, 09:17 PM
Post: #3
RE: Install MySQL and DBD::MySQL on Mac OS X
(02-25-2009 07:38 PM)Kwok Wrote:  If mysql is a x86 64-bit version, it will not work. I have to re-install mysql with a x86 32-bit version to get it work.

Yep, sorry - forgot to say that you'll need to be using the 32-bit version for this to work! Wink
Visit this user's website Find all posts by this user
Quote this message in a reply
05-04-2009, 02:57 AM
Post: #4
RE: Install MySQL and DBD::MySQL on Mac OS X
A couple of caveats:

1) I got "mysqladmin: command not found." It's in /usr/local/mysql/...

2) OSX user account needs a password -- if not, building DBD::mysql fails
Quote this message in a reply
10-10-2009, 04:58 AM (This post was last modified: 11-14-2009 10:05 AM by Daz.)
Post: #5
RE: Install MySQL and DBD::MySQL on Mac OS X
Thank you for sharing with us the ways that we can install MySQL on Mac OS X such as install binary packages from MySQL, build it from source, build and install it using MacPorts, and install it using Fink.
Quote this message in a reply
10-12-2009, 03:29 AM
Post: #6
RE: Install MySQL and DBD::MySQL on Mac OS X
Installing MySQL and DVD on older version of Mac OS is indeed tricky because of the numerous things to consider. Anyone who’s installing Mac OS especially on 64-bit version and 32-bit should be more analytic and consider which will work best. I already ready a lot of this thread with issue on installing mysql but this thread has more complete resources.



_________________
Carbide Tooling
Quote this message in a reply
03-15-2010, 07:21 PM
Post: #7
RE: Install MySQL and DBD::MySQL on Mac OS X
Greetings,
The word size for MySQL must match the word size for the Perl you are using. You can find the later by running $ perl -V (capitalized).
I also found that I had to run 'make' as root (i.e. $ sudo make) for make to have permission to use a required module. Here's the error as non-root user:
mkdir blib: Permission denied at /Library/Perl/Updates/5.10.0/ExtUtils/Command.pm line 288
make: *** [blib/lib/DBD/.exists] Error 13
But with that modification, if the word size matches, this worked great. Thanks!
Find all posts by this user
Quote this message in a reply
03-16-2010, 07:57 PM
Post: #8
RE: Install MySQL and DBD::MySQL on Mac OS X
(03-15-2010 07:21 PM)kellert Wrote:  Greetings,
The word size for MySQL must match the word size for the Perl you are using. You can find the later by running $ perl -V (capitalized).
I also found that I had to run 'make' as root (i.e. $ sudo make) for make to have permission to use a required module. Here's the error as non-root user:
mkdir blib: Permission denied at /Library/Perl/Updates/5.10.0/ExtUtils/Command.pm line 288
make: *** [blib/lib/DBD/.exists] Error 13
But with that modification, if the word size matches, this worked great. Thanks!

If you use perl come with the box i.e. supply by Apple, I think with desktop OS X 10.5 and any things before that are 32-bit (I don't know server version), Snow Leopard (OS X 10.6) perl is 64-bit.

If you are running 32-bit perl in OS X 10.5, I think it would be a good idea to have this command before install any perl modules with C code:

$ export ARCHFLAGS='-arch i386'

That will compile any C code into 32-bit binary and should work well with the 32-bit perl.

Also in OS X 10.5, if you try to use perl within Apache and mod_perl, you probably need to compile both into 32-bit or you might have problem with the one come with the box as they are 64-bit programs. Another way to get round this is compile your own 64-bit perl, but I haven't tried to roll my own perl yet.
Find all posts by this user
Quote this message in a reply
05-03-2010, 11:55 PM
Post: #9
RE: Install MySQL and DBD::MySQL on Mac OS X
Any news on installing on Snow Leopard? I am currently trying to do this... and starting to pull out my hair! Thanks in advance for any help.
Quote this message in a reply
05-04-2010, 12:54 PM (This post was last modified: 05-04-2010 12:56 PM by Kwok.)
Post: #10
RE: Install MySQL and DBD::MySQL on Mac OS X
(05-03-2010 11:55 PM)dhoen Wrote:  Any news on installing on Snow Leopard? I am currently trying to do this... and starting to pull out my hair! Thanks in advance for any help.

I install mine on a Intel Core 2 Duo Machine running OS 10.6.2. I use mysql-5.1.43-osx10.6-x86_64 binary.

Before installing DBD::MySQL, I set up my environment by:
PATH=$PATH:/usr/local/mysql/bin

You might have to change this if mysql installed in different directory.
You also need to set up your user name and password for mysql

Then:
$ perl -MCPAN -e 'shell'

cpan[1]> get DBD::mysql

cpan[2]> exit

$ cd ~/.cpan/build/DBD-mysql-4.013-cB623q/

$ perl Makefile.PL --testuser='mysql_user_name' --testpassword='mysql_passwd'

$ make

$ make test

It should say all test successful.

Finally:
$ sudo make install

Hope this helpful.

Kwok
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)