Author Archives: Simon

Useful Mysql Commands

when you see  a # it means use the command from the unix shell. When you see mysql> it means from a MySQL prompt after logging into MySQL.

To login (from unix shell) use -h only if needed.

# [mysql dir]/bin/mysql -h hostname -u root -p

Create a database on the sql server.

mysql> create database [databasename];

List all databases on the sql server.

mysql> show databases;

Switch to a database.

mysql> use [db name];

To see all the tables in the db.

mysql> show tables;

To see database’s field formats.

mysql> describe [table name];

To delete a db.

mysql> drop database [database name];

To delete a table.

mysql> drop table [table name];

Show all data in a table.

mysql> SELECT * FROM [table name];

Returns the columns and column information pertaining to the designated table.

mysql> show columns from [table name];

Show certain selected rows with the value “whatever”.

mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";

Show all records containing the name “Bob” AND the phone number ‘3444444’.

mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';

Show all records not containing the name “Bob” AND the phone number ‘3444444’ order by the phone_number field.

mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;

Show all records starting with the letters ‘bob’ AND the phone number ‘3444444’.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';

Show all records starting with the letters ‘bob’ AND the phone number ‘3444444’ limit to records 1 through 5.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;

Use a regular expression to find records. Use “REGEXP BINARY” to force case-sensitivity. This finds any record beginning with a.

mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";

Show unique records.

mysql> SELECT DISTINCT [column name] FROM [table name];

Show selected records sorted in an ascending (asc) or descending (desc).

mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

Return number of rows.

mysql> SELECT COUNT(*) FROM [table name];

Sum column.

mysql> SELECT SUM(*) FROM [table name];

Join tables on common columns.

mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;

Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;

Change a users password from unix shell.

# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'

Change a users password from MySQL prompt. Login as root. Set the password. Update privs.

# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
mysql> flush privileges;

Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.

# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start

Set a root password if there is on root password.

# mysqladmin -u root password newpassword

Update a root password.

# mysqladmin -u root -p oldpassword newpassword

Allow the user “bob” to connect to the server from localhost using the password “passwd”. Login as root. Switch to the MySQL db. Give privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by 'passwd';
mysql> flush privileges;

Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;

or

mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;

To update info already in a table.

mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';

Delete a row(s) from a table.

mysql> DELETE from [table name] where [field name] = 'whatever';

Update database permissions/privilages.

mysql> flush privileges;

Delete a column.

mysql> alter table [table name] drop column [column name];

Add a new column to db.

mysql> alter table [table name] add column [new column name] varchar (20);

Change column name.

mysql> alter table [table name] change [old column name] [new column name] varchar (50);

Make a unique column so you get no dupes.

mysql> alter table [table name] add unique ([column name]);

Make a column bigger.

mysql> alter table [table name] modify [column name] VARCHAR(3);

Delete unique from table.

mysql> alter table [table name] drop index [colmn name];

Load a CSV file into a table.

mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY 'n' (field1,field2,field3);

Dump all databases for backup. Backup file is sql commands to recreate all db’s.

# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql

Dump one database for backup.

# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql

Dump a table from a database.

# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

Restore database (or database table) from backup.

# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql

Create Table Example 1.

mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));

Create Table Example 2.

mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');

xen missing gpg key

If you get the following error message after installing xen tools and running aptitude update

W: GPG error: http://updates.vmd.citrix.com etch Release: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY 841D6D8DFE3F8BB2
W: You may want to run apt-get update to correct these problems

Issue the following command:

wget -q http://updates.vmd.citrix.com/XenServer/5.5.0/GPG-KEY -O- | apt-key add -

This will download the current gpg key from the citrix update server and add the key to apt. You should see an ok message after thats done run

aptitude update The error message should be gone

converting vmware linux virtual machine to citrix xenserver virtual machine

1. Install quemu on the vmware Server or another Linux machine

2. Uninstall vmware modules on the vmware guest you wish to convert

3. Stop the vmware guest

4. Convert the image file qemu-img convert Linux_2.6.x_kernel-flat.vmdk -O raw Debian.img

5. Copy the image file to the xen server with scp or ftp or to an attached storage server if you have one. Your storage uuid will be different: scp Debian.img 10.110.1.1:/var/run/sr-mount/7d327ac3-f85e-e742-1f5b-f52dd4dbadf6

6. Create a guest with at least the same disk size and amount of RAM as the imported vmware virtual disk. Rename the disk under properties so you can locate it later.
You may need to boot the guest with an ISO image to get it to work

7. Open a console or connect to the xen server with ssh and find the disk copy the uuid of the host: xe vdi-list or with: xe vdi-list name-label=your_disk_name

8. Import the image with: xe vdi-import uuid=652cc56f-4251-44d0-8f07-dcf219edf15a filename=Debian.img

9. Fire up your converted xen image. You may have to modify the grub boot loader vmware uses /dev/sda for it’s HD and xen uses /dev/hda.

