Jan 25 2009

Quick DB Setups with MySQL Sandbox

Category: Databasesjgoulah @ 2:57 PM

Introduction

There are various reasons to setup quick “sandbox” instances of MySQL. You can use them to test different types of replication (such as master-master or various slave topologies), to test your code against different versions of MySQL, or to setup instances of MySQL on a per developer basis where each person has their own database running on a different port so they can breakdown/setup the DB easily or make schema changes without affecting other team members. A perfect tool to do all of these things easily is MySQL Sandbox.

Download the Prerequisites

To use MySQL sandbox effectively you need two things, the MySQL sandbox tool itself, and a MySQL tarball that the sandbox script can use to setup instances.

You can download the latest MySQL sandbox version like so (2.0.12 as of this writing):

wget http://launchpad.net/mysql-sandbox/mysql-sandbox-2.0/2.0/+download/mysql_sandbox_2.0.12.tar.gz

You can download the MySQL tarball from any MySQL mirror. Its important to get the non RPM, Intel C/C++ compiled, glibc-2.3 version (look for a tar.gz file with “icc” in the filename) for example if you want version 5.1:

wget ftp://mirror.anl.gov/pub/mysql/Downloads/MySQL-5.1/mysql-5.1.30-linux-i686-icc-glibc23.tar.gz

Installing an Instance with MySQL Sandbox

First you need to extract the MySQL Sandbox tool and change directory to it:

$ tar xzvf mysql_sandbox_2.0.12.tar.gz
$ cd mysql_sandbox_2.0.12

The easiest and quickest way to create an instance is:

$ ./make_sandbox /path/to/mysql-X.X.XX-osinfo.tar.gz

where mysql-X.X.XX-osinfo.tar.gz is the MySQL tarball we just downloaded. And you’re done.

However, this will put the sandbox in a directory under your home directory ($HOME/sandboxes/msb_X_X_XX), which may or may not suit your purposes. It sets it up with default users, passwords, ports, and directory name. Lets fine tune things a bit.

Setting up a Custom Tuned Instance

I want to put my instance into a partition I created called /mnt/mysql_sandboxes. I’ve created a subdirectory in there called tarballs, which holds the MySQL tarball that we downloaded above which MySQL Sandbox will extract for setup. Since I’m installing version 5.1.30 I want to call the directory that houses the MySQL data files 5.1.30_single, but you can call it anything you like. I’ll create a default user named jgoulah and a password goulah. By default it sets the port to the version number without the dots (5130 in this case) so we’ll give it a custom port so that it listens on 10000 instead.

mysql_sandbox_2.0.12 $  ./make_sandbox \
/mnt/mysql_sandboxes/tarballs/mysql-5.1.30-linux-i686-icc-glibc23.tar.gz \
--upper_directory=/mnt/mysql_sandboxes/ --sandbox_directory=5.1.30_single \
--db_user=jgoulah --db_password=goulah --sandbox_port=10000

Here’s the output:

unpacking /mnt/mysql_sandboxes/tarballs/mysql-5.1.30-linux-i686-icc-glibc23.tar.gz
Executing ./low_level_make_sandbox \
        --basedir=/mnt/mysql_sandboxes/tarballs/5.1.30 \
        --sandbox_directory=msb_5_1_30 \
        --install_version=5.1 \
        --sandbox_port=5130 \
        --no_ver_after_name \
        --upper_directory=/mnt/mysql_sandboxes/ \
        --sandbox_directory=5.1.30_single \
        --db_user=jgoulah \
        --db_password=goulah \
        --basedir=/mnt/mysql_sandboxes/tarballs/5.1.30 \
        --sandbox_port=10000 \
        --my_clause=log-error=msandbox.err
    The MySQL Sandbox,  version 2.0.12 16-Oct-2008
    (C) 2006,2007,2008 Giuseppe Maxia, Sun Microsystems, Database Group
installing with the following parameters:
upper_directory                = /mnt/mysql_sandboxes/
sandbox_directory              = 5.1.30_single
sandbox_port                   = 10000
datadir_from                   = script
install_version                = 5.1
basedir                        = /mnt/mysql_sandboxes/tarballs/5.1.30
my_file                        =
operating_system_user          = jgoulah
db_user                        = jgoulah
db_password                    = goulah
my_clause                      = log-error=msandbox.err
prompt_prefix                  = mysql
prompt_body                    =  [\h] {\u} (\d) > '
force                          = 0
no_ver_after_name              = 1
verbose                        = 0
load_grants                    = 1
no_load_grants                 = 0
do you agree? ([Y],n) y
loading grants
. sandbox server started
installation options saved to current_options.conf.
To repeat this installation with the same options,
use ./low_level_make_sandbox --conf_file=current_options.conf
----------------------------------------
Your sandbox server was installed in /mnt/mysql_sandboxes//5.1.30_single

Its now installed and started up, we can see that the process is running with the correct options:

