-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsetup_mysql.sh
112 lines (92 loc) · 2.8 KB
/
setup_mysql.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
# Author :- Rahul Patil<http://www.linuxian.com>
# Date :- Fri Dec 13 17:51:43 IST 2013
# Why ?
# Setup MySQL Multiple Instance for Testing Scenario
# Supported Only for CentOS and Ubuntu
# Report issue
# Only root can use this script
[[ $UID == 0 ]] || { echo >&2 "Only root can use this script"; exit 1; }
# set arch
arch="$( [[ "$(uname -m)" =~ 'i686' ]] && echo i386 )"
# set password for mysql root user
root_pas=test123
set_password='mysqladmin -u root password $root_pas'
# set OS Ubuntu or CentOS/RHEL
package_manager=$( [[ -x $(command -v yum) ]] && echo "RHEL Based" || echo "Ubuntu Based")
rhel_installation(){
rpm -qa mysql-server* | grep -qw mysql-server ||
{ yum install -y mysql-server mysql.$arch;
mysqladmin status || /etc/init.d/mysqld restart;
eval $set_password;
echo "MySQL Successfully Installed";
}
}
ubunt_installation(){
export DEBIAN_FRONTEND=noninteractive
dpkg -l | grep -qw mysql-server ||
{
apt-get -q -y install mysql-server;
mysqladmin status || /etc/init.d/mysqld restart;
eval $set_password;
echo "MySQL Successfully Installed";
}
}
install_packages() {
case $package_manager in
RHEL* ) rhel_installation ;;
Ubuntu* ) ubunt_installation ;;
esac
}
ask() {
while true
do
read -p "Do you want to add more Instance (y/n)?" ans
case $ans in
[yY]|[yY][eE][sS]) ans=yes; break ;;
[nN]|[nN][oO]) ans=no; exit 0 ;;
*) echo "Please Press (y/n)" ;;
esac
done
}
setup_new_instance(){
ask
total_instance=$( ls -1d /var/lib/mysql* | wc -l )
(( total_instance++ ))
i=$total_instance
port=$[ 3305 + $i ]
location=/var/lib/mysql${i}
logpath=/var/log/mysql${i}
[[ -d ${location} ]] && { echo "Already Exists"; exit 1; }
mkdir ${location}
mkdir ${logpath}
chown -R mysql. ${location} ${logpath}
cp /etc/init.d/mysql /etc/init.d/mysql${i}
case $package_manager in
RHEL*) cnf=/etc/my${i}.cnf
cp /etc/my.cnf $cnf ;;
Ubuntu*) cp -R /etc/mysql/ /etc/mysql${i}/
cnf=/etc/mysql${i}/my.cnf
appr=/etc/apparmor.d/usr.sbin.mysqld
cp $appr ${appr}${i}
appr=${appr}${i}
int_conf=/etc/init/mysql${i}.conf
cp /etc/init/mysql.conf $int_conf ;;
esac
for config in $cnf $appr $int_conf
do
sed -i "s/3306/$port/g" $config
sed -i "s/mysqld.sock/mysqld$i.sock/g" $config
sed -i "s/mysqld.pid/mysqld$i.pid/g" $config
sed -i "s/var\/lib\/mysql/var\/lib\/mysql$i/g" $config
sed -i "s/var\/log\/mysql/var\/log\/mysql$i/g" $config
sed -i "s/etc\/mysql/etc\/mysql2/g" $config
done
[[ -z $appr ]] || /lib/init/apparmor-profile-load $appr
mysql_install_db --user=mysql --datadir=/var/lib/mysql$i/
}
Main(){
install_packages
setup_new_instance
}
Main