Archive for the ‘Linux’ category

Install & use mysql++ on linux(debian squeeze, ubuntu)

July 28th, 2011

The key to use mysql++ on debian/ubuntu system is to find where the mysql++ .h head files and the .so binary files.

Firrst install mysqlclient and mysql++ on debian squeeze:

root# apt-get install libmysqlclient-dev libmysqld-dev libmysql++-dev libmysql++-doc libmysql++3

This will install the mysql head files in /usr/include/mysql, mysql++ head files in /usr/include/mysql, and and in /usr/lib. These are all the files we need.

Now let us use the mysql++ library.

Save the simple code below as test.cpp

#include <iostream>
#include <mysql++.h>

int main() {

mysqlpp::String s(“hello, world”);

std::cout << s << std::endl;

return 0;


And save the makefile as Makefile:

CXX := g++
CXXFLAGS := -I/usr/include/mysql -I/usr/include/mysql++

LDFLAGS := -L/usr/lib -lmysqlpp -lmysqlclient -lnsl -lz -lm

all: test
clean:        rm -f $(EXECUTABLE) *.o

Finally, excute

make all

The compile result would be test.

move Mysql’s datadir on ubuntu

March 8th, 2011

Mysql’s datadir (on ubuntu) by default is /var/lib/mysql. Sometimes, if your DB grows bigger and bigger, and finally exceeds the limit of /var partition, you may think of moving it to some other place. Here are the steps.

  1. stop the server
    sudo service mysql stop


    sudo /etc/init.d/mysql stop
  2. copy data to the new folder
    For example, you want to set the new folder in /home2/: 

    sudo cp /var/lib/mysql /home2/

    Now /home2/mysql is the new datadir. Of course, you can change it to whatever name you want.

    Here I deleted the log file ib_logfile* by:

    sudo rm /home2/mysql/ib_logfile0

    Maybe you also want to change the original datadir to some other name to make sure it won’t be in effect any more.

    sudo mv /var/lib/mysql /var/lib/mysql.bak
  3. change the permission of the new directory
    Make sure the owner of /home2/mysql and all its subdirectories is mysql:mysql. When necessary, use chown to change the permission:sudo chown -R mysql:mysql /home2/mysql 

    Also make sure the permissions of the new datadir and its subdiretories are the same as those under /var/lib/mysql. In my experiment, the permission of table files in each database was changed to 400 when copying, so I had to use chmod to correct them:

    sudo chown -R me:me /home2/mysql;
    sudo chmod 660 /home2/mysql/*/*;
    sudo chown -R mysql:mysql /home2/mysql

    The reason why I changed the owner to me (my username on my computer) at the beginning and change it back to mysql at last is so that I can use the wildcast * to change all files’ permission at once.

  4. modify my.cnf and point datadir to the new directory
  5. sudo vi /etc/mysql/my.cnf

    Find datadir and change it to:


    It seems in Mysql 4 you also need to change the basedir and socket accordingly, but I’m not sure.

  6. add the new diretory into the AppArmor configure file
  7. This part is special on some Linux distributions like Ubuntu >= 7.04. AppArmor is a Linux Security Module implementation of name-based access controls. It confines individual programs to a set of listed files and posix 1003.1e draft capabilities.

    You can use

    sudo aa-status

    to check whether mysql is confined by AppArmor. If so, use

    sudo vi /etc/apparmor.d/usr.sbin.mysqld

    to add the new datadir at the end of the file.

  8. restart apparmor service
    sudo /etc/init.d/apparmor reload
  9. start mysql server
    sudo service mysql start


    sudo /etc/init.d/mysql start

    Refer to /var/log/mysql/error.log to see if there are any errors.

  10. do some tests.
    List all the database, select some records from a table, create a new table and see whether it’s in the new datadir, insert some records, delete them, and whatever.
    WARNING: you may need to run Mysql with the new datadir for a few days before deleting the old datadir.

export mysql data to a custom directory on ubuntu linux

March 8th, 2011

On ubuntu,  mysql is confined with AppArmor to certian directories. Thus when you try to export data into a directory other than /tmp, you’ll get a Errcode 13. So if you really want to export db data inoto a custom dictory, you need to add it to the AppMrmor configure file.

First, check whether mysql is restricted to certain dirs:

sudo aa-status

If you see mysqld listed, then add the custom dirs at the end of the configure file:

sudo vi /etc/apparmor.d/usr.sbin.mysqld

Finally, reload the service:

sudo /etc/init.d/apparmor reload

compress & decompress files under Linux

February 24th, 2011



zip -r dir





rar a xxx.rar dir


unrar e xxx.rar



bzip2 file


bunzip2 xxx.bz2 (NOTE: use the switch -k to keep the .bz2 file)



gzip file (NOTE: -r, --recursive   operate recursively on directories)


gunzip xxx.gz



tar -cjf xxx.tar.bz2 dir


tar -jxvf xxx.tar.bz2



tar -czf xxx.tar.gz dir


tar -zxvf xxx.tar.gz

Enable USB support in Virtualbox

January 14th, 2011

OK, I only tried a Windows XP client on the Ubuntu 10.10 host. here I’m not sure it will work on other hosts or clients. :-)

I was using Virtualbox OSE which I installed through Ubuntu repository. However, only Virtualbox PUEL supports USB.

So this is what I did:

First, uninstall virtualbox-ose:
sudo apt-get remove virtualbox-ose
. also run
sudo apt-get autoremove
to remove unnecessary dependencies.

Now install virtualbox PUEL version. There are 2 ways:

  • Download .deb package and install it directly. Here is the download page:
  • Add to your repository source list, and install it via
    sudo apt-get install
    . The source url for ubuntu 10.10 isdeb maverick contrib, add the public key via
    wget -q -O- | sudo apt-key add -
    , and install it by
    sudo apt-get update;
    sudo apt-get install virtualbox-4.0

    . The lastest info can be found here:

NOTE: uninstalling virtualbox-ose and reinstalling virtualbox PUEL won’t affect the installed guest machines.

Install VirtualBox Extension Pack to suport USB 2.0. The lastest version is still listed in the download page: Here is the directly link for VirtualBox 4.0.0:

Update guest additions. You can download the guest additions ISO file here: Remember to choose the right version, here I chose 4.0.0.

Now, plugin your usb device, and add a usb filter in your guest machine’s settings. Remember, you have to all these before you turn on your guest machine machine. Maybe you think now everything is done, you boot your guest machine and it will tell you a new device is found. No way!

Refer to for the coming steps.

Create a group “usbfs”
sudo addgroup usbfs
, and add yourself to this group:
sudo usermod -aG usbfs <your accout>

Add this line
none /proc/bus/usb usbfs devgid=1001,devmode=664 0 0
(remember to change the “1001″ to your own group id for “usbfs”!) to the end of the file “/etc/fstab”.

Reboot the host machine (here is ubuntu).

In the reference, it says: *USB DEVICES HAVE TO BE UNMOUNTED BEFORE VIRTUAL MACHINE CAN RECOGNIZE THEM*. However, I didn’t do this and it also worked. So I think with version 4.0.0, it’s not necessary.

Now start the guest machine (here is Winsows XP), it should report new USB devices found.

kill a process by name on Linux

November 23rd, 2010

Way 1: find the pid of the process and then kill it by pid.

Step 1: How to find pid:
$> ps aux | grep amsn

weiping 7208 0.4 2.2 260684 86808 ? Sl 09:22 0:15 wish8.5 /usr/bin/amsn weiping 7804 0.0 0.0 8952 876 pts/10 S+ 10:15 0:00 grep amsn

$> pidof wish8.5


Step 2: Kill the process
$> kill 7208

Way 2: use killall

killall -9 wish8.5

Way 3: use pkill

pkill wish8.5

Way 4: use ps + kill

export sspid=`ps aux | grep wish8.5| awk 'NR==1{print $2}' | cut -d' ' -f1`;kill $sspid

bash commands

November 12th, 2010

1. for loop

for (( EXP1; EXP2; EXP3 ))


for (( i=0; i<5; i++ ))
	echo "Hello $i..."

2. if / else / fi statements

basic grammar (note the spaces between the [ ]):

if [ EXP ]; then
	statements 2...


alpha=$(echo "scale=1; $step/10" | bc)
if [ $(echo "$alpha<1" | bc) -eq 1 ]; then

2.1 Arithmetic Comparisons

-lt <
-gt >
-le <=
-ge >=
-eq ==
-ne !=

2.2 String Comparisons

= equal
!= not equal
< less then
> greater then
-n s1 string s1 is not empty
-z s1 string s1 is empty

3. real number calculation

(echo "scale=2; EXP" | bc)

where scale=2 sets the number of the digits after decimal point.

c=$(echo "scale=2; $a/$b" | bc)
echo $c




d=$(echo "scale=2; ${exp}" | bc)
echo $d




4. functions

basic grammar:

function div() {
    return XXX
NOTE: A bash function can only return a number. There is no way to return directly a string. So we need to use $() to capture the result outputted in the function body.
# ----------------------------------------------
# return $1/$2 with 1 digit after decimal point.
# ----------------------------------------------
function div() {
   res=$(echo "scale=1; $1/$2" | bc)
   if [ $(echo "$res<1" | bc) -eq 1 ]; then
   echo $res
# use the function
alpha=$(div $step 10)

use “checkInstall” instead of “make install”

July 22nd, 2010

as said:

CheckInstall keeps track of all files installed by a “make install” or equivalent, creates a Slackware, RPM, or Debian package with those files, and adds it to the installed packages database, allowing for easy package removal or distribution.


set up postfix on debian

July 13th, 2010

Suppose you have 2 sites: & And you would like to set as a local domain, and as a virtual domain. That means emails of will go to the system user’s personal mail dir while has no actual corresponding system user. Details about local domain & virtual domain, please see paragraph “Virtual domains versus local domains” at

part 1: configure local domain

  • install postfix
    apt-get install postfix
  • config postfix
    add the domain to config files using psotconf:
    postconf -e "myorigin ="

    add hostname:
    postconf -e ""
  • Reload Postfix Server:
    postfix reload

Now you can try to send a email via telnet.
telnet localhost 25
You should see:

Connected to localhost.
Escape character is ‘^]’.
220 ESMTP Postfix (Debian/GNU)

Now send a test mail to yourself:

mail from:<>
rcpt to: <>
Subject: First test mail to yourself
Damn it! It worked! Hahahahaha…

End the input with an enter, then a dot “.”, and an enter again.

And then quit:

Now use mail to check your email.

part 2: configure virtual domain

  • create a user and take his home dir as the virtual server’s base dir.
    #> groupadd -g 5000 vmail
    #> useradd -g vmail -u 5000 vmail -d /home/vmail -m
  • set the virtual_uid_maps and virtual_gid_maps to these IDs:
    #> postconf -e virtual_uid_maps=static:5000
    #> postconf -e virtual_gid_maps=static:5000
  • add as a virtual domain into virtual_mailbox_domains
    #> postconf -e
  • set the base dir
    #> postconf -e virtual_mailbox_base=/var/spool/vmail
  • reload postfix
    #> postfix reload;
  • add virtual email addresses
    • tell which file the email addresses are stored in
      #> postconf -e virtual_mailbox_maps=hash:/etc/postfix/vmailhost
    • create the file and add some addresses in there.
      #> vi /etc/postfix/vmailhost
      with content like this:

      So, the mail files for service will actually be stored in /home/vmail/, and mail files for webmaster will be in /home/vmail/
    • Now, map the file into DB
      #> postmap /etc/postfix/vmailhost
  • Probably you also need to create the dirs for these 2 addresses
    mkdir /home/vmail/
    #> mkdir /home/vmail/
    #> cd /home/vmail/
    #> mkdir new curr tmp
    And do the same for
  • change the owner and permissions if you created this folders manually and with ROOT privilege:
    #> chown -R vmail:vmail /home/vmail/
    #> chmod -R 700 /home/vmail/

OK, now you can send mail via telnet to have a test.

part 3: configure alias for

If you want to forward received mails of, you can use a alias to forward to your another email address, let’s say

  • tell where is the alias configuration file
    #> postconf -e "alias_maps = hash:/etc/postfix/aliases"
    postfix reload;
  • add forward rules in the file:
    #> vi /etc/postfix/aliases
    with the conetent:
    and map it:
    #> postmap /etc/postfix/aliases


  • Every configuration done by postconf, you can actually do it by editing /etc/postfix/ directly.
  • Every time you change with command postmap, do not forget to reload postfix with: #> postfix reload.
  • Every time you change a db file, do not forget to re-postmap it with: #> postmap /etc/postfix/xxxxx.
  • If you want to set both sites into virtual server, only set localhost as your destination and both sites into virtual domain.

Important debug method

First, all check the log which should be /var/log/mail.log, no matter you want to find the cause of the failure or to check that your email is sent/received successfully.

Second, use mutt to try to send/receive emails

Important references


———————– v1.1: 27/08/2010 ————————

How to add SASL auth

atually a complete instruction for install postfix on debian
official postfix manual

———————– v1.2: 27/08/2010 ————————

How to set up SPF

———————– v1.3: 03/09/2010 ————————

How to set up domain keys & dkim

Setting up domain keys with dkfilter:

Note: as the dkfilter author has said, dkfilter is deprecated now and DKIMproxy is recommended.

Follow this link : except the settings in file /etc/postfix/ should be:

pickup    fifo  n       -       n       60      1       pickup
    -o content_filter=dksign:[]:10027

And, you may also need to add these as said in

# modify the default submission service to specify a content filter
# and restrict it to local clients and SASL authenticated clients only
submission  inet  n     -       n       -       -       smtpd
    -o smtpd_etrn_restrictions=reject
    -o smtpd_sasl_auth_enable=yes
    -o content_filter=dksign:[]:10027
    -o receive_override_options=no_address_mappings
    -o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject

# specify the location of the DomainKeys signing filter
dksign    unix  -       -       n       -       10      smtp
    -o smtp_send_xforward_command=yes
    -o smtp_discard_ehlo_keywords=8bitmime

# service for accepting messages FROM the DomainKeys signing filter
# inet  n  -      n       -       10      smtpd
    -o content_filter=
    -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks
    -o smtpd_helo_restrictions=
    -o smtpd_client_restrictions=
    -o smtpd_sender_restrictions=
    -o smtpd_recipient_restrictions=permit_mynetworks,reject
    -o mynetworks=
    -o smtpd_authorized_xforward_hosts=

How to generate key:

perl -MMIME::Base64 -e 'print encode_base64("cihangcihangpassword");'

How to test:

telnet localhost 25
ehlo localhost
auth plain Y2loYW5nAGNpaGFuZwB4TDM4MjVNdjZ1

Setting up Domain Keys and/or DKIM with DKIMproxy:

didn’t try. see here: Mail-DKIM and DKIMproxy.

install sopcast player in Ubuntu 10.04(Lucid Lynx) AMD64

June 12th, 2010

If you are installing sopcast player on a X86 machine, you can read this article:

But if you want to install sopcast on a AMD64 machine, you need to manually install sp-auth as said in here:

In summary (for AMD64):

  • add the sopcast player PPA:
    sudo add-apt-repository ppa:jason-scheunemann/ppa
  • update the source list:
    sudo apt-get update
  • install sp-auth:
    tar xzf sp-auth.tgz
    sudo mv sp-auth /usr/local/share
    sudo ln -s /usr/local/share/sp-auth/sp-sc-auth /usr/local/bin/sp-sc
  • install spcast-player:
    sudo apt-get install sopcast-player