Tuesday, September 25, 2012

Adding directories to the Mac OS X PATH variable


See what's in the PATH variable
  Open Terminal -> Type echo $PATH

This will display whatever is set in the path variable. 

Add more directories to the PATH variable
    Open Terminal -> Type export PATH=new_directory:$PATH

This will add the new_directory to the PATH variable. If you want to make this change permanent simply redirect the above to your .profile file (You can find it in your home directory.. If not, you can simply create one). See below for an example of redirection... 

Open Terminal -> Type export PATH=new_directory:$PATH >> ~/.profile

Where can I find "make" for MAC OS X Lion


"make" command or utility to be precise is used to figure out which parts of a large program needs to be recompiled and issues commands to recompile them. It is not limited to just programs but can be used for any other instructions where some files need to be updated automatically.

To be able to use make you need to install the make utility and write a Makefile (describes the relationship between various files of your program and also has instructions/commands for updating them).

Generally "make" utility comes by default on any *nix platforms. But on MAC OS X it does not come by default. You need to install XCode and command line tools in order to get make as well as some other developer tools installed.

Earlier XCode installation itself installed make and other tools but now those need to be installed separately.. Here is what you need to do..

1.  Open XCode.
2.  Goto Preferences -> Downloads.
3.  Select Command Line Tools and click Install.

This will install command line tools which include make and other developer tools like gcc on your machine. 

Monday, September 24, 2012

Slow queries on Amazon RDS

Amazon does not give you direct access to its database server to view any log files (like slow query log files).. But since slow query logs are very much useful in production systems it does give you a view of them in mysql's slow_log table. 

Here are some steps you will have to follow to enable Slow Query Logging on RDS (By default it is not enabled): 

1.  Modify the DB Parameter group your RDS Instance is using. Enable "slow_query_log" parameter.  

2.  Modify long_query_time to few seconds. Basically it tells to log queries which takes more than this many seconds. 

3.  Wait till your RDS Instance tells you that it is syncing. Once the DB Parameter group and RDS Instance are in sync (takes around 1 min), you can see that mysql's, slow_log tables shows you detailed view of various queries and how much time those are taking. This will give you a better picture of you various queries you application is firing and you can improve them. 

Some points to remember: 

1.  Queries are logging for new connections.. For existing connections you would see that the queries are not logged. 

2.  If you want to clear this table, call stored procedure rds_rotate_slow_log. e.g., CALL rds_rotate_slow_log();

3.   When the above stored procedure is called, the existing data in slow_log is dumped to slow_log_backup. You can now dimply dump that table for futher analysis. 

4.  When you run rds_rotate_slow_log() again the data from slow_log_backup is cleared and fresh data from slow_log is dumped into it. 

5.  If you are working to improve performance of your application, you would also be interested in turning on "log_queries_not_using_index". This will basically dump queries which are not using index. If you index them you will notice performance benefits. 

6.  I could not find millisecond precision on RDS.. which means I cannot log queries which are taking less than 1 seconds which is kind of weird. I was more interested to know queries which are taking less time but their frequency is much more high... taking care of such queries would have been more beneficial to me.. 

Anyways,, I hope my learnings above will help you guys to some extent.... 

ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number


mysql> GRANT * ON *.* TO 'sgoswami'@'121.23.3.13' IDENTIFIED BY PASSWORD "test123";


ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number

MySql expects you to enter password in an encrypted format. test123 which is been passed above is not in encrypted format and hence the error. 

Use mysql console to find out encrypted value for this password and replace it within your query. 

For e.g., 


mysql> select password('test123');
+---------------------+
| password('test123') |
+---------------------+
| 39817a786ddf7333    | 
+---------------------+
1 row in set (0.00 sec)

mysql> 


Replace test123 in above query with the encrypted password shown above. Note when you login you should use test123 only and not the encrypted one. For security reasons, the password is always stored in encrypted format. 

