Wednesday, December 9, 2009

How (& Why) To Install i686 Kernel In Fedora Core 6 in 7 Steps

Anaconda has a well documented defect where it installs i586 based kernel in i686 machines. It causes problems with several drivers including nvidia drivers. Also you will gain performance improvement by moving to i686 kernel. Let's upgrade the kernel in just 7 steps.

First you must verify that you do have a i686 cpu and you have i586 kernel.

Note: All the following commands must be run as root.

1. Run the following from terminal to know your cpu architecture:
arch

2. Now run the following to find out the kernel installed:
yum list kernel

You may get something like this at the end:
kernel.i586 2.6.18-1.2798.fc6 installed
Note the i586. It shows that you have the wrong kernel installed. Let's go to the next step.
Note: If it shows i686 then you needn't proceed further.

3. First save a copy of your grub.conf. You may need it.
cd
cp /boot/grub/ .

4. Next remove the existing kernels.
yum remove kernel
Confirm the removal.
This will remove the kernel and all iuts dependencies. Note the dependencies.You will have to install them later.

You may get a grub error message like:
grubby fatal error: unable to find a suitable template
grubby: doing this would leave no kernel entries. Not writing out new config.

Ignore it.

5. Now install the i686 kernel:
yum install kernel.i686
This will force installation of i686 kernel. Confirm the installation when asked.

You may get grub error like this:
grubby fatal error: unable to find a suitable template

This one is dangerous. If you get it then first fine the exact version of kernel installed as follows:
yum list kernel
You will get a version string like: 2.6.20-1.2933.fc6
Write it down.
Then open grub.conf in any text editor like gedit. I did:
gedit /boot/grub/grub.conf &
Note the line starting with kernel /vmlinuz. Change the version string of vmlinuz to the string you copied before. Double check to ensure there are no typos. Then repeat the process for the line starting with initrd /initrd. Save the file and exit.

6. Now install the dependencies you removed earlier. I did the following:
yum install compiz gnome-volume-manager systemtap gnome-session pcmciautils

7. Reboot and you are done:
reboot

Note: You may get an error while rebooting if you previously ignored the "unable to find suitable template" error or incorrectly modified grub.conf. You will get a second chance to modify it now to make it boot. However if you do have to modify then ensure that you make the same changes in grub.conf so you don't have to type it again and again while rebooting.

Monitoring MySQL Queries Using Bash Script

Monitoring MySQL queries is a favorite pastime of MySQL administrators especially for performance reasons. Here is a simple bash script to monitor long running MySQL queries in realtime using the ubiquitous 'show processlist'. The best part about this script is that you can use it to log your queries over time for later evaluation.


#!/bin/bash
while [ 1 ]
do
mysql -N -u root -ppassword -e 'show processlist' |grep -v 'show processlist'
sleep 2
done



Note: Replace password with your actual password.
Note: -N removes column headers.

This script excludes the show processlist thread itself. You may also exclude the Sleeping threads with the following modification:
mysql -N -u root -ppassword -e 'show processlist' |egrep -v 'Sleep|show processlist'

Note: This is not the only or best solution in market. There is mtop script with more functionality but written in perl. I am alergic to perl and also couldn't get it working in my only attempt. So here is a simple solution in bash. Personally I don't like hacking perl scripts.

Note: You can use slow query log too but in my experience a running top like display is better at finding bottlenecks. Slow query log doesn't indicate why a query took long time. It could be because it was waiting on another query to complete.





How to Change MySQL Password for Other Users

Do you get this error message when you try to change the root password?

mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'

Or may be you need to change the MySQL password for other users. So what do you do then?

To change a normal user password you need to type:
$ mysqladmin -u user-name -p oldpassword newpass

How to Update or Change Password for a Single User in MySQL and Reload Privileges

MySQL stores usernames and passwords in the user table inside the MySQL database. You can directly update a password using the following method to update or change passwords:

1) Login to the MySQL server, type the following command at the shell prompt:

$ mysql -u root -p

2) Use the mysql database (type commands at the mysql> prompt):

mysql> use mysql;

3) Change password for a user:

mysql> update user set password=PASSWORD("newpass") where User='YOUR-USER-NAME';

4) Reload privileges:

mysql> flush privileges;
mysql> quit


*the above script works only with PHP or pearl scripting*

4 How To Change MySQL Password

Let's look at all the ways to change MySQL password, for root and other users:

In MySQL the default password is empty. This is inherently unsafe and should be immediately changed. Here is how you can change MySQL default root password:

mysqladmin -u root password NEWPASSWORD

Here is how you can change OLDPASSWORD to NEWPASSWORD

mysqladmin -u LOGIN -p OLDPASSWORD NEWPASSWORD

Replace LOGIN with your login name, OLDPASSWORD with your current password and NEWPASSWORD with the new password.


enjoy!

How to Set Up Root Password for Your MySQL Server

If you have never set a root password for MySQL, the server does not require a password at all for connecting as root. To set up a root password for the first time, use the mysqladmin command at the shell prompt as follows:

$ mysqladmin -u root password newpass

If you want to change (or update) a root password, then you need to use the following command:

$ mysqladmin -u root -p oldpassword newpass

I hope this will work for you perfectly.

How to Recover MySQL Root Password

Do you want to recover the MySQL root password. its by no means, easy. But its quite simple if you follow the procedure. You will have to follow this step-by-step processes

Step 1: Stop the MySQL server process.

Step 2: Start the MySQL (mysqld) server/daemon process with the –skip-grant-tables option so that it will not prompt for a password.

Step 3: Connect to the MySQL server as the root user

Step 4: Set a new root password

Step 5: Exit and restart the MySQL server.


Here are the commands you need to type for each step (log in as the root user):


Step 1 : Stop the MySQL service:

# /etc/init.d/mysql stop


Output:

Stopping MySQL database server: mysqld.


Step 2: Start the MySQL server w/o password:

# mysqld_safe --skip-grant-tables &

Output:

[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started


Step 3: Connect to the MySQL server using the MySQL client:

# mysql -u root

Output:

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>


Step 4: Set a new MySQL root user password:

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit


Step 5: Stop the MySQL server:

# /etc/init.d/mysql stop

Output:

Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6186]: ended

[1]+ Done mysqld_safe –skip-grant-tables


Now Start the MySQL server and test it:

# /etc/init.d/mysql start
# mysql -u root -p


phew done now enjoy programming


Sunday, December 6, 2009

The Real Ajay Bhatt of Intel's 2009 Ad Campaign

Here's the picture of Real Ajay Bhatt


You must have seen this Intel's ad on your television many times, here's what you don't know about this ad.
The tagline of the advertisement is ''our rock stars are different from your rock stars.'' The ad is great but the only person missing in the ad is the Real Ajay Bhatt. Yes, the ad is played by some actor instead of Real Ajay Bhatt.

Ajay V. Bhatt is an Indian-American computer architect. He has been instrumental in driving definition and development of broadly adopted technologies such as USB (Universal Serial Bus), AGP (Accelerated Graphics Port), PCI Express, Platform Power management architecture and various chipset enhancements. Ajay Bhatt rose to global celebrity as the co-inventor of USB through an Intel 2009 TV advertisement, where he was portrayed by actor Sunil Narkar.
The ad was created by the agency Venables Bell & Partners.
After completing his graduation from the Maharaja Sayajirao University of Baroda, India, Bhatt received his master's degree from The City University of New York. Ajay Bhatt is an Intel Fellow and Chief Client Platform Architect for the Intel Architecture Group at Intel Corporation. Bhatt leads definition and development of the next-generation Client Platform architecture. He is primarily focused on the novel advances in platform hardware and software by working with key internal and external technology partners to develop the future Client Platform Architectures and Technologies. As a lead Client Platform Architect, Ajay also works with key business and planning groups to be at the forefront of the future Client Platform innovation areas by setting Intel-wide and industry-impacting strategies.
In addition, Bhatt continues to hold a position of Intel's Chief I/O architect. In his role as a Chief I/O architect, he is responsible for the platform and I/O interconnects directions for Intel; leading the definition of next-generation platform architectures and I/O technologies across the market segments internally and within the industry.
Bhatt is an industry-recognized expert in the area of I/O technologies. At Intel, Bhatt has been instrumental in driving definition and development of broadly adopted technologies such as USB, Accelerated Graphics Port, PCI Express, Platform Power management architecture and various chipset enhancements. Bhatt joined Intel in 1990 as a senior staff architect on the chipset architecture team in Folsom.
Bhatt received his master's degree from The City University of New York. He holds nine U.S. patents with several in various stages of filing. In 1998, 2003 and 2004 Bhatt was nominated to take part in a Distinguished Lecture Series at leading universities in the United States and Asia. He received an Achievement in Excellence Award for his contribution in PCI Express specification development in 2002.

Friday, December 4, 2009

