• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Script it
 

Script it

on

  • 733 views

Make professional DBA tools out of nothing

Make professional DBA tools out of nothing

Statistics

Views

Total Views
733
Views on SlideShare
653
Embed Views
80

Actions

Likes
0
Downloads
16
Comments
0

2 Embeds 80

https://twitter.com 79
http://www.percona.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Script it Script it Presentation Transcript

    • Script it!Make professional DBA tools out of nothingGiuseppe MaxiaContinuent, Inc11Wednesday, April 24, 13
    • Why not using Puppet or Chef?Why scripting?‣ Not all companies use it‣ Some tasks dont fit the pattern‣ If you want to do testing on-the-fly, knowing how toscript will save time22Wednesday, April 24, 13
    • Why not {Perl|Python|Ruby|PHP|Lua} ?Why shell scripts?‣ Shell is available everywhere• (unless you run Windows, but in this case you are in thewrong room)‣ Shell scripting is powerful‣ It is fast to write, and reasonably easy to read33Wednesday, April 24, 13
    • If you know where you want to go, you can easily list what youneed to do to get thereDefine the objectives of your scripts‣ We want:• Install a master• Install and provision one or more slaves• Check if replication is working44Wednesday, April 24, 13
    • For each goal, find what data you needFind implementation requirements‣ Install a master• Choose a host• Find a data directory with enough storage• Define user names and passwords• Define port, socket, and log directory‣ Install a slave• Choose the hosts• Assume same requirements as the master‣ Check replication• Define which values you need from the database and fromthe OS55Wednesday, April 24, 13
    • The installation should be reversibleInstall clean‣ Objectives:• Do not confuse the installation scripts with their target;• Do not mix installation scripts and the product of theinstallation;• Allow a clean removal of what you installed;66Wednesday, April 24, 13
    • The code is committed to a public repositoryCODE FOR THIS SESSION‣ https://code.google.com/p/tungsten-toolbox‣ Go to "source"‣ Browse the code‣ Look for "deploy samples"‣ You may also need MySQL Sandbox• (http://mysqlsandbox.net)77Wednesday, April 24, 13
    • 88Wednesday, April 24, 13
    • Make your scripts easily changeableTips - 1 - use a config file‣ Do not write host names, ports, paths, user names andpasswords in scripts‣ Use a configuration file‣ Load that file before each script‣ Make different config files for each scenario, and link toit99Wednesday, April 24, 13
    • Simplify remote commandsTips - 2 - Use a remote messenger‣ Do not write a series of "ssh ..." commands‣ Getting the return values correctly and quotes is messy‣ instead:1. create a script with the instructions for a given node2. transfer the file to the node3. run the file remotely1010Wednesday, April 24, 13
    • Help your memory, avoid conflictsTips - 3 - Write a banner‣ When the installation task is finished, write a filecontaining• what you have installed• where dis you install from‣ Check for that file, to avoid overwriting an installation‣ Remove the file when uninstalling1111Wednesday, April 24, 13
    • Type once, type lessTips - 4 - Write customized shortcuts‣ Your installation may depend on variable components(e.g. the path of your tools)‣ As part of the installation, write a shortcut file‣ example:12$ cat client#!/bin/bash$HOME/opt/mysql/5.5.31/bin/mysql $@12Wednesday, April 24, 13
    • Keep them all busyTips - 5 - Run operations in parallel‣ If your operation takes long, and you have to operate it inmany servers:1. Start all of them at once, in background2. Send the output to a different file for each task3. wait for the tasks to finish4. display the contents of the output files1313Wednesday, April 24, 13
    • If they look alike, put them togetherTips - 6 - Use convenience arrays‣ Use arrays to group together items that you want to use:‣ e.g.:• slaves=($NODE2 $NODE3)• all_nodes=($NODE1 $NODE2 $NODE3)• tools=(mysql mysqldump mysqladmin)‣ use loops• for NODE in ${all_nodes[*]} ; do .... ; done1414Wednesday, April 24, 13
    • Remember your goals, and put the data togetherGetting started : the config file‣ Should contain:• list of your nodes• database path• software path• user names• convenience arrays (see tip #6)1515Wednesday, April 24, 13
    • Default Config file (1)$ cat CONFIG_default.sh#!/bin/bashexport NODE1=host1export NODE2=host2export NODE3=host3#export NODE4=host4## ==================export BANNER=$HOME/current_replication.txtexport DB_PORT=3306export DATADIR=/var/lib/mysqlexport BASEDIR=/usr...1616Wednesday, April 24, 13
    • Default Config file (2)...export DB_USER=powerfulexport DB_PASSWORD=can_do_allexport REPL_USER=slave_userexport REPL_PASSWORD=can_do_little## ==================export ALL_NODES=($NODE1 $NODE2 $NODE3 $NODE4)export MASTERS=($NODE1)export SLAVES=($NODE2 $NODE3 $NODE4)exportDASH_LINE=-----------------------------------------------------------1717Wednesday, April 24, 13
    • Customized Config file (1)$ cat CONFIG_sb_5_5_31.sh#!/bin/bash...export DB_PORT=15531export SBDIR=$HOME/sandboxes/mysql_5_5_31export DATADIR=$SBDIR/dataexport BASEDIR=$HOME/opt/mysql/5.5.31export DB_USER=msandboxexport DB_PASSWORD=msandboxexport REPL_USER=rsandboxexport REPL_PASSWORD=rsandbox...1818Wednesday, April 24, 13
    • Use only one:$ ln -s CONFIG_default.sh CONFIG.sh$ ls -l CONFIG*-rwxr-xr-x 1 x.x CONFIG_default.sh-rwxr-xr-x 1 x.x CONFIG_sb_5_1_69.sh-rwxr-xr-x 1 x.x CONFIG_sb_5_5_31.sh-rwxr-xr-x 1 x.x CONFIG_sb_5_6_11.shlrwxrwxrwx 1 x.x CONFIG.sh -> CONFIG_sb_5_5_31.sh1919Wednesday, April 24, 13
    • This should always be the first operationCheck and load for your config in every script$ head install_replication.sh#!/bin/bashif [ ! -f ./CONFIG.sh ]thenecho "Configuration file CONFIG.sh not found"exit 1fi. ./CONFIG.sh2020Wednesday, April 24, 13
    • Make sure you can do what you want to doCode defensivelyfor NODE in ${MASTERS[*]}doRUNNING=$($MYSQL_SLAVE -BN --host=$NODE -e select 1)if [ -z "$RUNNING" ]thenecho "# WARNING:"echo " mysql not reachable "echo " by user $REPL_USER in node $NODE"exit 1fidone2121Wednesday, April 24, 13
    • Filter and cutGet precise values (1)$ mysql -e show master statusG************* 1. row ************File: mysql-bin.000001Position: 107Binlog_Do_DB:Binlog_Ignore_DB:$ mysql -e show master statusG | grep FileFile: mysql-bin.000001$ mysql -e show master statusG | grep File |awk{print $2}mysql-bin.0000012222Wednesday, April 24, 13
    • Filter and cutGet precise values (2)$ mysql -e show master statusG************* 1. row ************File: mysql-bin.000001Position: 107Binlog_Do_DB:Binlog_Ignore_DB:$ mysql -e show master statusG | grep PositionPosition: 107$ mysql -e show master statusG | grep Position |awk{print $2}1072323Wednesday, April 24, 13
    • Filter and cutGet precise values (3)$ BINLOG_POS=$(mysql -e show master statusG | grepPosition |awk {print $2})$ BINLOG_FILE=$(mysql -e show master statusG | grepFile |awk {print $2})$ echo $BINLOG_FILEmysql-bin.000001$ echo $BINLOG_POS1072424Wednesday, April 24, 13
    • Using the precise values, set the slave statusSet replicationfor NODE in ${SLAVES[*]}do$MYSQL --host=$NODE -e stop slave$MYSQL --host=$NODE -e "CHANGE MASTER TOmaster_host=$MASTER, master_port=$DB_PORT,master_user=$REPL_USER,master_password=$REPL_PASSWORD,master_log_file=$BINLOG_FILE, master_log_pos=$BINLOG_POS "$MYSQL --host=$NODE -e start slavedone2525Wednesday, April 24, 13
    • Save yourself some typingCreate and use shortcuts# write utility scriptsfor script in mysql mysqldump mysqladmindoecho #!/bin/bash > $scriptecho "$BASEDIR/bin/$script --user=$DB_USER --port=$DB_PORT $@" >> $scriptchmod +x $scriptdone######$ ./mysql -p2626Wednesday, April 24, 13
    • sample installation run$ ./install_replication.sh# Making sure MySQL is running# MASTER host1: ok# Node host1: ok# Node host2: ok# Node host3: ok# node host1 - server_id: 10# node host1 - log_bin: 1# node host2 - server_id: 20# node host2 - log_bin: 1# node host3 - server_id: 30# node host3 - log_bin: 12727Wednesday, April 24, 13
    • Improved from the one published inhttp://datacharmer/blogspot.comCheck replication‣ read the master status‣ read the slave status‣ compare values‣ scream if they dont match2828Wednesday, April 24, 13
    • sample check runSLAVE host2 : Replication OKfile: mysql-bin.000001 at 106SLAVE host3 : Replication OKfile: mysql-bin.000001 at 106SLAVE host4 : Replication OKfile: mysql-bin.000001 at 1062929Wednesday, April 24, 13
    • Run a test‣ Create a table in the master‣ insert some records‣ Wait 1 second‣ Count the records in all servers3030Wednesday, April 24, 13
    • Sample test run$ ./test_replication.shnode host1 - 0node host2 - 0node host3 - 0node host4 - 0node host1 - 3node host2 - 3node host3 - 3node host4 - 33131Wednesday, April 24, 13
    • Remove replicationMYSQL="$MYSQL --user=$DB_USER --password=$DB_PASSWORD--port=$DB_PORT"for NODE in ${SLAVES[*]}do$MYSQL -BN --host=$NODE -e stop slave$MYSQL -BN --host=$NODE -e reset slavedone### Remove banners and shortcuts3232Wednesday, April 24, 13
    • This method works fine from MySQL 5.0 up to 5.5Install using a different version$ rm CONFIG.sh$ ln -s CONFIG_sb_5_5_31.sh CONFIG.sh$ ./install_replication.sh3333Wednesday, April 24, 13
    • Looks like its the same, but ...Install using a MySQL 5.6./install_replication.sh# Making sure MySQL is runningWarning: Using a password on the command lineinterface can be insecure.# MASTER host1: okWarning: Using a password on the command lineinterface can be insecure.# Node host1: okWarning: Using a password on the command lineinterface can be insecure.# Node host2: okWarning: Using a password on the command lineinterface can be insecure.# Node host3: ok3434Wednesday, April 24, 13
    • Instead of a password in the command line, we generate anoptions file dynamicallyChanging approachexport user_cnf=user$$.cnfecho "[client]" > $user_cnfecho "user=$DB_USER" >> $user_cnfecho "password=$DB_PASSWORD" >> $user_cnfexport MYSQL="$MYSQL --defaults-file=$PWD/user$$.cnf--port=$DB_PORT"3535Wednesday, April 24, 13
    • No nasty messagessample run with new installer and MySQL 5.6$ ./install_replication.sh# Making sure MySQL is running# MASTER host1: ok# Node host1: ok# Node host2: ok# Node host3: ok# node host1 - server_id: 10# node host1 - log_bin: 1# node host2 - server_id: 20# node host2 - log_bin: 1# node host3 - server_id: 30# node host3 - log_bin: 13636Wednesday, April 24, 13
    • Serial restore operations$ time ./provision_slaves.shtaking backuprestoring on slavesWed Apr 24 09:10:55 EDT 2013restore started in node host2restore donerestore started in node host3restore donerestore started in node host4restore doneWed Apr 24 09:11:03 EDT 2013real 0m8.539suser 0m0.323ssys 0m0.442s3737Wednesday, April 24, 13
    • parallel restore operations$ time ./provision_slaves_parallel.shtaking backuprestoring on slavesWed Apr 24 09:12:59 EDT 2013restore started in node host2restore started in node host3restore started in node host4restore donerestore donerestore doneWed Apr 24 09:13:03 EDT 2013real 0m4.330suser 0m0.404ssys 0m0.542s3838Wednesday, April 24, 13
    • Serial restore operations (bigger database)$ time ./provision_slaves.shtaking backuprestoring on slavesWed Apr 24 09:20:11 EDT 2013restore started in node host2restore donerestore started in node host3restore donerestore started in node host4restore doneWed Apr 24 09:23:53 EDT 2013real 3m53.504suser 0m11.401ssys 0m4.698s3939Wednesday, April 24, 13
    • Parallel restore operations (bigger database)$ time ./provision_slaves_parallel.shtaking backuprestoring on slavesWed Apr 24 09:26:11 EDT 2013restore started in node host2restore started in node host3restore started in node host4restore donerestore donerestore doneWed Apr 24 09:28:00 EDT 2013real 1m59.583suser 0m16.505ssys 0m6.983s4040Wednesday, April 24, 13
    • Thanks for your attention‣ Blog: http://datacharmer.blogspot.com‣ Twitter: @datacharmer4141Wednesday, April 24, 13