<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MySQL Preacher &#187; automation</title>
	<atom:link href="http://mysqlpreacher.com/wordpress/tag/automation/feed/" rel="self" type="application/rss+xml" />
	<link>http://mysqlpreacher.com/wordpress</link>
	<description>Because Sharing is Caring</description>
	<lastBuildDate>Mon, 26 Sep 2011 23:34:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Automating MySQL access with expect and bash scripting</title>
		<link>http://mysqlpreacher.com/wordpress/2010/02/automating-mysql-access-with-expect-and-bash-scripting/</link>
		<comments>http://mysqlpreacher.com/wordpress/2010/02/automating-mysql-access-with-expect-and-bash-scripting/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 17:08:26 +0000</pubDate>
		<dc:creator>Darren Cassar</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[OS]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[expect]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://mysqlpreacher.com/wordpress/?p=327</guid>
		<description><![CDATA[If you have multiple database servers with strange names, or if you have to hop over multiple machines to connect to any mysql database server, then you know what a pain it can be to administer such a setup. Thanks to some scripting, you can automate such tasks as follows: Create an expect script: /path/to/sshmysql.exp [...]]]></description>
			<content:encoded><![CDATA[<p>If you have multiple database servers with strange names, or if you have to hop over multiple machines to connect to any mysql database server, then you know what a pain it can be to administer such a setup. Thanks to some scripting, you can automate such tasks as follows:</p>
<p>Create an expect script:<br />
/path/to/sshmysql.exp</p>
<blockquote><p>#!/usr/bin/expect -f<br />
#script by darren cassar<br />
#mysqlpreacher.com</p>
<p>set machine  [lindex $argv 0]</p>
<p>set timeout -1</p>
<p>spawn ssh username@$machine<br />
match_max 100000<br />
expect -exact &#8220;assword: &#8220;<br />
send &#8212; &#8220;password\r&#8221;<br />
send &#8212; &#8220;sudo -k; sudo su &#8211; mysql\r&#8221;<br />
expect -exact &#8220;sudo -k; sudo su &#8211; mysql&#8221;<br />
expect -exact &#8220;assword:&#8221;<br />
send &#8212; &#8220;password\r&#8221;<br />
interact</p></blockquote>
<p># you should change the word password in &#8216;send &#8212; &#8220;password\r&#8221;&#8216; to your login password<br />
# if you have the same password for each environment you could also script logging into mysql directly from the same expect script BUT that is not recommended.</p>
<p>Create a bash script:<br />
/path/to/login.sh</p>
<blockquote><p>#!/bin/bash<br />
#script by darren cassar<br />
#mysqlpreacher.com</p>
<p>sm=&#8217;/path/to/sshmysql.exp&#8217;</p>
<p>menu() {<br />
  echo &#8221; 101 &#8211; dev.databaseserver1 &#8220;<br />
  echo &#8221; 102 &#8211; dev.databaseserver2 &#8220;<br />
  echo &#8221; 103 &#8211; dev.databaseserver3 &#8220;<br />
  echo &#8221; 201 &#8211; qa.databaseserver1 &#8220;<br />
  echo &#8221; 301 &#8211; uat.databaseserver1 &#8220;<br />
  echo &#8221; 302 &#8211; uat.databaseserver2 &#8220;<br />
  echo &#8221; 401 &#8211; prod.databaseserver1 &#8220;<br />
  echo &#8221; &#8220;<br />
}</p>
<p>ARGUMENT=notmenu</p>
<p>if [ -z "$1" ]<br />
  then<br />
    ARGUMENT=menu<br />
else<br />
  choice=$1<br />
fi</p>
<p>if [ $ARGUMENT = "menu" ]<br />
  then<br />
    menu<br />
else<br />
  case &#8220;$choice&#8221; in<br />
  101|dev.databaseserver1   ) $sm dev.databaseserver1;;<br />
  102|dev.databaseserver2   ) $sm dev.databaseserver2;;<br />
  103|dev.databaseserver3   ) $sm dev.databaseserver3;;<br />
  201|qa.databaseserver1   ) $sm qa.databaseserver1;;<br />
  301|uat.databaseserver1   ) $sm uat.databaseserver1;;<br />
  302|uat.databaseserver2   ) $sm uat.databaseserver2;;<br />
  401|prod.databaseserver1   ) $sm prod.databaseserver1;;<br />
  *        ) echo &#8220;Wrong value passed to script&#8221;<br />
             menu ;;<br />
  esac<br />
fi</p></blockquote>
<blockquote><p>alias l=&#8217;/path/to/login.sh&#8217;</p></blockquote>
<p>Output: </p>
<blockquote><p>[darrencassar@mymachine ~ ]$ l<br />
 101 &#8211; dev.databaseserver1<br />
 102 &#8211; dev.databaseserver2<br />
 103 &#8211; dev.databaseserver3<br />
 201 &#8211; qa.databaseserver1<br />
 301 &#8211; uat.databaseserver1<br />
 302 &#8211; uat.databaseserver2<br />
 401 &#8211; prod.databaseserver1</p></blockquote>
<p>Output:<br />
The below command would log you into the first development database server as mysql user.</p>
<blockquote><p>[darrencassar@mymachine ~ ]$ l 101 </p></blockquote>
<p>On each machine place aliases for each instance in the .profile</p>
<blockquote><p>alias use3306=&#8217;mysql -u root -p -h 127.0.0.1 -P 3306 &#8211;prompt=&#8221;mysql \D> &#8220;&#8216;</p></blockquote>
<p>The above setup can be used using any client/server OS: Linux, Solaris, MAC OS or Windows(running Cygwin)</p>
<p><strong>NOTE: If you store the password in clear text inside the expect script, you should at least save the scripts inside an encrypted partition on your machine and make sure that folder is not shared or accessible by anyone. Another way of doing it would be to use either SSHKeys OR save the password inside a file and encrypt it using <a href="http://www.madboa.com/geek/openssl/#encrypt-simple" target="_blank">OpenSSL</a></strong></p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://mysqlpreacher.com/wordpress/2010/02/automating-mysql-access-with-expect-and-bash-scripting/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

