Skip to content

Instantly share code, notes, and snippets.

@ionelmc
Last active August 21, 2017 07:02
Show Gist options
  • Save ionelmc/3931130 to your computer and use it in GitHub Desktop.
Save ionelmc/3931130 to your computer and use it in GitHub Desktop.
Mysql instances (ramdisk, linux)
#!/bin/bash -xe
# source: http://www.ducea.com/2009/01/19/running-multiple-instances-of-mysql-on-the-same-machine/
i=$1
port=$[3306+$i]
#mysqladmin -S /var/run/mysqld/mysqld$i.sock shutdown || true
kill -9 `cat /var/run/mysqld/mysqld$i.pid` || true
umount /var/lib/mysql$i || true
rm -rf /var/lib/mysql$i
mkdir /var/lib/mysql$i
mount -t tmpfs -o size=400M tmpfs /var/lib/mysql$i/
chown -R mysql.mysql /var/lib/mysql$i/
mkdir -p /var/log/mysql$i
chown -R mysql.mysql /var/log/mysql$i
rm -rf /etc/mysql$i
cp -R /etc/mysql/ /etc/mysql$i
cd /etc/mysql$i/
sed -i "s/3306/$port/g" my.cnf
sed -i "s/mysqld.sock/mysqld$i.sock/g" my.cnf
sed -i "s/mysqld.pid/mysqld$i.pid/g" my.cnf
sed -i "s/var\/lib\/mysql/var\/lib\/mysql$i/g" my.cnf
sed -i "s/var\/log\/mysql/var\/log\/mysql$i/g" my.cnf
if [ ! -f /etc/apparmor.d/disable/usr.sbin.mysqld ]; then
touch /etc/apparmor.d/disable/usr.sbin.mysqld
service apparmor reload
fi
mysql_install_db --user=mysql --datadir=/var/lib/mysql$i/
pass=`perl -e 'print map{("a".."z","A".."Z",0..9)[int(rand(62))]}(1..16)'`;
mysqld_safe --defaults-file=/etc/mysql$i/my.cnf&
sleep 3
mysqladmin -S /var/run/mysqld/mysqld$i.sock password $pass
cat <<EOF | mysql -uroot -h127.0.0.1 -P$port -p$pass
USE mysql;
CREATE DATABASE IF NOT EXISTS dbname CHARACTER SET utf8;
GRANT ALL ON dbname.* TO 'username'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment