Friday, July 17, 2009

mysql-5.1.36 in Ubuntu 9.04: FATAL ERROR: Could not find mysqld

I was building mysql-5.1.36 in Ubuntu 9.04 - the Jaunty Jackalope. I was following the instructions given in INSTALL-SOURCE provided with the package. I was trying to create the MySQL data directory and initialize the grant tables by running this command:

$ sudo bin/mysql_install_db --user=mysql

I got this error:

FATAL ERROR: Could not find mysqld

The following directories were searched:

/usr/libexec
/usr/sbin
/usr/bin

If you compiled from source, you need to run 'make install' to copy the software into the correct location ready for operation.

If you are using a binary release, you must either be at the top of the level of the extracted archivem or pass the --basedir option pointing to that location.

After a bit googling and investigation, I found that this package mysql-common was already installed with
Ubuntu 9.04 and there is a file my.cnf in /etc/mysql and db initialization was using this file instead of the one I copied to /etc with this command while following the INSTALL-SOURCE file:

$ sudo cp support-files/my-medium.cnf /etc/my.cnf

So the quick fix was to replace /etc/mysql/my.cnf with /etc/my.cnf that I copied earlier. I did these:

$ sudo mv /etc/mysql/my.cnf /etc/mysql/my_backup.cnf
$ sudo cp /etc/my.cnf /etc/mysql/

CodeIgniter: AJAX Pagination

To use CodeIgniter's Pagination class to create pagination in one of our controller functions:


$this->load->library('pagination');

$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = '200';
$config['per_page'] = '20';

$this->pagination->initialize($config);

echo $this->pagination->create_links();

The $config array contains your configuration variables. It is passed to the $this->pagination->initialize function. At a minimum we need the three configuration variables shown above.
  • base_url: Full URL to the controller class/function containing our pagination.
  • total_rows: Total rows in the result set we are creating pagination for.
  • per_page: Number of rows we intend to show per page
The pagination links provided by create_links function may not be appropriate for all cases. After-all clicking on these links will do page load which may not be desired on many situation. We may be interested to invoke a javascript function while clicking these links for doing AJAX style pagination. Still we want to use all other features provided by CodeIgniter's Pagination class. This can be accomplished simply. Just follow me ;)

1. All you need to do is adding some more functionality to the existing library i.e. CodeIgniter's Pagination class. To extend the native Pagination class you'll create a file named application/libraries/MY_Pagination.php
, and declare your class with:


class MY_Pagination extends CI_Pagination
{
}

Here MY_ is the sub-class prefix, defined in application/config/config.php as follows:


$config['subclass_prefix'] = 'MY_';


If you need to use a constructor in yo ur class make sure you extend the parent constructor:


class MY_Pagination extends CI_Pagination
{
function MY_Pagination()
{
parent::CI_Pagination();
}
}


2. Next add some member variables to this class:


class MY_Pagination extends CI_Pagination
{
var $js_function_name = '';
var $js_function_params = array();

function MY_Pagination()
{
parent::CI_Pagination();
}
}

js_function_name is the name of the javascript function to invoke when pagination links are clicked.

js_function_params is an array of parameters required for that javascript function. Our customized pagination will; add one more parameter for that javascript function; offset. While viewing the first page, offset=0. In the second page, offset = rows_per_page as defined by $config['per_page'] provided to the $this->pagination->initialize function.

js_href is the name of the div used in the pagination links as the value of the href attribute prefixed with #

3. Add a member function
initialize_js_function to the class:


function initialize_js_function($jsFunction = array())
{
if (count($jsFunction) > 0) {
if (isset($jsFunction['name'])) {
$this->js_function_name = $jsFunction['name'];
}
if (isset($jsFunction['params'])) {
for ($i = 0; $i <>js_function_params[$i] = $jsFunction['params'][$i];
}
}
}
}

This function will be called from the controller function where we are using pagination stuffs.

4. Now modify the function create_links and add this modified function to the MY_Pagination class as create_js_links function. The modification basically ensures that each of the anchor tags generated, will have value of onclick attribute equals the javascript function as defined by the member variable js_function_name having the parameters as defined by the member variable js_function_params plus one extra parameter offset. A comma sperated list of the items in js_function_params will be created. Then the offset parameter will be appended in that comma separated list.



function create_js_links()
{
// If our item count or per-page total is zero there is no need to continue.
if ($this->total_rows == 0 OR $this->per_page == 0)
{
return '';
}

// Calculate the total number of pages
$num_pages = ceil($this->total_rows / $this->per_page);

// Is there only one page? Hm... nothing more to do here then.
if ($num_pages == 1)
{
return '';
}

// Determine the current page number.
$CI =& get_instance();

if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
{
if ($CI->input->get($this->query_string_segment) != 0)
{
$this->cur_page = $CI->input->get($this->query_string_segment);

// Prep the current page - no funny business!
$this->cur_page = (int) $this->cur_page;
}
}
else
{
if ($CI->uri->segment($this->uri_segment) != 0)
{
$this->cur_page = $CI->uri->segment($this->uri_segment);

// Prep the current page - no funny business!
$this->cur_page = (int) $this->cur_page;
}
}

$this->num_links = (int)$this->num_links;

if ($this->num_links <>cur_page))
{
$this->cur_page = 0;
}

