Monday, November 9, 2009

REPLACE vs. INSERT ... ON DUPLICATE UPDATE in MySQL

REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.

REPLACE is a MySQL extension to the SQL standard. It either inserts, or deletes and inserts.

For another MySQL extension to standard SQL — that either inserts or updates — it should be use “INSERT ... ON DUPLICATE KEY UPDATE Syntax”.

If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed.

REPLACE (http://dev.mysql.com/doc/refman/5.1/en/replace.html)

INSERT ... ON DUPLICATE UPDATE (http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html)

Wednesday, October 7, 2009

Recover MySQL Database root password

If you have set the password for root and forget or unable to recall the password, then you will need to reset the root password for MySQL.
Login as root to the Unix-like (Unix, Linux or BSD) machine with the MySQL server.

Stop the MySQL server by using either of the following command

#/etc/init.d/mysql stop

Now you need to Start MySQL server without password

# mysqld_safe --skip-grant-tables &

Connect to mysql server using mysql client with the following command

# mysql -u root

Now you should be having mysql prompt

mysql>

Now you need to Setup new MySQL root user password

mysql> use mysql;

mysql> update user set password=PASSWORD(”newrootpassword”) where user=’root’;

mysql> flush privileges;

mysql> quit

Note: Replace newrootpassword with the new root password for MySQL server. Flush Privileges is needed to making the password change effect immediately.

Now you need to Stop MySQL Server using the following command

# /etc/init.d/mysql stop

Test Your New Mysql root password

First you need to start mysql server using the following command

# /etc/init.d/mysql start

# mysql -u root -p

Now it will prompt for root password and enter your new root password.


Taken from: http://www.debianadmin.com/recover-mysql-database-root-password.html

Ubuntu version

lsb_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 9.04
Release: 9.04
Codename: jaunty

Thursday, August 20, 2009

Playing with DATE and FOR in XP DOS

@echo off

rem for /F "tokens=1-4 delims=/ " %%i in ('date /t') do set dow=%%i && set month=%%j && set day=%%k && set year=%%l
rem for /F "tokens=1-4 delims=/ " %%i in ('date /t') do set dow=%%i && set date=%%j-%%k-%%l
rem for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set date=%%i-%%j-%%k

for /F "tokens=1-4 delims=/ " %%i in ("%DATE%") do (
set dow=%%i
set dom=%%j-%%k-%%l
set month=%%j
set day=%%k
set year=%%l
)

set datew=%DATE:~0,3%
set datem=%DATE:~4,2%-%DATE:~7,2%-%DATE:~10,4%
set daten=%month%.%day%.%year%

echo %dow%
echo %datew%
echo %datem%
echo %daten%

Friday, July 10, 2009

UDP server in Perl

#!/usr/bin/perl

use strict;
use warnings;

use IO::Socket::INET;
use IO::Handle;

if ($#ARGV <>
die "Argument should be an integer port number\n";
} elsif ($ARGV[0] =~ /\D+/) {
die "Argument should be an integer port number\n";
}

my $Port = $ARGV[0];

my $MySocket = new IO::Socket::INET->new(
LocalPort => $Port,
Proto => 'udp'
) or die "Error opening UDP port $Port $!\n";

print "\nListening on UDP port $Port ...\n\n";

my $MsgText;

open(FILE, ">>udpserver.txt") or die "Error opening udpserver.txt file $!\n";
FILE->autoflush(1);
$MySocket->autoflush();

my $count = 1;
while ($MySocket->recv($MsgText, 4096)) {
print "$count <$MsgText>\n";
print FILE "$count <$MsgText>\n";
$count++;
}

close(FILE);

__END__