MySQL installation on User Mode Linux


For more than one year I am using MySQL Sandbox ( http://mysqlsandbox.net/ ) to test multiple MySQL instances on a single machine, and I find it absolutely the best tool available to manage various MySQL setups within seconds.

Lately, for some testing, I needed an environment with multiple machines with a single MySQL instance each.

For this purpose I decide to use virtual machines created with User Mode Linux ( http://user-mode-linux.sourceforge.net/ ) as virtualization product.

The virtual machine will run Debian Lenny, as the host OS.

Here the step-by-step procedure:
  • prepare the host OS;
  • create a guest OS;
  • install MySQL in the guest OS;
  • launch the guest OS.


Prepare the host OS requires only the installation of two packages:
shell# apt-get install user-mode-linux uml-utilities

Additionally, another package is required to install the guest OS:
shell# apt-get install debootstrap

The creation of the guest OS and the installation of MySQL are two tasks that require multiple steps, and to simply I created the follows script:

#!/bin/sh
INSTANCE_NAME=my-db1
FS_SIZE=1024  # filesystem size in MB
SWAP_SIZE=128  # swap size in MB
MOUNT_POINT="/uml/vegaicm/uml"
APT_REPOSITORY="file:///media/cdrom0"
NETWORK="192.168.1"   # the first 3 octets of the IP
IP=151                # the last octet of the IP


# MySQL binary name
MYSQL_BINARY_PATH="/home/vegaicm/mysql/mysql-5.1.44-linux-i686-glibc23.tar.gz"
[ -f $MYSQL_BINARY_PATH ] || { echo "MySQL binaries missing" ; exit 1; }

MYSQL_CUSTOM_CONFIG=""

# define the filename of filesystem file and swap file
FS_FILE=$INSTANCE_NAME-UML.fs
SWAP_FILE=$INSTANCE_NAME-UML.swap




CURRENT_PWD=$PWD


echo "===== create and format the root filesystem ====="
dd if=/dev/zero of=$FS_FILE bs=1M count=0 seek=$FS_SIZE
mkfs.ext2 -F $FS_FILE

echo "===== create and format the swap disk ====="
dd if=/dev/zero of=$SWAP_FILE bs=1M count=$SWAP_SIZE
mkswap $SWAP_FILE


echo "===== mount the root filesystem ====="
mkdir -p $MOUNT_POINT
mount -o loop $FS_FILE $MOUNT_POINT || { echo "mount failed" ; exit 1; }



echo "===== install Debian in the root filesystem ====="
debootstrap --include=openssh-server,udev lenny $MOUNT_POINT $APT_REPOSITORY || { echo "debootstrap failed" ; exit 1; }



echo "===== create system files ====="
# create /etc/network/interfaces
(
cat << EOF
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
        address $NETWORK.$IP
        netmask 255.255.255.0
        broadcast $NETWORK.255
        gateway $NETWORK.1

EOF
) > $MOUNT_POINT/etc/network/interfaces



# define the hostname
echo "$INSTANCE_NAME" > $MOUNT_POINT/etc/hostname




# create /etc/hosts
(
cat << EOF
127.0.0.1       localhost
$NETWORK.$IP    $INSTANCE_NAME
EOF
) > $MOUNT_POINT/etc/hosts


# create /etc/fstab
(
cat << EOF
/dev/ubda   /       ext2    defaults    0   1
/dev/ubdb   none    swap    sw          0   0
none /proc proc defaults 0 0
none /dev/shm tmpfs defaults 0 0
hostfs /lib/modules hostfs /usr/lib/uml/modules 0 0
EOF
) > $MOUNT_POINT/etc/fstab



echo "===== copy UML modules ====="
cp -Rp /usr/lib/uml/modules/* $MOUNT_POINT/lib/modules/

echo "===== change root password ====="
# set the root password as "root"
mv $MOUNT_POINT/etc/shadow $MOUNT_POINT/etc/shadow.old
cat $MOUNT_POINT/etc/shadow.old | sed -e 's/root::/root:$1$xbu5z7dX$PFJKVLMnIjOSoQHP4MkYJ\/:/' > $MOUNT_POINT/etc/shadow
rm $MOUNT_POINT/etc/shadow.old
rm $MOUNT_POINT/var/cache/apt/archives/*deb


echo "===== install MySQL ====="
# install MySQL from tarball
cd $MOUNT_POINT/usr/local
tar -zxf $MYSQL_BINARY_PATH
ln -s `basename $MYSQL_BINARY_PATH .tar.gz` mysql
chroot $MOUNT_POINT sh -c "groupadd mysql"
chroot $MOUNT_POINT sh -c "useradd -g mysql mysql"
echo "INSERT INTO user VALUES ('$NETWORK.%','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0);" >> $MOUNT_POINT/usr/local/mysql/share/mysql_system_tables_data.sql
chroot $MOUNT_POINT sh -c "cd /usr/local/mysql ; ./scripts/mysql_install_db --user=mysql"
chroot $MOUNT_POINT sh -c "chown -R root:root /usr/local/`basename $MYSQL_BINARY_PATH .tar.gz`"
chroot $MOUNT_POINT sh -c "chown -R mysql:mysql /usr/local/mysql/data"

echo "===== create MySQL startup file ====="
cp $MOUNT_POINT/usr/local/mysql/support-files/mysql.server $MOUNT_POINT/etc/init.d/mysql
cd $MOUNT_POINT/etc/rc2.d/
ln -s ../init.d/mysql S55mysql
cd $MOUNT_POINT/etc/rc0.d/
ln -s ../init.d/mysql K55mysql
cd $MOUNT_POINT/etc/rc1.d/
ln -s ../init.d/mysql K55mysql
cd $MOUNT_POINT/etc/rc6.d/
ln -s ../init.d/mysql K55mysql

echo "===== copy MySQL config file ====="
cp $MOUNT_POINT/usr/local/mysql/support-files/my-medium.cnf $MOUNT_POINT/etc/my.cnf
[ -f "$MYSQL_CUSTOM_FILE" ] && cp $MYSQL_CUSTOM_FILE $MOUNT_POINT/etc/my.cnf


cd $CURRENT_PWD

echo "===== umount root filesystem ====="
umount $MOUNT_POINT

echo "===== DONE ====="



There are few gotchas in this script:
  • all the parameters are at the beginning of the script, they are quite self-explaining and you need to modify only those ;
  • it assumes you are using a Debian CD/DVD as repository. If not, change APT_REPOSITORY to "http://http.us.debian.org/debian" or any other Debian repository ;
  • the guest system root password is "root" ;
  • MySQL tarball binaries need to be downloaded before launching the script ;
  • it is possible to use a customized my.cnf ;
  • it automatically create a MySQL user root that can access without password from the local network .

After the guest OS is created, you just need to run "linux" (the UML binary that launch the virtual machine) with its parameters:

linux root=/dev/ubda ubd0=$PWD/my-db1-UML.fs ubd1=$PWD/my-db1-UML.swap mem=256M eth0=tuntap,,,192.168.1.150 con=pty umid=my-db1-UML >&2 2>/dev/null &


Now you can both ssh to the guest OS or connect to its mysqld instance.

Connect through ssh:
shell$ ssh root@192.168.1.151
root@192.168.1.151's password:
Linux my-db1 2.6.32 #2 Sat Jan 9 04:37:12 UTC 2010 i686

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
my-db1:~#


Connect to mysqld:
shell$ mysql -h 192.168.1.151 -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.44-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>



To switch off the guest OS you can simple execute the poweroff command:
shell$ ssh root@192.168.1.151 poweroff
root@192.168.1.151's password:
shell$

No comments:

Post a Comment