Wednesday, January 20, 2010

MySQL Configuration in Zope/Plone

What is Zope

  • Zope Click here to access its official Website (Z Object Publishing Environment) is an open source web application server written Python on which well known CMS Plone runs. Everything in the Zope is Objects the URL is used for connecting from one Object to another. As the question arises for making dynamic pages we need one database that can talk to Zope for fetching the information from from the Zope and rendering into the HTML Page. For Presentation point of view Zope uses DTML (Dynamic Template Markup Language) which consist of DTML Documents (having identity of their own) and DTML Method (having no identity of their own) but our theme is to connect it by MySQL without further going into details of other topic I will now explain how can we connect to MySQL to the Zope.



What is ZODB/Gadfly

  • Zope also has its own Database called ZODB (Z Object Database) which is a full featured Object database and has Gadfly which is a Relational Database which has limited capability. If you are using Object Database than ZODB works fine but in case of Relational database if you are having a high traffic site or mission critical project than this database is not recommended at all.



Your RDBMS already existed

  • The other case may be that your database already existed in some other Database and you don't want to migrate to other database, here I will use MySQL Database to make a connection with Zope. The other database supported are Oracle, MySQL, SQL Server, PostgreSQL...
  • To set up a connection between the Database and the Zope u need -- Database Connection and Z SQL Method which is used for making queries to the database. (Don't get confused I will explain them)



Very 1st Step

  • But 1st and the foremost thing you have to install the MySQL from the source package, doing it so you will get know the path and various other configuration files, if you prefer installing it directly it will only trouble you. So, download the .tar file from MySQL site and follow the Instruction to install, once you have done with installing now its time to move further.



Installing MySQL from Source File....

The basic commands that you must execute to install a MySQL source distribution are:
shell> groupadd mysql
shell> useradd -g mysql mysql
shell> gunzip <>
shell> cd mysql-VERSION
shell> ./configure --prefix=/usr/local/mysql
shell> make
shell> make install
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> cd /usr/local/mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> bin/mysql_install_db --user=mysql

========================================
FATAL ERROR: Could not find mysqld

The following directories were searched:

/usr/libexec
/usr/sbin
/usr/bin
========================================
IF THIS ERROR COMES THEN SIMPLY JUST DELETE THE /etc/mysql FOLDER

From the source directory run:
cp support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
chkconfig mysql on
service mysql start


shell> chown -R root .
shell> chown -R mysql var
shell> bin/mysqld_safe --user=mysql &

RESTART SYSTEM!!!!!!!!!!


Key Concept

  • Remember as in case of JDBC we have to download the driver for MySQL Connection and place in the Apache Tomcat's lib directory and set the path in the .bashrc file, the concept is same here...
  • But 1 more extra step is needed here. The concept is that let the Python talk to MySQL using the driver for it and allow the Zope to talk to the MySQL by using ZMySQLDA adapter.



Step by Step Procedure

Its Time to Start Congiguration....
1. Download the Python MySQL Database Interface (MySQLdb),Download Here

2. Download Zope MySQL Database Adapter (ZMySQLDA),Download Here

3. Extract the MySQLdb .tar file and simply copy the extracted Directory in Zope add-on Products
  • Directory Location which typically may be /usr/local/Plone/Zope-2.2*************/lib/python/Products

4. Simillary Extract the ZMySQLDA .tar file and simply copy the extracted Directory in Zope add-on
  • Products Directory Location which typically may be
/usr/local/Plone/Zope-2.2*************/lib/python/Products

5. Now, see which version of python Zope is using so you have to use the python executable only bundled with the Zope otherwise MySQLdb will not be able to install or it will be installed in the wrong location which results that Zope will not be able to detect the ZMySQLDA Adapter.

6.Typically if you have installed the Zope through Plone pacakge it may typically goes to the the location/usr/local/Plone/Python-2.4/bin/python, once you have detected this then give the commands as written in the instruction in that package, like this....
  • export ZOPEBIN="/usr/local/Plone/Python-2.4/bin/"
  • $ZOPEBIN/python setup.py build
  • (If you have not installed the MySQL by source it will retun error like 'mysql_config' file not found)
  • $ZOPEBIN/python setup.py install

NOTE:
If you are getting the following Error :

error: command 'gcc' failed with exit status 1

You need to install the following package according to your machine configuration:
mysql-devel.i386 : Files for development of MySQL applications
mysql-devel.x86_64 : Files for development of MySQL applications


7. After the installation is successful you can check the MySQLdb in the python interface like this:
  • root@akshat-desktop:/home/akshat# $ZOPEBIN/python
  • root@akshat-desktop:/home/akshat# $ZOPEBIN/python
  • Python 2.4.6 (#1, Jan 7 2010, 10:23:05)
  • [GCC 4.3.3] on linux2
  • Type "help", "copyright", "credits" or "license" for more information.
  • >>> import MySQLdb;
  • Traceback (most recent call last):
  • File "", line 1, in
  • ImportError: No module named MySQLdb
  • >>>
  • Means something wrong happened during the installation..
So, check all the steps and again try unless you got no error...

One another error that can give you headache is like this...
  • Python 2.4.6 (#1, Jan 7 2010, 10:23:05)
  • [GCC 4.3.3] on linux2
  • Type "help", "copyright", "credits" or "license" for more information.
  • >>> import MySQLdb;
  • Traceback (most recent call last):
  • File "", line 1, in ?
  • File "/usr/local/Plone/Python-2.4/lib/python2.4/site-packages/MySQLdb/__init__.py", line 27, in ?
  • import _mysql
  • ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory
So, remember while installing the MySQL export the path like this...
  • export LD_LIBRARY_PATH=/usr/local/mysql/lib:$LD_LIBRARY_PATH
  • (Assuming it is installed in /usr/local)

8. Finally Restart the Zope/Plone.

9. Now go to the http://localhost:8080/manage at the dropdown you will found the Z MySQL Database Connection which you will add and later it will display a textbox where you have to pass string like this... Enter a Database Connection String : ncra@localhost:8080 root secret where
  • ncra = Database Name in MySQL
  • @localhost:8080 = host and Port Number which it is using
  • root = Username
  • secret = Password
Enjoy......
The Queries will be discussed in the next section..

ADDITION OF TABS IN MAIN NAVIGATION OF PLONE 3




First create folders for the Required sections, into which you will add content. Each of the above sections will be visible in your top-level navigation. Within each top-level folder, you can also create subfolders.
To create a folder, go to your homepage, select Add new... and choose the Folder option from the drop-down list, Specify the Title and the optional Description.
Click on Save, and then make sure that your folder has been published, after that on refreshing you can be able to see the Brand New tab which you have added.

OR
IF YOU HAVE ALREADY INSTALLED qPloneDropDownMenu-0.2.1 (VERSION NOT IMPORTANT) THAN THIS METHOD WILL NOT WORK
THEN TRY THIS
Go to the Plone Site Setup and choose the DROP DOWN MENU from the Add-on Product Configuration which you have recently installed and simply add the TAB by adding the lines for new tab by giving the path.
The Script will be like as shown except the code written between ul Tag (that code is for Drop Down Menu) if and only if you have installed the qPloneDropDownMenu-0.2.1 package otherwise older method will work.


FOR GETTING DROP DOWN MENUS

* You have to install qPloneDropDownMenu-0.2.1 and add the following lines between the TABS code like this

<li id="portaltab-Members" class="plain">
<a href="http://localhost:8080/ncra/Members" accesskey="t" title="Container for users' home directories">Users</a>
<ul>
<li><a href="localhost:8080/ncra/Members/akshat">Akshat</a></li>
<li><a href="localhost:8080/ncra/Members/xyz">XYZ</a></li>
</ul>
</li>


FULL SCRIPT WILL LOOK LIKE THIS

<li id="portaltab-index_html" class="plain">
<a href="http://localhost:8080/ncra" accesskey="t" title="">Home</a>
</li>
<li id="portaltab-Members" class="plain">
<a href="http://localhost:8080/ncra/Members" accesskey="t" title="Container for users' home directories">Users</a>
<ul>
<li><a href="localhost:8080/ncra/Members/akshat">Akshat</a></li>
<li><a href="localhost:8080/ncra/Members/xyz">XYZ</a></li>
</ul>
</li>
<li id="portaltab-gmrt-1" class="plain">
<a href="http://localhost:8080/ncra/gmrt-1" accesskey="t" title="GIANT METERWAVE RADIO TELESCOPE - KHODAD">GMRT</a>
<ul>
<li><a href="http://localhost:8080/ncra/gmrt-1/gmrt-array">GMRT Array</a></li>
<li><a href="http://localhost:8080/ncra/gmrt-1/images/folder_contents">Images</a></li>
</ul>
</li>