forked from wengych/mysql-multi-instance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch-create.sh
executable file
·85 lines (66 loc) · 3.12 KB
/
batch-create.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
#!/bin/bash -e
# Usage: $0 $instance_name $port $(/var/lib/)data_dir
set -u
set -e
#echo "Input server ip:"
#SERVER_IP="$1"
#echo "Input new mysql instance name:"
INSTANCE_NAME="$1"
MYSQL_CONF="/etc/"${INSTANCE_NAME}
if [ -e ${MYSQL_CONF} ]; then
echo "mysql instance existed. quit!"
exit 1
fi
#echo "Input mysql listen port:"
MYSQLD_LISTEN_PORT="$2"
#echo "Input mysql data file path(enter as /var/lib/${INSTANCE_NAME}):"
MYSQL_DATA_DIR="$3"
if [ -z ${MYSQL_DATA_DIR} ]; then
MYSQL_DATA_DIR="/var/lib/"${INSTANCE_NAME}
fi
#INSTANCE_NAME="mysql1"
#MYSQLD_LISTEN_PORT=""
#MYSQL_DATA_DIR="/var/lib/"${INSTANCE_NAME}
MYSQLD_SOCK_FILE="/var/run/mysqld/${INSTANCE_NAME}.sock"
MYSQLD_PID_FILE="/var/run/mysqld/${INSTANCE_NAME}.pid"
DEBIAN_CNF="${MYSQL_CONF}/debian.cnf"
DEBIAN_SYS_MAINT_PASS=`perl -e 'print map{("a".."z","A".."Z",0..9)[int(rand(62))]}(1..16)'`
mkdir ${MYSQL_DATA_DIR}
chown -R mysql.mysql ${MYSQL_DATA_DIR}
# Generate mysql config files
cp -R etc/mysql-multi ${MYSQL_CONF}
sed -i "s@[email protected]@g" ${MYSQL_CONF}/my.cnf
sed -i "s@mysql_listen_port@"$MYSQLD_LISTEN_PORT"@g" ${MYSQL_CONF}/my.cnf
sed -i "s@mysqld_sock_file@"$MYSQLD_SOCK_FILE"@g" ${MYSQL_CONF}/my.cnf
sed -i "s@mysqld_pid_file@"$MYSQLD_PID_FILE"@g" ${MYSQL_CONF}/my.cnf
sed -i "s@mysql_data_dir@"$MYSQL_DATA_DIR"@g" ${MYSQL_CONF}/my.cnf
sed -i "s@mysql_instance_name@"$INSTANCE_NAME"@g" ${MYSQL_CONF}/debian-start
echo "# Automatically generated for Debian scripts. DO NOT TOUCH!" > ${DEBIAN_CNF}
echo "[client]" >> ${DEBIAN_CNF}
echo "host = localhost" >> ${DEBIAN_CNF}
echo "user = debian-sys-maint" >> ${DEBIAN_CNF}
echo "password = ${DEBIAN_SYS_MAINT_PASS}" >> ${DEBIAN_CNF}
echo "socket = ${MYSQLD_SOCK_FILE}" >> ${DEBIAN_CNF}
echo "[mysql_upgrade]" >> ${DEBIAN_CNF}
echo "host = localhost" >> ${DEBIAN_CNF}
echo "user = debian-sys-maint" >> ${DEBIAN_CNF}
echo "password = ${DEBIAN_SYS_MAINT_PASS}" >> ${DEBIAN_CNF}
echo "socket = ${MYSQLD_SOCK_FILE}" >> ${DEBIAN_CNF}
echo "basedir = /usr" >> ${DEBIAN_CNF}
mysql_install_db --user=mysql --datadir=${MYSQL_DATA_DIR}
if [ `dirname ${MYSQL_DATA_DIR}` = "/mnt/ramdisk" ]; then
/etc/init.d/${INSTANCE_NAME} stop
cp -R ${MYSQL_DATA_DIR} /root
/etc/init.d/${INSTANCE_NAME} start
fi
# Generate mysql init.d script
cp etc/init.d/mysql-multi /etc/init.d/${INSTANCE_NAME}
sed -i "s@mysql_instance_config_path@"$MYSQL_CONF"@g" /etc/init.d/${INSTANCE_NAME}
sed -i "s@mysql_data_dir@"$MYSQL_DATA_DIR"@g" /etc/init.d/${INSTANCE_NAME}
sed -i "s@mysql_instance_name@"$INSTANCE_NAME"@g" /etc/init.d/${INSTANCE_NAME}
# Update debian-sys-maint user in new database
/etc/init.d/${INSTANCE_NAME} start
mysql -S ${MYSQLD_SOCK_FILE} -e "create user 'debian-sys-maint' identified by '${DEBIAN_SYS_MAINT_PASS}'"
mysql -S ${MYSQLD_SOCK_FILE} -e "grant all privileges on *.* to 'debian-sys-maint'@'%'"
#mysql -S ${MYSQLD_SOCK_FILE} -e "create user 'admin' identified by 'admin'"
#mysql -S ${MYSQLD_SOCK_FILE} -e "grant all privileges on *.* to 'admin'@'%'"