Monday, August 2, 2010

Existing PHP / Ruby not working with MySQL installed with 'sudo port install'

By Default OSX 10.x contains PHP & Ruby installed already. I wanted to make work few applications which used MySQL, PHP & Ruby. So to do this, I installed Xcode & MacPorts too. Then installed mysql using 'sudo port install' and installed successfully. But when I executed the applications, I found that neither PHP nor Ruby could know which mysql.sock can be used to make the existing application work. After a deep search found that port install uses different location and stores the mysql socket. I tried to edit the php.ini file to make sure that it gets connected to the existing socket, but still it didn't worked.

When executed the PHP code I got this warning "Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in /Users" but without this solved I can't connect to DB and get the code working.

When I tried with Ruby also it gave me some weird error "ruby `initialize`: wrong number of arguments (4 for 0) (ArgumentError)" and I got frustrated little bit.

So instead of debugging more I thought I can remove the existing PHP & Ruby and install them using the port install command. So I followed the few steps like mentioned below:

sudo mv /System/Library/Frameworks/Ruby.framework/Versions/1.8 /System/Library/Frameworks/Ruby.framework/Versions/1.8.dead

sudo port install ruby
sudo port install rb-rubygems

sudo mv /usr/bin/ruby /usr/bin/old_ruby
sudo mv /usr/bin/gem /usr/bin/old_gem
sudo ln -s /opt/local/bin/ruby /usr/bin/ruby
sudo ln -s /opt/local/bin/gem /usr/bin/gem

sudo gem update --system

sudo gem install xml-simple
sudo gem install mysql

sudo port install php5
sudo port install php5-mysql
sudo port install php5-mbstring

sudo cp /opt/local/etc/php5/php.ini-production /opt/local/etc/php5/php.ini

locate mysqld.sock
==> /opt/local/var/run/mysql5/mysqld.sock (mostly this will be)

sudo vim /opt/local/etc/php5/php.ini

and change all mysql & mysqli default_socket = /opt/local/var/run/mysql5/mysqld.sock

sudo mv /usr/bin/php /usr/bin/old_php

The above commands will rename the existing PHP & Ruby and uses the new Ruby & PHP installed using the port install commands. After updating the php.ini my existing code worked perfectly as though there were no issues. Even the Ruby also didn't gave me any error, even that too worked fantastically.

I hope this will help you to resolve your problem easily rather than debugging the issue which might eat up your time.

No comments:

Post a Comment