How to install PHP GeoIP in Ubuntu

This instructions will install GeoIP over Ubuntu. It is tested over Ubuntu 12.04 LTS.

I assume you already have PEAR/PECL available. I am using the CentOS’s stock PHP. First you need to update your channels:

[code lang=”bash”]kahwee:~ kahwee$ sudo pear update-channels
Updating channel “”
Channel “” is up to date
Updating channel “”
Channel “” is up to date
Updating channel “”
Channel “” is up to date[/code]

Continue reading How to install PHP GeoIP in Ubuntu

How to automate GeoIP updates on Linux

This works for Ubuntu, Debian and CentOS distributions. You need a valid license key from MaxMind. This is available as a subscription at a nominal fee.

If you have installed GeoIP successfully, you should have the geoipupdate program. If you don’t, you need the run the following.

Install over Ubuntu or Debian:

[code lang=”bash”]sudo apt-get install geoip-bin geoip-database libgeoip-dev[/code]

Install over CentOS:

[code lang=”bash”]sudo yum install re2c geoip geoip-data geoip-devel[/code]

Both libgeoip-dev and geoip-devel are required for compiling PHP’s GeoIP.

Once you run geoipupdate the first time, it tells you about a missing GeoIP file.

[code lang=”bash”]root@gladlycode:~# geoipupdate
Error opening GeoIP Configuration file /etc/GeoIP.conf[/code]

Now you copy over from the default file:

[code lang=”bash”]mv /etc/GeoIP.conf.default /etc/GeoIP.conf[/code]

If you now run geoipupdate, you get the following:

MD5 Digest of installed database is f5c2f7eed9331dcaa99b7a6b4633b684
Invalid UserID

Continue reading How to automate GeoIP updates on Linux

How to use Symfony with existing database

I recently had to start a Symfony2 project using an existing database. Here is a rough guide on how it is done.

Firstly you have to update your app/config/parameter.ini to your existing database:

[code lang=”bash”][parameters]

My bundle is named GladlyCodeBundle, you will have to rename this. Based on this I do a map of existing database tables into XML format, note that the em dash is 2 dashes due to conversion by WordPress:

[code lang=”bash”]php app/console doctrine:mapping:convert xml ./src/Gladly/CodeBundle/Resources/config/doctrine/metadata/orm –from-database –force[/code]

You need to change the ‘Gladly/CodeBundle’ part to fit your project as the next step will look to import from the default location.

If you get any errors of this sort:

[code lang=”bash”]PHP Fatal error: Call to a member function getColumns() on a non-object in /Users/kahwee/projects/gladlycode/vendor/doctrine/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php on line 133
PHP Stack trace:
PHP 1. {main}() /Users/kahwee/projects/gladlycode/app/console:0
PHP 2. SymfonyComponentConsoleApplication->run() /Users/kahwee/projects/gladlycode/app/console:22

It could be due to one of your tables not having a primary key. Just assign it the appropriate primary to continue with the import process.

Then I import them with annotations:

[code lang=”bash”]php app/console doctrine:mapping:import GladlyCodeBundle annotation[/code]

Finally I regenerate them into entities:

[code lang=”bash”]php app/console doctrine:generate:entities GladlyCodeBundle[/code]

And that’s it. Everything is imported.

Tools of the trade

