Keep mysql replication healthy with percona

Install percona

cd /tmp
wget \
&& dpkg -i percona-toolkit_2.2.12_all.deb \
&& rm percona-toolkit_2.2.12_all.deb

If you miss dependencies, try’apt-get -f install’
If the deb file isn’t found, get the newest version at

Main scripts


nohup pt-slave-restart --verbose --sleep=1 --min-sleep=0 --max-sleep=30 --database=YOURDBNAMEHERE --host=localhost --user=root --password=YOURPASSHERE --port=3307 --socket=/var/run/mysqld/mysqld3307.sock >> /var/percona_replicate.out 2>&1


nohup pt-table-sync --execute --databases YOURDBNAMEHERE h=REMOTEHOST,u=REMOTEUSER,p=REMOTEPASS h=localhost,u=root,p=YOURPASSHERE,P=3307,S=/var/run/mysqld/mysqld3307.sock >> /var/percona-sync.out 2>&1

Install helper scripts

echo -e "\
# Argument expects the full command, so process name including parameters\n\
# Get base process name from command\n\
procname=\$(echo \$procnamefull | awk '{print \$1}')\n\
# First fetch by full command, from there fetch by process name\n\
# Exclude the grep command itself, and the bin commands as well\n\
ps aux | grep \"\$procnamefull\" | grep -v \"grep \$procnamefull\" | grep -v \"/bin/bash /usr/bin/.* \$procnamefull\" | awk '{print \$11}' | grep ^\$procname > /dev/null 2>&1\n\
if [ \$? -eq 0 ]; then\n\
  exit 0\n\
  exit 1\n\
" \ > /usr/bin/isprocessrunning
chmod u+x /usr/bin/isprocessrunning

echo -e "\
\$(isprocessrunning \"\$1\")\n\
if [ \$res -eq 0 ]; then\n\
  echo \"Process is running\"\n\
  exit 1\n\
  if [ \$res -gt 1 ]; then\n\
    echo \"fail\"\n\
    exit 2\n\
  echo \"Process not running\"\n\
  sh -c \"\$2\"\n\
  exit 1\n\
" \ > /usr/bin/startprocessifnotrunning
chmod u+x /usr/bin/startprocessifnotrunning


# Make sure percona-replicate is on. This program keeps the replication slave running after errors
*/5 * * * * /usr/bin/startprocessifnotrunning "sh /var/" "sh /var/" &

# Run full re-sync every week
0 1 * * 0 /usr/bin/startprocessifnotrunning "sh /var/" "sh /var/" &

How to install mono 3 on debian or ubuntu

At the time of writing 3.8.0 was the newest version for Mono that I could find. If a newer version of Mono has come out by now, you can probably find it here.

Installation guide

Make might take 30-60 minutes, make-install should be fast

apt-get update
apt-get install -y --force-yes gcc libtool bison pkg-config libglib2.0-dev gettext make bzip2 g++ build-essential

mkdir -p /var/mono-install
cd /var/mono-install

tar xvjf mono-3.*.tar.bz2
cd mono-3.*

./configure --prefix=/opt/mono3
make install

rm /usr/bin/mono
rm /usr/bin/gmcs
rm /usr/bin/mcs
rm /usr/bin/smcs
rm /usr/bin/dmcs
ln -s /opt/mono3/bin/mono /usr/bin/mono
ln -s /opt/mono3/bin/gmcs /usr/bin/gmcs
ln -s /opt/mono3/bin/mcs /usr/bin/mcs
ln -s /opt/mono3/bin/smcs /usr/bin/smcs
ln -s /opt/mono3/bin/dmcs /usr/bin/dmcs

rm -R /var/mono-install

Installing OpenVPN v2.3+ on debian

Version 1


