June 14, 2007

Make MySQL and GFS play nice

So you want to use a GFS shared storage option and have mysql run its databases on that shared storage? Well I had this problem at work and it took a bit to figure out since there wasn't much documentation on it but it works. It is slower then just using it over ext3. I have never tested it on a mysql cluster so I am unsure of the speeds compared to that

So the first thing you want to do is edit your /etc/my.cnf file

in the [mysqld] section you want to add this

skip-innodb

Then in the /etc/init.d/mysql script you want to add --external-locking to the following line so it looks like

/usr/bin/mysqld_safe --defaults-file=/etc/my.cnf --external-locking --pid-file="$mypidfile" --log-error="$errlogfile" >/dev/null 2>&1 &

That is pretty much it.. Now if you want to connect to localhost you need to set a new sock file

so in [mysqld] I do something like this

socket=/var/lib/sock/mysql.sock

and in the [client] section ( create one if one isn't in the cnf file)

socket=/var/lib/sock/mysql.sock

Then

mkdir /var/lib/sock/ chown mysql:mysql /var/lib/sock/

mysql should now run over gfs!

March 19, 2007

Webapp from scratch the right way part 1

So I am writing this series to help others that might stumble upon this entry as a starting point for building a good web application from the start. Let me tell you that there is nothing worse then building up a web application just to figure out you need to make a change that requires a change in 8 other sections.

So a bit of background. I am the lead developer for Photoblog. Before doing Photoblog, I had around 5-6 years of PHP experience. I made many mistakes and have learned a lot from them. So please use this and the others I write to learn from my mistakes.

So my first and foremost advice is to create a wrapper class around the database driver. If you were one of the poor souls that didn't do this and wanted to move from php's mysql to their mysqli class then you had to change a ton of mysql_* commands. So that is where the benefits of a wrapper class comes in. When you need to make some changes, you just do it in the wrapper class.

Basically to make a simple mysql query this is what I do

$DB = new Database;
$DB->connect();
$DB->query("SELECT * FROM table WHERE id=1 LIMIT 1");
$DB->fetch_array();

Do you see the benefits to that? You might at first think that it is overkill to do this but its really not. You can do so much with a wrapper class. If you want to run an explain on each query or find the time to complete each query you just add it in the query() function in the wrapper class.

Doing this will save you time in the end.

I use to write sql queries like this

mysql_query("query here") or die(mysql_error());

So now I just do

$DB->query("query here");

In that query() function I can create my own error page if sql errors out. For Photoblog what I do is if the user is an admin it shows the SQL query and the error. If its a normal user it will email the admins the SQL error and just show a error page that the user can understand and say a email has been sent out.

So as you can see there are major benefits to creating a wrapper class for mysql. This is just the tip of the iceberg of what you can do with a wrapper class.

March 9, 2007

Calculate Distance In Mysql with Latitude and Longitude

So you have a whole table full of members or places with latitude and longitude's associated with them. Just replace the $lat and $lon with the center point you want to find distances from. You can also change the distance<=10 to a number you want to search from. This will limit your results to all results that are under 10 miles from the starting point

SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance FROM members HAVING distance<='10' ORDER BY distance ASC

March 1, 2007

Bash Patch to log to mySQL

So at work we wanted to create a centralized syslog server. I had a great idea of why don't we log all user commands run on all servers into mysql. I knew about the bash-paranoia patch so that gave me the idea of using that as my basis for my mysql patch. This should apply to both 3.0 and 3.1 even with all the all the latest security patches included.

So first lets download the paranoia patch

wget http://e133.enemy.cx/xSH-paranoia/download/bash-paranoia.patch

Using that and compiling bash with ---enable-paranoia will allow you to log all user commands to syslog.

So now lets download my patch now

wget http://zcentric.com/bash-mysql.patch

Now if you want to apply it to bash 3.1 I will give a little howto on how to do that. All in 1 step

You want to create the following table in a mysql database


CREATE TABLE `logs` (
`rowid` mediumint(8) NOT NULL auto_increment
`host` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL default '',
`user` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL default '',
`as_user` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL default '',
`ip` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL default '',
`ut_line` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL default '',
`command` text character set utf8 collate utf8_unicode_ci NOT NULL,
`ts` timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`rowid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


Now that mysql table is made lets install bash and patch it and all that good stuff

wget http://ftp.gnu.org/gnu/bash/bash-3.1.tar.gz
tar zxfv bash-3.1.tar.gz
cd bash-3.1
wget http://e133.enemy.cx/xSH-paranoia/download/bash-paranoia.patch
wget http://zcentric.com/bash-mysql.patch
patch -p0 < bash-paranoia.patch
patch -p1 < bash-mysql.patch
autoconf
./configure --enable-paranoia --prefix=/usr
make

Now the config file to let bash know where the mysql server is. Now if the mysql server is down bash shouldn't crash or anything.

So you have to create a file /etc/bash.conf and use the following lines.

SERVER=192.168.0.10
USER=username
PASS=password
DB=dbName

Now you should be able to like run /path/to/bash-3.1/bash and it should bring you to a new shell that is now the bash version with mysql. You should now be able to type commands it it will log to mysql!

You can then run make install if you wish to install

February 15, 2007

Get topleft position of a element in javascript

I am doing this as a first part of my series on how to make a Fotonotes system like Flickr uses but from scratch. Ok well almost from scratch.

The reason I want to redo this is mainly because Fotonotes imports the note data in an XML form inside the jpeg image. Now this is good for a site like Flickr that only shows one image but I want it to be able to handle 20 images on a page. So my idea was to put everything into a database and then just have the notes be hidden until a user hovers over a image. Then just use javascript to show those images. I am thinking this would be easier on the server since it doesn't have to reload the image each time to show the data. I could be wrong though :)

The first thing you want to do is is download dragresize. This pretty much creates the re-sizable boxes so you can select an area you want to add a note to.

I pretty much took the example that comes with it and I found this thread that had the perfect solution to find a x/y coordinate of a element on a page in javascript. Check out the second post for the function I am using. Right now it does just what I want.

The issue I am running into with this test is that firefox does not move the div to the upper left corner until you actually click on it. IE6 and opera seem to behave how I want. I will have to look into this issue and update.

Example of this in action!

The example has some bounding issues, but I just think that it is due to it being right at the top.

Also its a layout ripped from Photoblog since I need for this to work on that site :)

February 1, 2007

Great Examples of mySQL Queries

So this is just a quick on to show a nice website I found about great example of queries in mySQL v5

http://www.artfulsoftware.com/queries.php

January 25, 2007

Remove Leading 0's in Bash

So you have a var you want to remove leading 0's on.. well do the follwing

mzupan@mzupan-desktop:~$ var=00014
mzupan@mzupan-desktop:~$ let var="10#$var"
mzupan@mzupan-desktop:~$ echo $var
14
mzupan@mzupan-desktop:~$

January 16, 2007

Make the HP dv2120us webcam work in Linux

So here is a little quick little howto on how to get the dv2120us webcam working in Linux.

I am using the 2.6.20-rc5 kernel, which at the time of writing this is the latest release candidate kernel. It was not picked up in Linux but a lspci produced the following

03:09.2 System peripheral: Ricoh Co Ltd Unknown device 0843 (rev 01)

I think that is it.. I am not sure if it sees it as a usb or pci device or not. The module uses usbcore so its beyond me. So to get it working I ran the following commands

svn checkout http://svn.berlios.de/svnroot/repos/linux-uvc/
cd linux-uvc/linux-uvc/trunk/
make
make install
modprobe uvcvideo

You should see something like this in your dmesg once you load it

[ 1022.224000] usbcore: deregistering interface driver uvcvideo
[ 1045.764000] uvcvideo: Found UVC 1.00 device USB 2.0 Camera (0c45:62c0)
[ 1045.764000] usbcore: registered new interface driver uvcvideo
[ 1045.764000] USB Video Class driver (v0.1.0)


Also on a side not it sees it as a v4l2 device and not a v4l device

January 11, 2007

Slideshow for Photoblog

So I have been working on a nice little slideshow application that will be used for Photoblog. Its a little bit of html and editing some java code to make it work. I am looking to make this work a big better so it will be easily implemented into any webpage.

So first here are two examples of how you can use it

Showing most recent post
http://www.zcentric.com/examples/slideshow_front.html

Show a certain post by date
http://www.zcentric.com/examples/slideshow_date.html


You can see the different things you can change in the one javascript line for now.. This will probably change but for now it works

http://www.photoblog.com/slideshow/js/slideshow.js?user=mikezupan&type=large&timer=6&date=2006-12-3

now you can see some things

user : that will be your username
type : this is the size of the images you want to show.. the options can be large/medium/small
timer : this is the time in seconds you want it to cycle each picture. It will default to 3 so you can't put anything less.
date : this is optional but it needs to be in the yyyy-mm-dd format. If you leave it out it will show your most recent post.

December 17, 2006

ARP Poison A Network

So this little video I made will describe on how to use Cain & Abel to ARP poison a wireless network. It will then show you how you can get all the passwords from clients on the network. So watch and learn. This is one of the rare times I will do something like this in windows. This is the first part in the series and I hope to do more advanced stuff.

Learn to ARP poison

Here is a Xvid of the video to for the people that don't have flash8 installed

Learn to ARP poison (xvid version)