10. If you machine does not boot press e at the grub prompt an search for root=/dev/sda1 line and change it to root=/dev/hda1 please change this in you grub.conf once the machine has booted and save your changes

Encrypted home and swap partition on Ubuntu 9.10 Karmic

I have always wanted to encrypt my /home partition on my notebook. Due to lack of time and the worries of data lose i never got round to it. But now the time has finally come. Please be careful following this howto if you do anything wrong you may erase all of you data. You have been warned!

aptitude install cryptsetup libpam-mount

We will start of with the swap partition which is easy. First deactivate your swap partition you may need to remove it from /etc/fstab and reboot if it is in use.:

swapoff /dev/sda7

Then fill your swap with random data from /dev/urandom

dd if=/dev/urandom of=/dev/sda7 bs=1M

Configure encrypted swap in /etc/crypttab and /etc/fstab

cat /etc/crypttab

cryptoswap /dev/sda7 /dev/urandom cipher=aes-cbc-essiv:sha256,size=256,hash=sha256,swap

cat /etc/fstab

/dev/mapper/cryptoswap none swap sw 0 0

Okay thats it reboot to test. If you call top from a shell you should see a normal swap partition. Then try and run the follow command you should see something like this:

cryptsetup status cryptoswap
/dev/mapper/cryptoswap is active:
cipher: aes-cbc-essiv:sha256
keysize: 256 bits
device: /dev/sda7
offset: 0 sectors
size: 8401932 sectors
mode: read/write

Ok your swap partition is done lets move on to /home make sure you have an empty partition for this all data on the partition will be deleted. You’ve been warned

Fill your new home partition with random data.

dd if=/dev/urandom of=/dev/sda8

Initialize the partition and set initial key. Please make sure to set a good password and do not forget it otherwise your data is gone.

cryptsetup -c aes-cbc-essiv:sha256 -y -s 256 luksFormat /dev/sda8

Create a device mapping

cryptsetup luksOpen /dev/sda8 cryptohome

Now you can create a file system on cryptohome

mkfs.ext4 -j -m 1 -O dir_index,filetype,sparse_super /dev/mapper/cryptohome

Okay give your new home a test by closing it reopening it and finally the first mount


cryptsetup luksClose cryptohome
cryptsetup luksOpen /dev/sda8 cryptohome
Enter LUKS passphrase:
key slot 0 unlocked.
Command successful.
mkdir -p /mnt/cryptohome
mount /dev/mapper/cryptohome /mnt/cryptohome
touch /mnt/cryptohome/linux
ls /mnt/cryptohome/
lost+found linux

We can also confirm that it works by issuing the command

cryptsetup status cryptohome
/dev/mapper/cryptohome is active:
cipher: aes-cbc-essiv:sha256
keysize: 256 bits
device: /dev/sda8
offset: 2056 sectors
size: 20978771 sectors
mode: read/write

Now would be a good time to move your current home data to this partition. And umount the partition:

umount /mnt/cryptohome
cryptsetup luksClose cryptohome

To mount this partition at boot time do the following. The boot process will stop and you will be prompted for a password:

First edit /etc/cryptotab

cryptohome /dev/sda8 none luks

Then edit /etc/fstab and add

/dev/mapper/cryptohome /home/ ext4 relatime,errors=remount-ro 0 2

Automatically mount when logging in i will write this part shortly so stay tuned.

Upgrade Debian Etch to Debian Lenny

I started upgrading some of our Etch boxes to Lenny and this is how i did it.

1. Make sure you have installed all updates for the current debian version you are running in our case debian etch. To do this run:

aptitude update and aptitude upgrade

2. That should update all etch packages on your system. If you run into any missing gpg key problems after running the update please read this aptitude update gpg key problems Then open up you sources list file with you favourite editor.

vi /etc/apt/sources.list

3. You can either use vi’s great search and relace function or you can add a new list of sources thats up to you. If you use the search and replace function please make sure to check if that archive also offers packages for lenny. The same applies for any custom sources or backports. This is my sources.list file for lenny:

deb http://ftp.debian.org/debian/ lenny main contrib
deb-src http://ftp.debian.org/debian/ lenny main contrib
deb http://security.debian.org/ lenny/updates main contrib
deb-src http://security.debian.org/ lenny/updates main contrib
deb http://volatile.debian.org/debian-volatile lenny/volatile main
deb-src http://volatile.debian.org/debian-volatile lenny/volatile main

4. Upgrade the sources to lenny:

aptitude update

5. Check if you have sufficient hard disk space before you start the upgrade

aptitude -y -s -f --with-recommends dist-upgrade

6. Upgrade aptitude first.

aptitude install dpkg aptitude apt

7. Then do a Minimal system upgrade with

aptitude upgrade

8. Upgrade the rest of the system

aptitude dist-upgrade