apt-get update
apt-get -y --force-yes install openvpn udev easy-rsa
mkdir -p /etc/openvpn/easy-rsa/2.0
cp -r /usr/share/easy-rsa /etc/openvpn/easy-rsa/2.0
cd /etc/openvpn/easy-rsa/2.0
mv easy-rsa/* /etc/openvpn/easy-rsa/2.0
rmdir easy-rsa
chmod u+x vars
chmod u+x clean-all
chmod u+x build-ca
source ./vars

./build-key-server $KEYSERVERNAME

./build-key client

cp /etc/openvpn/easy-rsa/2.0/keys/ca.crt /etc/openvpn
cp /etc/openvpn/easy-rsa/2.0/keys/ca.key /etc/openvpn
cp /etc/openvpn/easy-rsa/2.0/keys/dh2048.pem /etc/openvpn
cp /etc/openvpn/easy-rsa/2.0/keys/$KEYSERVERNAME.crt /etc/openvpn
cp /etc/openvpn/easy-rsa/2.0/keys/$KEYSERVERNAME.key /etc/openvpn

gunzip -d /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz
cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf /etc/openvpn
vim /etc/openvpn/server.conf
*update the filenames for crt and key*

/etc/init.d/openvpn restart

mkdir -p ~/vpnclient
cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/vpnclient/client.ovpn
cp /etc/openvpn/easy-rsa/2.0/keys/client.key ~/vpnclient
cp /etc/openvpn/easy-rsa/2.0/keys/client.crt ~/vpnclient

Version 2

Different directory for when easy-rsa comes bundled. Doesn’t try to install it seperately. Also sets 2048bit pem because the default seems to be 1024


apt-get update
apt-get -y --force-yes install openvpn udev
cd /usr/share/doc/openvpn/examples/easy-rsa/2.0

vim vars
*change export KEY_SIZE=1024 to export KEY_SIZE=2048*

chmod u+x vars
chmod u+x clean-all
chmod u+x build-ca
source ./vars

./build-key-server $KEYSERVERNAME

./build-key client

cp keys/ca.crt /etc/openvpn
cp keys/ca.key /etc/openvpn
cp keys/dh2048.pem /etc/openvpn
cp keys/$KEYSERVERNAME.crt /etc/openvpn
cp keys/$KEYSERVERNAME.key /etc/openvpn

gunzip -d /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz
cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf /etc/openvpn
vim /etc/openvpn/server.conf
*update the filenames for crt and key*
*change dh dh1024.pem to dh dh2048.pem*

/etc/init.d/openvpn restart

mkdir -p ~/vpnclient
cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/vpnclient/client.ovpn
cp keys/ca.crt ~/vpnclient
cp keys/client.crt ~/vpnclient
cp keys/client.key ~/vpnclient

If you want to use the internet through the VPN, make the following adjustments:

vim /etc/openvpn/server.conf
*uncomment push "redirect-gateway def1 bypass-dhcp"*
*add push "dhcp-option DNS"*

vim /etc/sysctl.conf
*uncomment net.ipv4.ip_forward=1*
sysctl -p

*run this, and also run it on server boot*
iptables -t nat -A POSTROUTING -j SNAT -s --to-source PUBLIC_SERVER_IP_HERE

Install VMWare on Ubuntu 14 with kernel 3.13.0

Make sure you’re installing the latest vmware version, because even slightly older versions probably don’t support 3.13.0 kernels.

apt-get install build-essential linux-headers-$(uname -r) open-vm-dkms
ln -s /usr/src/linux-headers-$(uname -r)/include/generated/uapi/linux/version.h /usr/src/linux-headers-$(uname -r)/include/linux/version.h
vmware-modconfig --console --install-all

You may have to cd into vmware-modconfig‘s containing directory before executing that last command, but I don’t remember where it is.

Start VMWare Services

/etc/init.d/vmware start

Start VMWare Workstation


Start VMWare player


Install vsftpd version 3 to fix writable root error OOPS 500

Below information is taken from Stack Overflow

Install vsftpd v3 via repo

echo "deb jessie main contrib non-free" >> /etc/apt/sources.list
apt-get update
apt-get install -y --force-yes vsftpd

If the post-setup script fails, delete it so it doesn’t keep bothering you

cd /var/lib/dpkg/info && rm vsftpd.postinst

Allow writable chroot

echo "allow_writeable_chroot=YES" >> /etc/vsftpd.conf
service vsftpd restart

Or via package (no instructions)

Other config I use for first-time setup


Trusty Tahr apache2, php5-fpm and event MPM

apt-get install -y --force-yes apache2 php5-fpm  libapache2-mod-fcgid php5-cgi
sed -i "s/listen =.*/listen =" /etc/php5/fpm/pool.d/www.conf
a2dismod mpm_prefork
a2enmod mpm_event

echo -e "<Directory /var/www>\n\
AddHandler fcgid-script .php\n\
FCGIWrapper /usr/lib/cgi-bin/php5 .php\n\
Options +ExecCGI\n\
# If you have Aliases provide php support for them (Here we provide php support for scripts in /usr/share's subdirectories)\n\
Alias /aptitude /usr/share/doc/aptitude/html/en\n\
Alias /apt /usr/share/doc/apt-doc\n\
<Directory /usr/share>\n\
AddHandler fcgid-script .php\n\
FCGIWrapper /usr/lib/cgi-bin/php5 .php\n\
Options ExecCGI FollowSymlinks Indexes\n\
> /etc/apache2/conf-available/eventphp.conf

a2enconf eventphp
service apache2 restart

Make sure libapache2-mod-php5 is NOT installed

phpinfo file for testing:

echo "<?php phpinfo();" >> /var/www/html/info.php


Show MPM module currently used:

apachectl -V | grep -i mpm

View the available mods

ls /etc/apache2/mods-available/mpm*

Edit the event MPM config:

vim /etc/apache2/mods-available/mpm_event.conf

Configuring phpmyadmin

vim vim /etc/apache2/conf-enabled/phpmyadmin.conf

change Options FollowSymLinks to Options +FollowSymLinks +ExecCGI

Reset mysql password

/etc/init.d/mysql stop
mysqld --skip-grant-tables &
mysql -u root
UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
/etc/init.d/mysql stop
/etc/init.d/mysql start

Linux NTP synchronize time (Europe)

The following commands will install the required software, and then find the best server to synchronize with.

apt-get install -y --force-yes ntp ntpdate
/etc/init.d/ntp stop
ntpdate -u
ntpdate -u
ntpdate -u
/etc/init.d/ntp start
ntpq -p

Example output:

root@debian7:~# ntpdate -u
14 May 11:11:13 ntpdate[37185]: step time server offset 200343.474928 sec
root@debian7:~# ntpdate -u
14 May 11:11:23 ntpdate[37238]: adjust time server offset 0.002920 sec
root@debian7:~# ntpdate -u
14 May 11:11:36 ntpdate[37239]: adjust time server offset -0.001120 sec
root@debian7:~# /etc/init.d/ntp start
[ ok ] Starting NTP server: ntpd.
root@debian7:~# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================    2 u    1   64    1    9.807   -3.671   0.391    3 u    -   64    1   10.671    4.814   0.439    3 u    1   64    1   11.161   -2.069   0.000    2 u    -   64    1   10.258   -2.359   0.000