// Is the page number beyond the result range?
// If so we show the last page
if ($this->cur_page > $this->total_rows)
{
$this->cur_page = ($num_pages - 1) * $this->per_page;
}

$uri_page_number = $this->cur_page;
$this->cur_page = floor(($this->cur_page/$this->per_page) + 1);

// Calculate the start and end numbers. These determine
// which number to start and end the digit links with
$start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
$end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;

// Is pagination being used over GET or POST? If get, add a per_page query
// string. If post, add a trailing slash to the base URL if needed
if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
{
$this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
}
else
{
$this->base_url = rtrim($this->base_url, '/') .'/';
}

//$js_output = $this->js_function_name . '(';
$js_output = '';
for ($paramIndex = 0; $paramIndex <>js_function_params); $paramIndex++) {
$js_output = $js_output . $this->js_function_params[$paramIndex] . ',';
}
//$js_output = rtrim($js_output, ',');
//$js_output .= ')';

// And here we go...
$output = '';

// Render the "First" link
if ($this->cur_page > ($this->num_links + 1))
{
$js_output = rtrim($js_output, ',');
$output .= $this->first_tag_open.'js_function_name.'('.$js_output.'0)'.'">'.$this->first_link.''.$this->first_tag_close;
}

// Render the "previous" link
if ($this->cur_page != 1)
{
$i = $uri_page_number - $this->per_page;
//if ($i == 0) $i = '';
$output .= $this->prev_tag_open.'js_function_name.'('.$js_output.$i.')'.'">'.$this->prev_link.''.$this->prev_tag_close;
}

// Write the digit links
for ($loop = $start -1; $loop <= $end; $loop++) { $i = ($loop * $this->per_page) - $this->per_page;

if ($i >= 0)
{
if ($this->cur_page == $loop)
{
$output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
}
else
{
$n = ($i == 0) ? '' : $i;
$output .= $this->num_tag_open.'js_function_name.'('.$js_output.$n.')'.'">'.$loop.''.$this->num_tag_close;
}
}
}

// Render the "next" link
if ($this->cur_page < $num_pages) { $output .= $this->next_tag_open.'js_function_name.'('.$js_output.($this->cur_page * $this->per_page).')'.'">'.$this->next_link.''.$this->next_tag_close;
}

// Render the "Last" link
if (($this->cur_page + $this->num_links) < $num_pages) { $i = (($num_pages * $this->per_page) - $this->per_page);
$output .= $this->last_tag_open.'js_function_name.'('.$js_output.$i.')'.'">'.$this->last_link.''.$this->last_tag_close;
}

// Kill double slashes. Note: Sometimes we can end up with a double slash
// in the penultimate link so we'll kill all double slashes.
//$output = preg_replace("#([^:])//+#", "\\1/", $output);

// Add the wrapper HTML if exists
$output = $this->full_tag_open.$output.$this->full_tag_close;

return $output;
}



5. Now in the controller, where you are using pagination stuffs, initialize the pagition in this way:



//In actual scenario, populate it with number rows to paginate
$config['total_rows'] = 100;
$config['per_page'] = 10;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
//change it as per your controller's function's number of parameters
$config['uri_segment'] = 3;
$this->pagination->initialize($config);

$jsFunction['name'] = 'your_javascript_function_name';
//provide your params for the javascript function if there is any
//In my case, it is empty
$jsFunction['params'] = array();
$this->pagination->initialize_js_function($jsFunction);
//pass/use this $page_link in your view as per your need
$page_link = $this->pagination->create_js_links($pageNo);


This is the basic outline or structure. You have to write down your own javascript function to make ajax call and fill in the missing or leftover details ;)

Now taste the AJAXified pagiantion in action :D We have done a great job already ;)

You can download a complete sample or demo from here: http://www.mediafire.com/file/4ygym0trmjh/ajax_pagination_demo.zip

mysql-5.1.36 in Ubuntu 9.04: No curses/termcap library found

I was building mysql-5.1.36 in Ubuntu 9.04 - the Jaunty Jackalope and while trying to run:
$ ./configure --prefix=/usr/local/mysql

I got this error:

checking for termcap functions library... configure: error: No curses/termcap library found. After a bit thinking, I found that the ncurses stuff isn't installed for. To find which name Ubuntu uses for that library I did:

$ apt-cache search ncurses

and I found:

libncurses5-dev - Developer's libraries and docs for ncurses

and I installed it with:

$ sudo apt-get install libncurses5-dev

After this, I tried configuring mysql-5.1.36 again and it was a success:) Altough it was a simple workaround for the problem that I faced,
I can't resist myself of sharing this ;)

Thursday, June 18, 2009

Ways of utilizing PHP by a web server