After all thats done if you didn’t encounter any problems your done. Before you reboot check you boot loader just to make sure the new kernel is listed on lenny it should be 2.6.26 something then you should be good to reboot.

Source and further documentation Debian handbook

Clean up your Ubuntu or debian install with deborphan

If you want to clean up your Ubuntu or Debian machine and delete unnecessary (orphaned) deb packages you can use the utility deborphan. It finds packages that have no packages depending on them. First install deborphan:

aptitude install deborphan

Then start of with a dry run just to make sure that you are not removing any packages you still need

deborphan --guess-all

To delete unnecessary data packages use the command:

sudo deborphan --guess-data | xargs sudo aptitude -y purge

To delete all unnecessary packages use the command:

sudo deborphan --guess-all | xargs sudo aptitude -y purge

To get rid of downloaded deb packages use:

aptitude autoclean

Ubuntu Server generate locals

I was having a problem with one of our ubuntu servers and it’s locals. I was seeing the following error after typing local:

root@wiki:~# locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=de_DE.UTF-8
LC_CTYPE="de_DE"
LC_NUMERIC="de_DE"
LC_TIME="de_DE"
LC_COLLATE="de_DE"
LC_MONETARY="de_DE"
LC_MESSAGES="de_DE"
LC_PAPER="de_DE"
LC_NAME="de_DE"
LC_ADDRESS="de_DE"
LC_TELEPHONE="de_DE"
LC_MEASUREMENT="de_DE"
LC_IDENTIFICATION="de_DE"
LC_ALL=de_DE

So i deceided to set my locals in my .bashrc for root. Like this:

export LANG=de_DE.UTF-8
export LC_ALL=de_DE.UTF-8

I was still getting error messages because the locals where not generated on the server. To generate German locals with UTF-8 issue the following command:

localedef -v -c -i de_DE -f UTF-8 de_DE.UTF-8

Thats it the command local -a should now display something like this:

root@wiki:~# locale -a
C
de_DE.utf8
en_US.utf8
POSIX

The command local should display:

root@wiki:~# locale
LANG=de_DE.UTF-8
LC_CTYPE="de_DE.UTF-8"
LC_NUMERIC="de_DE.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_COLLATE="de_DE.UTF-8"
LC_MONETARY="de_DE.UTF-8"
LC_MESSAGES="de_DE.UTF-8"
LC_PAPER="de_DE.UTF-8"
LC_NAME="de_DE.UTF-8"
LC_ADDRESS="de_DE.UTF-8"
LC_TELEPHONE="de_DE.UTF-8"
LC_MEASUREMENT="de_DE.UTF-8"
LC_IDENTIFICATION="de_DE.UTF-8"
LC_ALL=de_DE.UTF-8

To do this for US locals use this command:

localedef -v -c -i en_US -f UTF-8 en_US.UTF-8 and also change the export command above

Alice keine Internet Verbindung über Router mehr möglich nach Umstellung auf ADSL2

Letzte Woche wurde ich von Alice auf ADSL2 umgestellt. Erstes Problem Alice hat es nicht auf die Reihe bekommen mir ein ADSL2 Modem zu schicken. Nach einem Anruf beim Kunden Support war das Problem relative schnell erfasst und ich bekam 4 Tage später ein neues ADSL2 Modem. Nachhause gekommen das Neue Modem angeschlossen mit meinem Router (WRT54G mit DD-WRT) verbunden. Es tat sich nichts ich bekam einfach keine IP vom PPPoE Server zugewiesen. Also wieder bei der Hotline angerufen und nachgefragt. Der Mitarbeiter war ziemlich unfreundlich und meinte nur Produkte von Drittanbietern werden von Alice nicht unterstützt (ich dachte kurz ich hab den Microsoft Support am Telefon). Der Mitarbeiter hat mich aufgefordert eine PPPoE Verbindung direkt von meinem PC ohne Router herzustellen und sehe da es funktioniert. Daraufhin habe ich den Mitarbeiter gefragt was Sie denn geändert haben den vor der Umstellung hat mein Router funktioniert. Der Mitarbeiter wurde ziemlich pampig und meinte nur ich soll zu einem Fachgeschäft gehen und mich beraten lassen und Sie hätten nicht geändert. Wohlgemerkt ich hab den Mitarbeiter vorher erzählt das ich als Systemadministrator arbeite was für ein Frechheit dachte ich. Hier komme ich nicht weiter aber evtl. ist google dein Freund 🙂 Und so war es auch so jetzt zur Lösung:


Zusammenfassung Router Config:

– Dienstname/service name: (leer)
– Login: Eure Telefonnummer
– Passwort: Eure Telefonnummer



Damit sollte sich der Router wieder einwählen können. Die Lösung habe ich hier im Hansenet Forum gefunden. Die Support Mirarbeiter schauen anscheint nicht in das Forum der Beitrag ist nämlich von 2007.