Wednesday, March 18, 2015

My Experience on Migrating a Database

I have a server that runs both application and database and all resources are utilized on peak time, so I migrated the database to 2 other new servers with more capacity.

It is about the whole story from OS installation to DB migration and the challenges on the way.

Use raid controller for mirroring 1st 2 disks for / and /boot.
The final layout of the root partitions is as:

sda 557.9G  0 disk
sda1 200M  0 part   /boot/efi
sda2 500M  0 part   /boot
sda3 557.2G  0 part
g_root-lv_root 424G  0 lvm    /
g_root-lv_swap 16G  0 lvm    [SWAP]
vg_root-lv_varlog 19.5G  0 lvm    /var/log
vg_root-lv_home 97.7G  0 lvm    /home

The /boot/efi may appear new that is because of using UEFI mode of BIOS boot agent.

Clearing old partitions of other disks after installing OS (RHEL):

For each disk that has partition
fdisk /dev/sd? -> p ->d -> w
or
dmsetup remove /dev/mapper/ddf???????????????????????


LVM:
pvcreate /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg
vgcreate -s 16M vg_data /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf
lvcreate --type raid6 -i 3 -L 1.5T -n lv_mysql vg_data
mkfs.ext4 /dev/mapper/vg_data-lv_mysql

Mounting Permanently:
/etc/fstab
/dev/mapper/vg_data-lv_mysql /data/mysql        ext4    defaults        1 2

SE Linux:
install semanage
yum install policycoreutils-python-2.0.83-19.47.el6.x86_64

Adding permission for new customized directory of mysql
semanage fcontext -a -t mysqld_db_t "/data/mysql(/.*)?"
restorecon -R -v /data/mysql

Firewall:
yum install system-config-firewall-1.2.27-7.1.el6.noarch

iptables -I INPUT 4 -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
service iptables status
service iptables save

Note: -I INPUT 4 is putting this rule before the global rejection rule that is there by default, The status should be checked and ensure about the position of the rule, here 4.


MySQL:
Update /etc/my.cnf
mysql_install_db --defaults-file=/etc/my.cnf --user=mysql
service mysql start

Grant options for defining replication on both servers
mysql  -p -h 127.0.0.1 -P3306 -u root -e "GRANT ALL ON *.* TO root@'%' WITH GRANT OPTION;"

Install mysql utilities
yum install mysql-connector-python-2.1.1-1.el6.noarch.rpm
yum install mysql-utilities-commercial.noarch

mysqlreplicate --master=root@10.1.196.4:3306 --slave=root@10.1.196.12:3306 --rpl-user=rpl:sLavep@ss

Note: the --rpl-user values will be created automatically, no need to define that user.

Checking Replication Status:
mysqlrplcheck --master='root@10.1.196.4:3306' --slave='root@10.1.196.12:3306'

mysqlrplshow --master='root@10.1.196.4:3306' --discover-slaves-login='root'

Master Only:
mysql_secure_installation
mysql>
update mysql.user set user = 'dba', password=Password('123456'), host = '%' where user = 'root' and host = '::1';
SET sql_log_bin = 0;
delete from mysql.user where user = 'root';
SET sql_log_bin = 1;
flush privileges;

Note: SET sql_log_bin can stop/start replication of the current session.

SLAVE:
SET sql_log_bin = 0;
delete from mysql.user where user = 'root';
delete from mysql.user where host = 'hostname' and user = '';
SET sql_log_bin = 1;
flush privileges;

Checking replication status:
show slave status \G

Fixing replication failures:
Running empty transaction for failed transaction id (GTID)

SLAVE:
If there is error:
show slave status \G;
SET GTID_NEXT="2e82f8b8-cc26-11e4-bb69-0010e02446b8:3"
BEGIN; COMMIT;
SET GTID_NEXT="AUTOMATIC";
STOP SLAVE;
START SLAVE;
or
SET GLOBAL sql_slave_skip_counter = 1;

Redirecting old server processes to new DB server:
vi ~/.my.cnf
[client]
user=dba
password=123456
host=10.1.196.4

chmod og-rwx,u-w ~/.my.cnf

DB Migration:
Export:
mysqldump  --tab=./System/ System_db
mysqldump  --tab=./db2/ db2
mysqldump  --tab=./Dimension/ Dimension

Statistics Database:
mysqldump  --tab=./stat/ --no-data CS3_Statistics

Note: --no-data is used because I don't want to migrate all tables data in this phase

mysqldump  --tab=./stat/ --ignore-table=Statistics.ProcessStatistics --ignore-table=Statistics.Log --ignore-table=Statistics.Statistics_System  --ignore-table=Statistics.ServerIf --single-transaction Statistics &> /tmp/dump.log &

Note: --single-transaction is used to  prevent table lock.

Import:
mysqladmin create System_db
mysqladmin create db2
mysqladmin create Dimension
mysqladmin create Statistics

cat *.sql | mysql System_db
repeat for each db export

Note: In some cases that you have a view or foreign key you should ensure the order of sql and txt files to keep consistency. This order is not keeping by use *.sql or *.txt and you may need to list files manually.

mysqlimport --local Dimension Dimension/*.txt

MySQL Proxy:
stop old database server to free the listen port

Run mysql proxy to redirect connections to new server
./bin/mysql-proxy  --defaults-file=my.cnf

MySQL Connection Error Checking

Current Counts:
select * FROM performance_schema.host_cache;
SHOW GLOBAL STATUS LIKE 'Aborted_connects'


Clear Abort Connections:
mysql > Flush Hosts;
mysqladmin flush-hosts

Sunday, March 15, 2015

Clear BIOS Raid Disks

Get Info:
dmsetup info
dmsetuo list

Clear Specific Device:
dmsetup remove /dev/mapper/ddf1_4c534920202020201000007910009263471147119133e6ca


Clear All Device (be cautious about operational and lvm disks as they are in the list):
dmsetup remove_all



Sunday, March 8, 2015

Update the Boot Archive on a RAID 1 Volume

Check the current meta devices and mirror status before reboot
metastat -p
like d0 d10 d20

Go to fail safe mode:
ok boot -F failsafe or reboot -- "-F failsafe"

Mount primary submirror
mount /dev/dsk/c0t0d0s0 /a

Update vfstab
backup original cp /a/etc/vfstab /a/etc/vfstab.orig

Comment out / filesystem
#/dev/md/dsk/d0     /dev/md/rdsk/d10    /     ufs    1    no    -

Add sub-mirror disk as /
/dev/dsk/c0t0d0s0 /dev/rdsk/c0t0d0s0  /     ufs    1    no    -

Update system conf
Backup original cp /a/etc/system /a/etc/system.orig

Comment out rootdev
* Begin MDD root info (do not edit)
# rootdev:/pseudo/md@0:0,0,blk
* End MDD root info (do not edit)

Update boot archive
bootadm update-archive -R /a

Reboot
umount /a
shutdown -i 6

Detach faulty sub-mirror
metadetach d0 d20

Replace changed file
cp /etc/vfstab.orig /etc/vfstab
cp /etc/system.orig /etc/system

Reboot
shutdown -i 6

Attach sub-mirror
metattach d0 d20

Check resync
metastat

Meta Device in Single Mode

boot cdrom -s

mkdir /tmp/a

ls -lthr /dev/dsk

mount -o ro /dev/dsk/c0t5000CCA01535BEA8d0s0 /tmp/a

cp /tmp/a/kernel/drv/md.conf /kernel/drv/md.conf

umount /tmp/a

update_drv md

metastat /dev/md/dsk/d0

metastat

mount /dev/md/d0 /tmp/a