Sunday, September 26, 2010

Sick on MySQL Lockups

Pardon my French but MySql is a cunt.

Tuesday, September 21, 2010

Php class for simulating Auto Increment in MongoDB




/**
* Static class for generating and
* maintaining records of per-collection
* auto-increment ids
*
* @author Dmitri Snytkine
*
*/
class MongoIncrementor
{
/**
* Name of collection where to
* store the auto-increment values
* You can change it but only before you
* store your first value.
* Once you begin storing values of you
* auto-increments, it's best not to change this, ever!
*
* @var string name of collection
*/
const COLLECTION_NAME = 'Autoincrement';

/**
* The pseudo auto increment handling is done
* by storing collectionName => id
* in Autoincrements collection
*
* We get value, increment it and resave it
* but watch for Errors/Exceptions in order
* to prevent race condition
*
* @param obj $db must pass object of type MongoDB
*
* @param string $collName which collection this id
* is for. This has nothing to do with the name of the collection
* where these generated sequence numbers are stored.
* For example if you need the next id for collection 'STUDENTS',
* then you pass the 'STUDENTS' as $collName value
* This way different values of 'next id' are maintained
* per collection name
*
* @param int initialId if there is no record
* for the collection yet, then start the increment counter
* with this value.
*
* @param int $try this is used for recursive calling this method
* You should NEVER pass this value yourself
*
* @return int value of next id for the collection
*/
public static function nextValue(MongoDB $db, $collName, $minId = 0, $try = 1)
{

if( $try > 100 ){
throw new RuntimeException('Unable to get nextID for collection '.$collName.' after 100 tries');
}

$prevRecordID = null;
$coll = $db->selectCollection(self::COLLECTION_NAME);
$coll->ensureIndex(array('coll' => 1, 'id' => 1), array('unique' => true));

/**
* We use find() instead of findOne() for a reason!
* It's just more reliable this way
*/
$cursor = $coll->find(array('coll' => $collName))->sort(array('id' => -1))->limit(1);
if($cursor && $cursor->hasNext()){
$a = $cursor->getNext();
$prevRecordID = $a['_id'];
} else {
$a = array('coll' => $collName, 'id' => $minId);
}

$prevID = $a['id'];
$newId = ($a['id'] + 1);

/**
* Remove the _id from record, otherwise
* we will be unable to insert
* a new record if it already has the same _id
* This way a new _id will be auto-generated for us
*/
unset($a['_id']);
$a['id'] = $newId;


/**
* Wrapping this inside try/catch so that if
* another process inserts the same value of coll/id
* between the time we selected and updated this
* it will throw exception or return false and then
* we will try again up to 100 times
*
* In Case of duplicate key Mongo throws Exception,
* but just in case it will change in the future,
* we also test if $ret is false
*/
try{
/**
* Using fsync=>true because its very critically important
* to actually write the row to disc, otherwise if database
* goes down we will lose the correct value
* of our increment ID
*/
$ret = $coll->insert($a, array('fsync' => true));
if(!$ret){
$try++;

return self::nextValue($db, $collName, $initialId, $try);
}

/**
* Insert successfull
* now delete previous record(s)
*/
if(null !== $prevRecordID){
$removed = $coll->remove(array('_id' => $prevRecordID)); //, array('fsync' => true) // not very important to fsync
}

} catch (MongoException $e){

$try++;

return self::nextValue($db, $collName, $initialId, $try);
}

return $newId;
}
}

Monday, September 20, 2010

In Java always use braces

Warning: Python programmers must NOT read this post.

This is funny: from Standford Java course professor Mehram Sahami
In Java programming follow the rule of orthodontist:
"You need braces"
Orthodontist will always say this to EVERY patient!

What he means is: don't write a shorthand "no braces" code blocks,
like these:
if(num % 2 == 0)
println ('num is even');

always use braces:
if(num % 2 == 0){
println ('num is even');
}

It's just easier for another programmer to read.

Saturday, September 18, 2010

random.h library in C++ but not in php

Useful function from C++ random.h library:
RandomChance(doublce probability)
You pass it a probability chance like 0.25
and the it will return true 1/4 of the times, other times
if will return false.

There is no such function in php, so that's one more area where php could improve,
perhaps add the "Random" class into SPL and include all the functions from the C++ random.h

Strings in Java, php and C++

Strings in C++ are mutable
Strings in php are also mutable
Strings in Java are immutable, but there is a string builder class
to make string building in mutable fashion.

Sunday, September 12, 2010

sorry, you must have a tty to run sudo

If you are unable to run sudo command,
check the /var/log/secure log

If you are seeing this message:

"sorry, you must have a tty to run sudo"

Then you should edit /etc/sudoers file and comment out this line:

Defaults requiretty
(just add the # in front of this line and save the file)

This this will fix your sudo error.

Saturday, September 11, 2010

Unable to login to mysql as root user access denied

Just got a new server from theplanet webhost

Could not login to mysql as root user

Kept getting messages access denied for user 'root'@'localhost'
using password yes

I tried with and without the password for root user

I then tried instructions for resetting the mysql root user password from
mysql website


Nothing worked!

Finally what I did was:
Stopped mysql
/etc/rc.d/init.d/mysqld stop

Deleted everything from /var/lib/mysql
so the default database 'mysql' was removed

Then reinstalled mysql and mysql-server from yum

yum reinstall mysql
yum reinstall mysql-server

Then started the mysql server again
/etc/rc.d/init.d/mysqld start

This time the startup automatically created
brand new 'mysql' database.

This time I was able to login to mysql with as root user and without a password:
/usr/bin/mysql -u root

Once Logged in I changed password:
mysql> UPDATE mysql.user SET Password = PASSWORD('newpwd')
-> WHERE User = 'root';
mysql> FLUSH PRIVILEGES;

Done!

How to build and install Postfix on 64 Bit CentOS or RedHat

How to build latest postfix from source with MySQL and PCRE support on
64 bit Linux (CentOS or RHEL)

First make sure you have Mysql and pcre libraries installed, in not, then install them

#yum install mysql-devel pcre-devel

Then download the latest source for Postfix, cd into the Postix directory,
it should have several fines, and MUST have Makefile.init

From this directory type:

make -f Makefile.init makefiles \
'CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DHAS_PCRE -I/usr/include' \
'AUXLIBS=-L/usr/lib64/mysql -lmysqlclient -lz -lm -L/usr/lib64 -lpcre'

Then type make
then "make install"

if will ask several questions, just accept defaults by hitting "Enter"
This is it

Now stop and restart your sendmail
/etc/rc.d/init.d/sendmail stop
/etc/rc.d/init.d/sendmail start

Now check to make sure that mysql and pcre are on the list
of supported posfix maps:

type: /usr/sbin/postconf -m

You should see mysql and pcre in the list of maps

Of cause make sure to properly edit the configuration files
in the /etc/postfix directory.

That's a whole new big article (more like a small book), so I am not going to explain about postix
configuration here.

Friday, September 10, 2010

php 5.3.3 skipping incompatible /usr/lib/mysql/libmysqlclient.so

If you having problem building php 5.3 on 64 bit Linux and getting some errors about
skipping incompatible /usr/lib/mysql/libmysqlclient.so when searching for -lmysqlclient

You just need to specify the correct path to where the 64bit libs are for mysql

Do this before running ./configure:

export LDFLAGS=-L/usr/lib64/mysql