There are three ways a web server can utilize PHP to generate web pages:
  • CGI wrapper
  • Module in a multiprocess web server
  • Plug-in for a multi-threaded web server
CGI wrapper:

The first method is to use PHP as a CGI wrapper. In this case. an instance of the PHP interpreter is created for every page request. The page is offcourse a PHP page. The instance is destroyed after the request is served.

Module in a multiprocess web server:

A multiprocess server typically has one parent process which coordinates a set of child processes. The child processes actually do the serving up web pages. when a request comes from a client, one of the children, who is not serving any client at that moment, is allocated to serve the request. This means that when the same client makes a second request to the server, it may be served by a different child process than the first time. This method currently includes Apache web server. This is the most popular method.

Plug-in for a multi-threaded web server:

This method uses PHP as a plug-in for a multithreaded web server. Currently PHP 4 has support for ISAPI, WSAPI, and NSAPI (on Windows), which all allow PHP to be used as a plug-in on multithreaded servers like Netscape FastTrack (iPlanet), Microsoft's Internet Information Server (IIS), and O'Reilly's WebSite Pro. The behavior is essentially the same as for the multiprocess model.

Thursday, January 1, 2009

Opening file having extension .z

This type of file is Unix Compressed File. This is an standard file format supported by many programs. This compression format is used to compress or "pack" files on Unix servers to save disk space; incorporates a simple compression algorithm that has been mostly replaced by GNUzip compression (which creates (.GZ files)).

File can be decompressed on a Unix machine by typing uncompress filename, where "filename" is the name of the file you want to decompress.

There is a lot of programs listed below for uncompressing/openning this type of file:

Sunday, July 27, 2008

apache didn't start after installing subversion

I installed apache long before. Recently I installed subversion and thought everything is OK. But actually it wasn't. When I tried to start apache, i got this error:

$ /usr/local/apache2/bin/apachectl start
[Mon Jul 28 11:13:10 2008] [warn] module ferite_module is already loaded, skipping
[Mon Jul 28 11:13:11 2008] [warn] module php5_module is already loaded, skipping
Syntax error on line 1048 of /usr/local/apache2/conf/httpd.conf:
Cannot load /usr/local/apache2/modules/mod_dav_svn.so into server: /usr/local/apache2/modules/mod_dav_svn.so: undefined symbol: dav_xml_get_cdata

This time, it took little time to solve this problem. I reinstalled apache using these steps:

$ ./configure --prefix=/usr/local/apache2 --enable-dav --enable-so
$ make
$ sudo make install

And everything is ok now :)

Monday, July 21, 2008

Using svn and got "Unrecognized URL scheme."

This morning, I built subversion-1.5.0 from source using the ./configure, make and make install. Then I tried to check out a repository and got this error:
$ svn checkout http://svn.assembla.com/svn/blog1/ --username azim.babu
svn: Unrecognized URL scheme for 'http://svn.assembla.com/svn/blog1'

It seemed to be a curious problem to analyze. So I did some googling and found these from http://subversion.tigris.org/faq.html :

Subversion uses a plugin system to allow access to repositories. Currently there are three of these plugins: ra_local allows access to a local repository, ra_dav which allows access to a repository via WebDAV, and ra_svn allows local or remote access via the svnserve server. When you attempt to perform an operation in Subversion, the program tries to dynamically load a plugin based on the URL scheme. A `file://' URL will try to load ra_local, and an `http://' URL will try to load ra_dav.

The error you are seeing means that the dynamic linker/loader can't find the plugins to load. This normally happens when you build Subversion with shared libraries, then attempt to run it without first running 'make install'. Another possible cause is that you ran make install, but the libraries were installed in a location that the dynamic linker/loader doesn't recognize. Under Linux, you can allow the linker/loader to find the libraries by adding the library directory to /etc/ld.so.conf and running ldconfig. If you don't wish to do this, or you don't have root access, you can also specify the library directory in the LD_LIBRARY_PATH environment variable.

So what I got is that I was not able to setup the SVN on my ubuntu box properly. Here even after compiling the source I was not able to enable the support of ra_dav module for http and https protocol. So I did a little googling again and found that I was missing neon. So I download neon from http://www.webdav.org/neon/neon-0.28.1.tar.gz , extract the tarball and install neon using ./configure, make and make install.
Then I recompile subversion as follows:
$ ./configure --with-ssl --with-apr=/usr/local/apache2/bin/apr-config --with-apr-util=/usr/local/apache2/bin/apu-config --with-neon=/usr/local
$ make
$ sudo make install

A little explanation of the above options are:

--with-apr : prefix for installed APR, path to APR build tree, or the full path to apr-config

--with-apr-util : prefix for installed APU, path to APU build tree, or the full path to apu-config

--with-neon : Determine neon library configuration based on 'PREFIX/bin/neon-config'. Default is to search for in a subdirectory of the top source directory.

As I installaed neon without any option during configure, it should be /usr/local for my case :P

Although all these steps may seem to be very much intuitive for many, I think for newbies, this type of blog post may provide some sort of quick help. :)