Archive

Archive for March, 2012

MySQL – Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’

March 30, 2012 Leave a comment

Problem:
I upgraded mysql server from 5.1 to 5.1.6 and when I try to log in I get the following error.

user@computer:~$ mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Solution:
After a quick search on google this is what helped me.
1. Completely remove mysqlserver-5.1.6
2. I renamed the existing /etc/mysql/my.cnf to /etc/mysql/my.cnf.doesnotwork.
3. Install mysqlserver-5.1.6 again.

Source:
http://forums.debian.net/viewtopic.php?f=17&t=59674

Categories: MySQL

bash – Padding numbers in loops

March 28, 2012 Leave a comment

Problem:
I needed a preceeding zero before single digits to represent hours.

shell>for i in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23; do echo $i; done;

Solution:
Use sed or printf

shell> for i in  $(seq 0 23 | sed 's/^\([0-9]\)$/0\1/'); do echo $i; done

OR

shell> for i in  $(seq 0 23 | xargs env printf "%.2d\n"); do echo $i; done

Update:(2012-06-05)
From man seq

-w, –equal-width
equalize width by padding with leading zeroes

Which now makes it

shell> for i in  $(seq -w 0 23); do echo $i; done
Categories: bash Tags: , ,

MySQL Error – Operand should contain 1 column(s)

March 26, 2012 Leave a comment

Problem:
I was getting the following mysql error in python.
OperationalError: (1241, ‘Operand should contain 1 column(s)’)

Solution:
Convert the python list to a string.

Here is what I was not doing.

>>>my_list
['First', 'Second', 'Third']

Instead of

INSERT INTO table (data) VALUES (str(my_list))

I was doing this

INSERT INTO table (data) VALUES (my_list)

Source:
This helped narrow down the issue.
http://lists.mysql.com/mysql/189034

Categories: MySQL Tags:

bash – Trimming spaces from variables.

March 15, 2012 Leave a comment

Problem:
I was reading “settings” from a tab separated text file. The values I was getting had extra white spaces that were not needed. I needed to strip them out. Here is the sample of the text file I was using.

cat sample_file
this | is a sample |   file with |    unwanted spaces   | 

Solution:
After a quick search on google I got a few solutions. Here is what worked for me.

1. sed

cut -d"|" -f4 sample_file | sed 's/^ \+\| \+$//g'

2. awk

cut -d"|" -f4 sample_file | awk '{gsub(/^ +| +$/,"")}1'

Source:
http://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-bash-variable

Categories: awk, bash, sed Tags: ,