$  ps -ef | grep mysql | grep jgoulah
jgoulah  11128     1  0 13:48 pts/3    00:00:00 /bin/sh /mnt/mysql_sandboxes/tarballs/5.1.30/bin/mysqld_safe --defaults-file=/mnt/mysql_sandboxes//5.1.30_single/my.sandbox.cnf
jgoulah  11203 11128  0 13:48 pts/3    00:00:00 /mnt/mysql_sandboxes/tarballs/5.1.30/bin/mysqld --defaults-file=/mnt/mysql_sandboxes//5.1.30_single/my.sandbox.cnf --basedir=/mnt/mysql_sandboxes/tarballs/5.1.30 --datadir=/mnt/mysql_sandboxes//5.1.30_single/data --user=jgoulah --log-error=/mnt/mysql_sandboxes//5.1.30_single/data/msandbox.err --pid-file=/mnt/mysql_sandboxes//5.1.30_single/data/mysql_sandbox10000.pid --socket=/tmp/mysql_sandbox10000.sock --port=10000

And we can connect to it on the port 10000:

$  mysql -u jgoulah --protocol=TCP  -P 10000 -pgoulah

You can also go into the directory where we’ve installed this and there are some convenience scripts:

$ cd /mnt/mysql_sandboxes/5.1.30_single/

You can run the use script to connect into mysql (same thing we just did above except we don’t have to remember our port, user, or pass):

$ ./use
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.30 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql [localhost] {jgoulah} ((none)) >

Stop the instance:

$  ./stop

Or start it back up again:

$  ./start
. sandbox server started

There are a few other scripts which you can experiment with in this directory which are documented here

Setting up a Replicated Instance

The nice thing about this tool is it will also setup replicated instances of MySQL with a single command. This allows you to test your application under a replicated environment, or even test different replication topologies including multiple slaves or multi-master replication.

We’ll use similar options as the single instance above, except we’ll use port 11000 this time (the slaves get port + 1, …, port + n where n is number of slaves). We’ll put the install into /mnt/mysql_sandboxes/5.1.30_replicated. Note this time we use the make_replication_sandbox script:

mysql_sandbox_2.0.12 $  ./make_replication_sandbox \
/mnt/mysql_sandboxes/tarballs/mysql-5.1.30-linux-i686-icc-glibc23.tar.gz \
--upper_directory=/mnt/mysql_sandboxes/ \
--replication_directory=5.1.30_replicated --sandbox_base_port=11000
installing and starting master
installing slave 1
installing slave 2
starting slave 1
. sandbox server started
starting slave 2
. sandbox server started
initializing slave 1
initializing slave 2
replication directory installed on /mnt/mysql_sandboxes//5.1.30_replicated

Now we have a master and two slaves going. Note that the command to setup replicated sandboxes will not let us specify the user as we did with the single instance, but two users are created by default:

User: root@localhost Password: msandbox
User: msandbox@% Password: msandbox

You can run the use script as shown above, or connect directly to the master:

$  mysql -u msandbox --protocol=TCP  -P 11000 -pmsandbox

Create a database:

mysql> create database jg_repl_test;
mysql> exit;

Connect to one of the slaves:

$  mysql -u msandbox --protocol=TCP  -P 11001 -pmsandbox
mysql> show databases like '%jg_%';
+------------------+
| Database (%jg_%) |
+------------------+
| jg_repl_test     |
+------------------+

And we can see that it has replicated across.

There are various options you can give the make_replication_sandbox command, for example you can give it the –master_master option to setup a multi master instance.

Conclusion

We’ve seen how to create a single instance of MySQL and also a replicated master with two slaves. Each of these takes only a few seconds to setup once you have the directory layout decided. There really isn’t a much easier way to setup scratch instances of MySQL for all kinds of different purposes.

Tags:

6 Responses to “Quick DB Setups with MySQL Sandbox”

  1. Ila says:

    Nice Tutorial. Thanks! It really helped me setting up MySQL sandbox and connect to it locally using mysql client and also using JDBC. But I have no luck connecting to it from another machine in the same network. It gives the following error
    ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.73.22' (113)
    Or when I try using jdbc to connect remotely, it gives the following exception:

    An error occurred while establishing the connection:
    Type: com.mysql.jdbc.CommunicationsException Error Code: 0 SQL State: 08S01

  2. jgoulah says:

    Show the command you’re using to connect?

  3. Ila says:

    Sorry i couldn’t reply earlier. When I installed the sandboxed MySQL (version 5.1.32), the installation process created a directory named ’5.1.32′ . I navigate to this directory and execute the following code:

    ./mysql -u root --protocol=TCP -P 5132 -p

    or

    ./mysql -u root --protocol=TCP -P 5132 -p -h localhost

    The above code(s) works perfectly by asking the password and allowing me to login and I get to the mysql prompt

    However if i execute the same command from another machine in the same network but with a host attribute, then it is not working. Here is the command i am issuing

    mysql -u root --protocol=TCP -P 5132 -p -h 192.168.73.22

    and this is the error i am getting:
    ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.73.22' (113)

    Any help/pointers to solve this problem would be much appreciated. Thanks!

  4. Ila says:

    Ok. I found the problem.. In the end, it was the firewall rules in the machine that hosts MySQL sandbox. I have to open up the port 5132 to allow remote machine to connect to. Good Tutorial! It really helped me setting up my machine. Thanks again!

  5. Tools4Mac: MySQL Sandbox | Frankys Blog says:

    [...] einer produktiv, sondern in einer Sandbox Umgebung. Ein schönes Tutorial hat John Goulah in seinem Blog dazu verfasst. Gerade für den Master -> Slave Betrieb bzw. die Replikation der Datenbanken auf [...]