So, your query should look like below now: 

mysql> GRANT * ON *.* TO 'sgoswami'@'121.23.3.13' IDENTIFIED BY PASSWORD "39817a786ddf7333";

Sunday, September 23, 2012

MySql Server for MAC os X Lion - 10.07


Installation

Till the date of writing this article there is no GA release package available for mysql Server for MAC OS X Lion or 10.7. The only package available are of the developmental release. One good thing though is that 10.6 package for mysql works equally well on 10.7. Follow below steps to setup 10.6 on your MAC OS X Lion:

1.  Download the MAC Disk Image for 10.6 MySql Server. Download from official MySql site: http://dev.mysql.com/downloads/mysql/

2.  Mount the disk image and install MySql package by double clicking it. The steps are simple.. so just keep on following on screen instructions from the installer. 

3.  Add /usr/local/mysql/bin to your path by editing .profile file.

4.  You can start MySQL server by running “/usr/local/mysql/bin/mysqld_safe &” from terminal but Disk image you downloaded also consists of Startup Package & Preferences Pane which allows you to start/stop MySQL server from System Preferences and even Automatically Start MySQL Server at Startup.

What does the Installation Package do?
 

The Mac OS X PKG of MySQL installs itself into `/usr/local/mysql-VERSION'

Note: /usr/local/mysql is just a symlink to /usr/local/mysql-version... So whenever you upgrade the symlink will remain same but it will start pointing to the new version that you have installed. This also means that if you want to migrate your data to the new version that you have just installed you will have to copy it manually. 

In addition to this, it also calls the grant tables in the `mysql' database by executing `mysql_install_db'.

About MySQLStartupItem Package

This installation of this package ensures that mysql automatically starts during system startup. So install this package only if you require to start mysql automatically on system startup.

This startup item gets installed into `/Library/StartupItems/MySQLCOM'. Startup Item installation adds a variable `MYSQLCOM=-YES-' to the system configuration file
`/etc/hostconfig'. If you want to disable the automatic startup of MySQL, simply change this variable to `MYSQLCOM=-NO-'.

On Mac OS X Server, the default MySQL installation bundled with the operating system uses the variable `MYSQL' in the `/etc/hostconfig' file. The Sun Microsystems, Inc. "Startup Item" installer disables this variable by setting it to `MYSQL=-NO-'. This avoids boot time conflicts with the `MYSQLCOM' variable used by the Sun Microsystems, Inc. "Startup Item".

Starting Mysql from Command Line

If you have installed the Startup Item, use this command:
     shell> sudo /Library/StartupItems/MySQLCOM/MySQLCOM start

If you don't use the Startup Item, enter the following command sequence:
     shell> cd /usr/local/mysql
     shell> sudo ./bin/mysqld_safe

You should be able to connect to the MySQL server, for example, by running `/usr/local/mysql/bin/mysql'.

Adding Aliases for easy access

     alias mysql=/usr/local/mysql/bin/mysql
     alias mysqladmin=/usr/local/mysql/bin/mysqladmin

Above allows you to access mysql directly without having to remember the full paths. 

Thursday, September 13, 2012

Keyboard Shortcuts on MAC Lion


Some Useful Keyboard Shortcuts on MAC Lion

Show Desktop    :    Five finger gesture Like a Zoom on trackpad.
Refresh Page    :     Command + R
Arrange Apps on Desktop :  Ctrl + Up Arrow Key
Switch Between Desktops :  Ctrl + Left and Right Arrow Keys
Print Screen on MAC: Command+Shift+3
Page Up and Page Down: Fn + Up Arrow And Fn + Down Arrow 



In php concatenate 2 string variables


There is only one string operator in PHP to concatenate string variables. And this operation is DOT (.). 

e.g., 

$fname = "Swapnil" 
$lname = "Goswami"

print "Hello". " " . $fname . " " . $lname

This would result into 

Hello Swapnil Goswami