mysql

Snow Leopard and PHP

I've recently upgraded to OS X 10.6 Snow Leopard and I was quite happy to know that it came with recent versions of Apache, PHP and Python. The first thing I did was enable PHP in Apache by editing http.conf

sudo mate /etc/apache2/httpd.conf

and then I uncommented the PHP line by changing

#LoadModule php5_module        libexec/apache2/libphp5.so

to
LoadModule php5_module        libexec/apache2/libphp5.so

on line 115.

Next, I checked out my company's source code and browsed to the website at http://localhost/~mwilliamson/website and tried to login. Unfortunately, I came across this lovely error:

Warning: mysql_pconnect(): OK packet 6 bytes shorter than expected in /Users/mwilliamson/Sites/GCS/EZrate/classes/database.class.php on line 59
Warning: mysql_pconnect(): mysqlnd cannot connect to MySQL 4.1+ using old authentication in /Users/mwilliamson/Sites/GCS/EZrate/classes/database.class.php on line 59 mysqlnd cannot connect to MySQL 4.1+ using old authentication

So I Googled around and saw various people with the same issue where none was able to be fixed. I checked to see if the MySQL database was using old passwords-- it wasn't. I changed the password on the database user. No dice. Then I decided to recompile PHP without mysqlnd and woo! It worked! Naturally, I'm making it sound much quicker than it really was. This took several days.

Here's the commands to recompile PHP and restart Apache.

$ cd ~/Downloads
$ curl http://us.php.net/distributions/php-5.3.0.tar.gz -o php-5.3.0.tar.gz
$ tar -xzf php-5.3.0.tar.gz
$ cd php-5.3.0

We can't make and install yet. There's a bug in the PHP sources which messes up iconv. We'll need to change line 186 in ext/iconv/iconv.c:

mate ext/iconv/iconv.c

change:

#ifdef HAVE_LIBICONV
#define iconv libiconv
#endif

to
#ifdef HAVE_LIBICONV
#define iconv iconv
#endif

Save, then run:

$ ./configure '--prefix=/usr' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--disable-dependency-tracking' '--sysconfdir=/private/etc' '--with-apxs2=/usr/sbin/apxs' '--enable-cli' '--with-config-file-path=/etc' '--with-libxml-dir=/usr' '--with-openssl=/usr' '--with-kerberos=/usr' '--with-zlib=/usr' '--enable-bcmath' '--with-bz2=/usr' '--enable-calendar' '--with-curl=/usr' '--enable-exif' '--enable-ftp' '--with-ldap=/usr' '--with-ldap-sasl=/usr' '--enable-mbstring' '--enable-mbregex' '--with-mysql=/usr/local/mysql' '--with-mysql-sock=/var/mysql' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-iodbc=/usr' '--enable-shmop' '--with-snmp=/usr' '--enable-soap' '--enable-sockets' '--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm' '--with-xmlrpc' '--with-iconv-dir=/usr' '--with-xsl=/usr'

Calling `Make` at this point will yield the following error:

Undefined symbols:
  "_res_9_dn_expand", referenced from:
      _php_parserr in dns.o
      _php_parserr in dns.o

...

ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [libs/libphp5.bundle] Error 1

We'll need to change the Makefile first. Add "-lresolv" to the EXTRA_LIBS line on line 102 like so:

mate Makefile

EXTRA_LIBS = -lexslt -liodbc -lmysqlclient -lldap -llber -liconv -liconv -lssl -lcrypto -lcurl -lbz2 -lz -lssl -lcrypto -lm -lxml2 -lz -licucore -lm -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -lcurl -lssl -lcrypto -lssl -lcrypto -lz -lz -lxml2 -lz -licucore -lm -lmysqlclient -lz -lm -liodbc -lxml2 -lz -licucore -lm -lnetsnmp -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxslt -lxml2 -lz -licucore -lm

to

EXTRA_LIBS = -lexslt -liodbc -lmysqlclient -lldap -llber -liconv -liconv -lssl -lcrypto -lcurl -lbz2 -lz -lssl -lcrypto -lm -lxml2 -lz -licucore -lm -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -lcurl -lssl -lcrypto -lssl -lcrypto -lz -lz -lxml2 -lz -licucore -lm -lmysqlclient -lz -lm -liodbc -lxml2 -lz -licucore -lm -lnetsnmp -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxslt -lxml2 -lz -licucore -lm -lresolv

Then we can build and install:

$ make
$ sudo make install
$ sudo apachectl restart

Hope it helps!

Use MySQL with Erlang

If you want to get your manager to try out Erlang, but they are hesitant (probably an understatement) you might be able to get some leverage if you use a relational database rather than mnesia. So, here's a quick sample of how to connect to and query a MySQL database.

First, you'll need to install the MySQL ODBC driver if you haven't already. You can find it at http://dev.mysql.com/downloads/connector/odbc/5.1.html. Then you'll need a database to connect to and a table to query. Here's the SQL script I used to create my test database:

CREATE DATABASE `test`;

CREATE TABLE `test`.`test_table`
(
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `first_name` VARCHAR(45) NOT NULL,
  `last_name` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`)
)
ENGINE = InnoDB;

INSERT INTO `test`.`test_table`
(
  `first_name`,
  `last_name`
)
VALUES
(
  'Matt',
  'Williamson'
),
(
  'Matt',
  'Williamson2'
),
(
  'Matt',
  'Williamson3'
);

And here's the code to connect and query in erlang:

application:start(odbc).
ConnString = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=test; User=root;Password=ace152;Option=3;".
{ok, Conn} = odbc:connect(ConnString, []).
Results = odbc:sql_query(Conn, "SELECT * FROM test_table").

This is what my interactive session looks like:

Erlang (BEAM) emulator version 5.5.5 [async-threads:0]

Eshell V5.5.5  (abort with ^G)
1> application:start(odbc).
ok
2> ConnString = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=test;
User=root;Password=ace152;Option=3;"
.
"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=test; User=root;Passwo
rd=ace152;Option=3;"

3> {ok, Conn} = odbc:connect(ConnString, []).
{ok,<0.39.0>}
4> Results = odbc:sql_query(Conn, "SELECT * FROM test_table").
{selected,["id","first_name","last_name"],
          [{1,"matt","williamson"},
           {2,"matt","williamson2"},
           {3,"matt","williamson3"}]}
5>

It's that simple. Post a comment if you want more samples or description.

Erlang docs: http://www.erlang.org/doc/apps/odbc/index.html

Syndicate content