Tuesday, May 12, 2009

Basic MS SQLServer backup and restore commands

1. backup
1.1 backup database
BACKUP DATABASE [_db] TO DISK = N'C:\dbBackupTest\MC\db\test' WITH NOINIT , NOUNLOAD , NAME = N'_db backup', NOSKIP , STATS = 10, NOFORMAT
1.2 backup transaction log
BACKUP LOG [db] TO DISK = N'C:\dbBackupTest\MC\db\testlog' WITH NOINIT , NOUNLOAD , NAME = N'_db backup', NOSKIP , STATS = 10, NOFORMAT
--restore
--allow transaction log restore after database restore
RESTORE DATABASE [_db] FROM DISK = N'C:\dbBackupTest\MC\cdsdb\_db_200905090700.BAK' WITH FILE = 1, NOUNLOAD , STATS = 10, NORECOVERY
--allow more transaction log restore after transaction log restore
RESTORE LOG [_db] FROM DISK = N'C:\dbBackupTest\MC\db\_db_tlog_200905091000.TRN' WITH FILE = 1, NOUNLOAD , STATS = 10, NORECOVERY
--last transaction log, put the db operational
RESTORE LOG [_db] FROM DISK = N'C:\dbBackupTest\MC\db\_db_tlog_200905100600.TRN' WITH FILE = 1, NOUNLOAD , STATS = 10, RECOVERY

Wednesday, April 22, 2009

Setting master slave relationship between MySQL DBs

1.creating a user for replication.
Each slave must connect to the master using a standard MySQL user name and password, so there must be a user account on the master that the slave can use to connect. privilege REPLICATION SLAVE must be granted to the selected replication user.

GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';

2. configure a master
2.1 must enable binary logging on master.
log-bin=c:\xxx\log file name
server-id=x
#specify the db replication is for
binlog-do-db=exampledb

3. configure a slave
server-id=2
master-host=192.168.0.100
master-connect-retry=60
replicate-do-db=exampledb
#these two parameters can be defined in script
master-user=slave_user
master-password=secret

you do not need to enable binary log on a slave.

or you can do by mysql command:
CHANGE MASTER TO MASTER_HOST='master_host_name',MASTER_USER='replication_user_name',MASTER_PASSWORD='replication_password',MASTER_LOG_FILE='recorded_log_file_name',MASTER_LOG_POS=recorded_log_position;

usually, it's not necessary to specify the log file and log position information. as a good practice, the following is a suggested sequence on setting slave:
- set slave server-id and master-host in mysql configure file
- stop slave
- reset slave
- change master to xxxxx
- start slave

4. to load data from master if the source db is myisam type and there's no db created on slave yet. in order to run that command successfully, RELOAD,SUPER and SELECt privileges on the tables you want to load have to be granted to the replication user.

load data from master;

5. replicate to another database
eg. db1 to db2 on another server
replicate-rewrite-db=db2->db2
replicate-do-db=db2

Misc:
A better method to ensure exact replication would go like the below:
1) start master
2) grant replication to slave
3) run "show master status" and record the index file and it's offset position
4) start the slave
5) run "stop slave"
6) run "change master to master_host='[MASTER HOST IP]', master_user='[USER]', master_password='[PASSWORD]', master_log_file='[NAME]', master_log_pos=[POSITION]" (from #3)
7) run "start slave"


misc:
1. get master replication information
-flush tables with read lock;
-show master status;
-unlock tables;
2. get slave replication information
-show slave status;
3.enforce log switch
flush logs;
4.retrieve log information
-show master logs
-show slave logs

Tuesday, April 21, 2009

Backup and Restore MySQL Database

1. Configure mysql to generate binlog.
example:
in my.ini, define these two lines
log-bin=c:\mysql_blog\mysqlblog
max_binlog_size=10m

2.make scheduled task that runs script to make full backup specified database.
the command used can be mysqldump

3. make scheduled task that runs script to make back up of binlog files. an easy way to back up only the newly created bin log files is to remove the binlog files have been backed up.

the backup scripts can rotate after a certain period.

upon the time to restore the database from disaster such as hard disk failure, the following steps can be followed.

1. restore the database from recent full database backup
mysql -u -p db 2. restore all the bin logs since the recent full database backup
mysqlbinlog binlogfiles binlogfile2 ... >onebig file
mysql -u -p db <onebig file

