Instances MySQL “jetables”

 

Quand on veut faire des tests sur un service, il est intéressant d'avoir des procédures d'installation rapide pour pouvoir faire des tests, tout casser, tout réinstaller et recommencer. On peut se créer des machines virtuelles VirtualBox (ou autres systèmes de virtualisation) et redéployer sans cesse des copies.

Cependant pour MySQL il existe une solution encore plus simple : MySQL Sandbox qui permet de déployer des instances multiples de MySQL sur un serveur.

Installation

Récupération des sources (Sandbox et différentes versions de MySQL) :

# cd /usr/src
# wget http://launchpad.net/mysql-sandbox/mysql-sandbox-3/mysql-sandbox-3/+download/MySQL-Sandbox-
3.0.17.tar.gz
# wget ftp://mirrors.ircam.fr/pub/mysql/Downloads/MySQL-5.0/mysql-5.0.89-linux-x86_64-glibc23.tar.gz
# wget ftp://mirrors.ircam.fr/pub/mysql/Downloads/MySQL-5.1/mysql-5.1.53-linux-x86_64-glibc23.tar.gz
# wget ftp://mirrors.ircam.fr/pub/mysql/Downloads/MySQL-5.5/mysql-5.5.8-linux2.6-x86_64.tar.gz

Installation SandBox :

# apt-get install build-essential
# tar xvzf MySQL-Sandbox-3.0.17.tar.gz
# cd MySQL-Sandbox
# perl Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for MySQL::Sandbox
# make
# make install

 

Création d'une instance MySQL

# make_sandbox /usr/src/mysql-5.0.89-linux-x86_64-glibc23.tar.gz
MySQL Sandbox should not run as root

If you know what you are doing and want to
run as root nonetheless, please set the environment
variable 'SANDBOX_AS_ROOT' to a nonzero value

# export SANDBOX_AS_ROOT='1'
unpacking /usr/src/mysql-5.0.89-linux-x86_64-glibc23.tar.gz
Executing low_level_make_sandbox --basedir=/usr/src/5.0.89 
        --sandbox_directory=msb_5_0_89 
        --install_version=5.0 
        --sandbox_port=5089 
        --no_ver_after_name 
        --my_clause=log-error=msandbox.err
    The MySQL Sandbox,  version 3.0.17
    (C) 2006-2010 Giuseppe Maxia
installing with the following parameters:
upper_directory                = /root/sandboxes
sandbox_directory              = msb_5_0_89
sandbox_port                   = 5089
check_port                     =
no_check_port                  =
datadir_from                   = script
install_version                = 5.0
basedir                        = /usr/src/5.0.89
tmpdir                         =
my_file                        =
operating_system_user          = root
db_user                        = msandbox
remote_access                  = 127.%
ro_user                        = msandbox_ro
rw_user                        = msandbox_rw
repl_user                      = rsandbox
db_password                    = msandbox
repl_password                  = rsandbox
my_clause                      = log-error=msandbox.err
prompt_prefix                  = mysql
prompt_body                    =  [h] {u} (d) >
force                          =
no_ver_after_name              = 1
verbose                        =
load_grants                    = 1
no_load_grants                 =
no_run                         =
no_show                        =
do you agree? ([Y],n) y
loading grants
. sandbox server started
Your sandbox server was installed in $HOME/sandboxes/msb_5_0_89

On se retrouve donc avec un nouveau serveur mysql :

# netstat -tpnl | grep mysql
tcp        0      0 0.0.0.0:5089            0.0.0.0:*               LISTEN      3498/mysqld

 

Pour y accéder

# /root/sandboxes/msb_5_0_89/use

Pour stopper l'instance

# /root/sandboxes/msb_5_0_89/stop

Pour la démarrer

# /root/sandboxes/msb_5_0_89/start

Pour la configurer :

# vi /root/sandboxes/msb_5_0_89/my.sandbox.cnf

Les data

# ls /root/sandboxes/msb_5_0_89/data/

 

Installer une seconde instance

# make_sandbox /usr/src/mysql-5.1.53-linux-x86_64-glibc23.tar.gz

# netstat -tpnl | grep mysql
tcp        0      0 0.0.0.0:5153            0.0.0.0:*               LISTEN      3743/mysqld
tcp        0      0 0.0.0.0:5089            0.0.0.0:*               LISTEN      3584/mysqld

 

Importer un fichier SQL dans une instance

# cd /root/sandboxes/msb_5_0_89/
# ./use -e 'create database world character set utf8;'
# ./use world < tmp/world.sql

Supprimer une instance

# cd /root/sandboxes/msb_5_0_89/
# ./stop
# cd ..
# rm -rf msb_5_0_89/

 

Mettre en place une architecture de replication maitre > esclaves

# make_replication_sandbox /usr/src/mysql-5.5.8-linux2.6-x86_64.tar.gz
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 in $HOME/sandboxes/rsandbox_5_5_8

Ce qui crée une architecture 1 maître / 2 esclaves (node1 et node2)

 

Conclusion

Voici donc une bonne "boîte à outils" pour tester de multiples configurations de serveurs MySQL.

Pour une documentation plus complète :

http://search.cpan.org/~gmax/MySQL-Sandbox-3.0.17/lib/MySQL/Sandbox/Recipes.pm