Every web developer uses his/her set of preferred tools. Here’s mine:

  • Text editor: SublimeText 2, because it loads fast and I swear by its open by file name feature
  • Computer: MacBook Air 13″, a decent screen size with good portability. I like working at cafés and this gadget fits my lifestyle.
  • Browser: Chrome 18 or 19. I lost track of Chrome’s version, comes with Chrome is the excellent debugger I cannot live without.
  • CSS generator: Less, because Less can be compiled by JavaScript on the client side. It’s easy to get started developing.
  • Database engine: MySQL InnoDB.
  • Database browser: Sequel Pro, never found a replacement for this. I’ve been using this for 3 years, by far the best.
  • Revision control: Git or SVN. My vote is to Git but my company is still on SVN.
  • Image editor: Photoshop. Embarrassingly I am one of those that will just launch Photoshop to make PNGs with alpha-transparency. There are probably better tools out there but I can’t get used to them.
  • Programming languages: PHP for web development. JavaScript or Ruby on scripting automation. I still use Expect for a bunch of server-side tasks, I cannot move away from that still.
  • Web server: Apache for development. It’s all because of PHP’s xdebug. My colleagues use .htaccess often too. I use nginx for production usually.
  • Load profiling: Seige, it’s simple and reliable.

Other tools include:

  • LiveReload 2 has been extremely convenient and it has been part of my workflow for months. I never looked back.
  • Bash alias lets me quickly navigate through the trenches of my messy directories. I also use it to pull for Git quickly. compromised by SQL injection

This day just had to come: compromised (the official site for the MySQL database) was compromised via (shocking!) blind SQL injection. A post was sent today to the full disclosure list explaining the issue and dumping part of their internal database structure.

What is worse is that they also posted the password dump online and some people started to crack it already. Some of the findings are pretty bad, like that the password used by the MySQL director of product management is only 4 numbers (6661) and also posted multiple admin passwords for…

MySQL have not said anything about this attack, but we will post more details as we learn more about it.

Source: Sucuri

The irony.

Oracle shuts down open source test servers

Oracle shuts down open source test servers that PostgreSQL uses to test their builds. PostgreSQL is competing with Oracle’s MySQL and Oracle Database.

Oracle shuts down open source test servers

Like most open source platforms, PostgreSQL relies on an army of distributed volunteers. It is volunteers that, for example, operate the PostgreSQL Build farm, a “distributed, automated build and verify system” built by enthusiast Andrew Dunstan.

Oracle has shut down servers Sun Microsystems was contributing to the build farm for open source database software, PostgreSQL, forcing enthusiasts to scramble to find new hosts to test updates to their software on the Solaris operating system.

“It’s a vital piece of the infrastructure for developing PostgreSQL,” Dunstan told iTnews. “Before it existed, if some change we made broke on some platform, it was often weeks or months before we found out about it. Now we know within hours.”

At the start of July, Oracle shut down its three PostgreSQL build farm servers without warning, leaving the PostgreSQL community rushing to find replacements.

Dunstan said he “suspects” Oracle does view PostgreSQL as a competitor. (Source: IT News)

I love to see Oracle post their financial results, they have made quite a bit of cost cutting moves since the acquisition. You can hardly blame them too; they’ve got shareholders to report to. After all, doing too much charity work on open source is partly why Sun Microsystems failed in the first place.

Moved to Rackspace Cloud

A couple of days ago, I encountered a database corruption. One sentence: My access log grew so huge it took up 100% of the disk space; MySQL can no longer write the database files and a few tables got corrupted.

Okay I cheated on the one sentence.

Rackspace Cloud has similar build as Slicehost (my previous host). I moved a few of my blogs over. Currently Slicehost is like an expensive DNS server. That said, Rackspace Cloud is cheaper than Slicehost. Slicehost is a subsidiary of Rackspace. Well anyway, if you do like to use Rackspace and would like to make my day, use my referrer code ‘REF-KW’ without quotations. It gives me some rebate, I think.

T-Mobile customer database stolen?

These kind of things always scare me. I never could trust saving my card number into any online services because of this.

Unconfirmed Reports of Massive T-Mobile Breach

Early reports indicate that hackers have penetrated the T-Mobile U.S. network and stolen proprietary operating data, customer databases and financial records. According to a post on, the hackers have claimed to be auctioning the pilfered data to the highest bidder. T-Mobile competitors, they say, turned them down.

There are no details as to how the hackers achieved the breach, but they did post code to show that they did penetrate the T-Mobile network.