Log4j MDC (Mapped Diagnostic Context) : What and Why

I hope that Log4j does not need any introduction and I assume that you already have basic understanding of Log4j. If not, I’ll recommend you to first read read my previous post to get an understanding.


That said, now I’ll start with MDC or Mapped Diagnostic Context. Don’t get scared with this name! MDC is not that tough. It’s a simple yet useful concept. Before I explain what is MDC, lets assume that we are going to develop a simple web application with one servlet ‘MyServlet’ servicing requests from multiple clients. And, this servlet uses log4j framework for logging. A file appender has been defined for this servlet, so all the log messages will be logged into a text file.

With the above said configuration, all the log messages from MyServlet will go into a single log file. And when this servlet is serving more than one clients at the same time, the log statements will be mixed and there’s no way to differentiate which log statement is belongs to which client’s processing. This’ll make it difficult to trace and debug if any processing error occured in MyServlet life cycle.

How to differentiate log statements with respective to each clients?:-
To avoid the log statements mix-in, we could add a user name (or some other data which will be unique to each client) to our log statements. To do this, we have to make sure that we pass this user name data explicitley to each and every log statements, which is a tedious and repetitive work. But, no need to worry! Log4j has an excellent way to overcome this. It’s called as MDC or Mapped Diagnostic Context.

So, What is Log4j MDC (Mapped Diagnostic Context):-
To put is simple, the MDC is a map which stores the context data of the particular thread where the context is running. To explain it, come back to our simple application – every client request will be served by different thread of the MyServlet. So, if you use log4j for logging, then each thread can have it’s own MDC which is global to the entire thread. Any code which is part of that thread can easily access the values that are present in thread’s MDC.


So, how do we make MDC to differentiate logging statements from multiple clients? Simple : Before starting any business process in your code, get the user name (for our Servlet, we can get it from request object) and put that into MDC. Now the user name will be available to the further processing. In your log4j.properties while defining the ‘conversionPattern’, add a pattern %X{key} to retrievce the values that are present in the MDC. The key will be ‘userName’ in our example. It’s like getting a value from a Session object.

In my next post, I’ll give the source code for the same example that I used in this post. So, keep Watching!

Thursday, December 3, 2009

Log4j Tutorial : Adding Log4j logging to your project

If you are new to Log4j, here is a short description on what is it and why should we care about it:
Log4j is a flexible logging library, an open source project from Apache. Using Log4j, we can replace the debugging print line statements, like System.out.println(”Value is ” + someVariable), with a configurable logging statement like logger.debug(”Value is ” + someVariable). The advantage in using Log4j is, if you do not want to print the debugging print lines in production application, you can easily switch them off using Log4j configuration file (which we will see it how, shortly).
How to add Log4j support to your project?
Follow these steps if you want to add Log4j logging support to your Java project.
Download the latest Log4j distribution from Apache website (at the time of this article is written, the latest version of Log4j is 1.2) and put the log4j-xxx.jar in your project class path. For Java web application, you can place the jar file in WEB-INF/lib folder. For Java applications, you can place the jar in any folder, but remember to add the folder to your classpath.
Next we need to configure the Log4j library to our requirements. Log4j reads its configurations from log4j.properties file (or log4j.xml) placed in the CLASSPATH. Every log4j.properties file defines the following three things, mainly:
an Appender – this is the class file which does the actual logging. It could be a simple Console appender which dumps the log messages to stdout (screen) or a file appender, which sends the log messages to a log file.
a Layout – is nothing but how the log message is formatted. This format is very simlar to C languages’s printf function formatting.
and a Logger – a logger ties the appender with the log messages that are coming from the Java application. Basically, a logger tells that the log messages from these packages should go to some appender which will log the message in the defined layout.
Below is a sample log4j.properties for configuring the console appender for your project.


#define the console appender
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender

# now define the layout for the appender
log4j.appender.consoleAppender.layout =
org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# now map our console appender as a root logger, means all log messages will go to this appender
log4j.rootLogger = DEBUG, consoleAppender

In the above properties file, we are first defining a Console appender and a layout pattern (refer this complete documentation to know what’s the weired symbols in the conversion patterns means). Then we are mapping all the DEBUG level log messages from the entire application to go into the consoleAppender, since we added the consoleAppender to the rootLogger.

In any of your Java file, add the below lines, in order to start logging:

private static Logger logger = Logger.getLogger(Myclassname.class);
logger.debug("this is a sample log message.");

Now you are done. Run your application and you should be seeing the log messages coming in your console window.