IO redirection can also be used here

Friday, March 20, 2009

Always successful Grant statement in MySQL

If you do not want to fail a grant statement in Mysql, you can always include "Create" privilege in the statement. In this way, you can grant privilege to a table that is not existing yet. The table can be a table even in a non-existing database.

E.g

Grant create, select on db.tbl to user@localhost identified by 'TEST'

This statement will alway successfully executed on any mysql server.

Thursday, March 12, 2009

Regular Expressions in Java and C#

This is a summary from reading "Introduction to Regular Expressions" written by Larry Mak

The basic idea of using regular expression is to match and to replace. The former determines if the pattern is in the string, and if so, find it. Replace changes the string according to the pattern to another pattern.

In C#, Regex in
System.Text.RegularExpressions is used to do the match. In Java, Pattern and Matcher in package java.util.regex are used do the work.

C#:
Regex.IsMatch( string data, "Hello" )

Java:
Pattern pat = Pattern.compile("Hello");
Matcher m = pat.matcher( data );
if ( m.find() )

To define a boundary in regular expression, you use "\b"

To match more than once in same string, in C#, you write something like below to loop all the matches:
   for ( Match m = Regex.Match( str, patternToMatch); m.Success; m = m.NextMatch() )
In Java, you use matcher's find method:
   Pattern pat = Pattern.compile(pattern);
Matcher m = pat.matcher( s );
while ( m.find() ){
System.out.println( m.group() );
}
Capturing groups are numbered by counting their opening parentheses from left to right.
E.g. $(\d+)\.(\d\d). For each matching, it can be further detailed to capturing groups.
In C#, GroupCollection gc = m.Groups;
In Java, matcher.group(i)

Replacement
In C#, use Regex.Replace( str, search, replace );
In Java, use matcher.replaceAll() method

One powerful operation you can do with regular expression is to change the
string with what you captured. you define the groups in order from 1,
and you can rearrange the groups by reorganize the order or groups,
for example, $1$3$2.






Tuesday, March 10, 2009

Refresh on GridBagLayout

I have to work on a GUI in Java recently. Since it works with JDK1.5 so that the most convinient and flexiable GroupLayout can't be a candidate. The NetBean 6.5 does not provide automatic conversion from GroupLayout to GridBagLayout as what has been provided in JBuilder3.x back to more than 10 years in 1998, and the visual Editor for eclipse does not fully support free GUI design, so that I have to code it by hand. Once again it reminds me that Borland's tools are the best.

This is a good chance for me to refresh the knowledge on layout managers in Java. Most of the content below is from http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html.

GridBagLayout is the second most flexiable and powerful layout manager next to GroupLayout. It places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. Not all rows necessarily have the same height. Similarly, not all columns necessarily have the same width. Essentially, GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be.

This resizing behavior is based on weights the program assigns to individual components in the GridBagLayout. The way the program specifies the size and position characteristics of its components is by specifying constraints for each component. The preferred approach to set constraints on a component is to use the Container.add variant, passing it a GridBagConstraints object.

It is possible to reuse the same GridBagConstraints instance for multiple components, even if the components have different constraints. However, it is recommended that you do not reuse GridBagConstraints, as this can very easily lead to you introducing subtle bugs if you forget to reset the fields for each new instance

Other attributes are easy to understand, below are the ones need to pay more attention to.

weightx, weighty

Specifying weights is an art that can have a significant impact on the appearance of the components a GridBagLayout controls. Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior.

Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.
Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary. Larger numbers indicate that the component's row or column should get more space. For each column, the weight is related to the highest weightx specified for a component within that column, with each multicolumn component's weight being split somehow between the columns the component is in. Similarly, each row's weight is related to the highest weighty specified for a component within that row. Extra space tends to go toward the rightmost column and bottom row.

Tuesday, March 03, 2009

Mouse problem in Word 2007

Symptom:

the mouse simply does not select any thing or locate to any position in work document.
keyboard is still working.

Related tries:
- remove HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Word\Data
- disable add-ins under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Word\Addins

Failed in my case.

Final solution:
-start word in secure mode:winword /a. this is because under normal start, the mouse does not work in the option dialog.
-work options-->add-ins
disable suspected add-ins will get the problem fixed. in my case, the expired power designer add-in caused the failure of mouse function in word 2007.