T-Mobile was the target of a masssive 2005 hack, in which Nicholas Jacobsen was charged with unauthorized network access by the U.S. Secret Service. According to published reports, Jacobsen had access to all the information about T-Mobile’s 16 million U.S. subscribers.

T-Mobile subscribers are also the frequent target of e-mail and text messaging phishing scams.

UPDATE: There is no mention of a security breach or incident on T-Mobile’s Web site. We spoke with T-Mobile Saturday night, and they were unaware of the reported incident. They were looking into it, but stil have not provided a formal response.


How to import and export MySQL database into an SQL file

Or Gzip for the matter. Here’s the command to run in your UNIX-based server to import or export via an SQL file, this is useful for performing backup and restoring of a MySQL database. (I wrote a similar import and export guide for PostgreSQL.) The mysqldump utility performs just that:

Exporting using mysqldump:

[code lang=”bash”]mysqldump -u[Username] -p[Password] [Database] > output.sql[/code]

For example, my username is ‘kahwee’, my password being ‘secret’ and database being ‘justrealized_db’, I would run the following to export my database to a SQL file:

[code lang=”bash”]mysqldump -ukahwee -psecret justrealized_db > output.sql[/code]

And to Gzip:

[code lang=”bash”]mysqldump -u[Username] -p[Password] [Database] | gzip > output.sql.gz[/code]

Importing using mysql:

To import back, we can use the mysql utility in a similar fashion, note that the > (greater than) has change to a < (lesser than).

[code lang=”bash”]mysql -u[Username] -p[Password] [Database] < output.sql[/code]

For example, my username is ‘kahwee’, my password being ‘secret’ and database being ‘justrealized_db’, I would run the following to import my database:

[code lang=”bash”]mysql -ukahwee -psecret justrealized_db < output.sql[/code]

And to Ungzip:

[code lang=”bash”]gunzip < output.sql.gz | mysql -u[Username] -p[Password] [Database][/code]


Backing up and restoring MySQL databases in Windows

Unfortunately, you can’t use gzip here. So all those commands above with gzip can’t work. The rest, however, still works. However, mysqldump and mysql may not be set in your system environment variables. These are instructions on how to add them for Windows Vista:

Editing system environment variables in Windows Vista.

Click on ‘Edit the system environment variables’, a dialog box will pop up. Click on ‘Environment Variables…’, you should be greeted with the following dialog box:

Editing the path for environment variables

My path looks like this before I add anything:

[code lang=”bash”]%SystemRoot%system32;%SystemRoot%;%SystemRoot%System32Wbem[/code]

Append your MySQL bin directory at the back of what is already there. I use XAMPP (XAMPP lite to be specific) which has its MySQL bin folder located here ‘;C:xampplitemysqlbin’, so I would be appending this:

[code lang=”bash”];C:xampplitemysqlbin[/code]

That’s all I guess, hope it is helpful for you.

How to optimize MySQL database with a command

When you delete large portions for data from your MySQL table, the database becomes more fragmented. It is always a good idea to re-optimize your MySQL database especially if you have a lot of varying characters (VARCHAR). Here’s the command to optimize just one table:

[code lang=”sql”]OPTIMIZE TABLE foo[/code]


But I am looking for a more command line solution and here it is:

[code lang=”bash”]mysqlcheck -op database_name[/code]

The ‘o’ refers to optimize. And the ‘p’ is the option for password requirement. You’ll be prompted for the password.

If you’re logged in as user ‘john’ in your SSH, the MySQL username will be ‘john’. If that’s not what you want, use option ‘u’ like this:

[code lang=”bash”]mysqlcheck -op -u user_name database_name[/code]

And to optimize all databases:

[code lang=”bash”]mysqlcheck -op -u user_name –all-databases[/code]

You can also use option ‘r’ to repair.

Note that the optimization only works for MyISAM, InnoDB, and ARCHIVE tables.