Leave a Reply

You must be logged in to post a comment.



  • new england patriots 98.5
  • hp support 530
  • zara phillips kids
  • la ink ink
  • tubing
  • new england patriots 1996 roster
  • connecticut 30 news
  • princes
  • neptune
  • bea 460 bosch
  • juliana
  • mtv rivals
  • bea 00037
  • baer
  • hong
  • search lsu.edu
  • transfers
  • cspan journal
  • dist 95
  • getaway
  • chad ochocinco age
  • bea karp
  • zara phillips husband
  • randy moss jail
  • connecticut education
  • randy moss legal issues
  • search engines for jobs
  • waffle
  • fundamentals
  • zara phillips wedding date
  • chicago bears 17 lisa lampanelli
  • bea goldfishberg
  • search engines no follow
  • zara phillips wedding hat
  • breaks
  • vince young yahoo stats
  • chicago bears tickets
  • search protocol host
  • joshua
  • xanadu bengals
  • connecticut football
  • plated
  • rambler
  • search 2.0
  • bea 71 series staples
  • chad ochocinco ultimate catch cast
  • tea party birthday
  • randy moss college
  • mtv executivesmtv fantasy factory
  • chicago bears zip hoodie
  • bengals for adoption
  • hp support greece
  • bengals tryouts
  • hp support englandhp support forum
  • bengals forum
  • lists
  • bengals arrests
  • mesa
  • dis windsor wi
  • randy moss future
  • bea fox
  • battleship yamato wreck
  • battleship aurora
  • chicago bears rumors 2011
  • hp support id
  • painted
  • chad ochocinco quotes video
  • vince young 6
  • chicago bears football club
  • vince young redskins
  • conditions
  • chad ochocinco nascar
  • battleship classes
  • frequency
  • havelock
  • new england patriots wiki
  • connecticut 104.1
  • dis systems
  • connecticut lakes
  • chicago bears garter
  • beagle
  • skate
  • trusted
  • chad ochocinco quickstep
  • tea party nj
  • dangerous
  • hp support helpline
  • cspan government shutdown
  • dually
  • c span 4 to 5
  • bend
  • mtv jams
  • search engines rankings 2011
  • zara phillips facebookzara phillips gossip
  • new england patriots kim kardashian
  • la ink book an appointment
  • battleship layout
  • tea party agenda
  • new england patriots jake locker
  • randy moss mix
  • search with image
  • units
  • la ink map
  • input
  • dist 91
  • $200
  • mtv 2 schedule
  • hp support englandhp support forum
  • new england patriots 65
  • searchbugsearch engines
  • la ink season 6
  • new england patriots needs
  • connecticut 97.7connecticut attorney general
  • chad ochocinco yesterday
  • randy moss vikings 2011
  • dis 2012 conference
  • sperry
  • lease
  • chad ochocinco 15
  • foster
  • cameras
  • chicago bears gifts
  • zara phillips baby
  • la ink youtube pixie
  • connecticut 5 star resorts
  • mtv 5 cover
  • connecticut 5th district
  • search comcast net
  • gelatin
  • zara phillips tongue
  • teck
  • la ink cast
  • resistivity
  • bangles eternal flame mp3bengals forum
  • buss
  • protector
  • la ink season 5
  • vince young 3rd 30
  • connecticut department of labor
  • new england patriots helmet
  • connecticut limo
  • discjuggler
  • chicago bears pictures
  • mtv oddities
  • hp support error 1005
  • bea taylor
  • search engines zuula
  • cspan facebook
  • mtv music awards
  • vince young injury
  • search engines 9
  • battleship hacked
  • gregg olsen books
  • bengals job fair
  • dis poem
  • bengals 09 record
  • search 4
  • timbaland
  • freida pinto can't act
  • hp support hard drive replacement
  • login
  • dis x
  • marathon
  • dis pater
  • search chuck norris
  • nubian
  • vince young uncle rico
  • connecticut transit
  • chicago bears 4th phase
  • bea spells a lot
  • tea party obama
  • di's hallmark
  • battleship aurora
  • albuterol
  • c span shelby foote
  • chad ochocinco to patriots
  • seized
  • search xml file
  • vince young football camp
  • vince young released
  • chad ochocinco wedding date
  • tea party lies
  • bea 2011 map
  • connecticut renaissance faire
  • zara phillips and the queen
  • zara phillips dating
  • fond
  • lavigne
  • ronda
  • chad ochocinco free agent
  • vince young rumors
  • cspan presidents
  • spares
  • search operatorssearch people