Skip to content

Commit 3d3b0c2

Browse files
committed
add master db container
1 parent fc7c147 commit 3d3b0c2

File tree

7 files changed

+171
-48
lines changed

7 files changed

+171
-48
lines changed

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,34 @@ backend as well as replication slaves, backup and deployment scripts.
1111

1212
This repository assumes a regular linux distro with the following programs accessible on the cli:
1313
- docker
14-
- mysql
14+
- mysql (+ mysqldump)
1515

1616
## Usage
1717

1818
TBD. The general idea would be to have frontend.sh expose all functionnalities although
1919
sub-projects should be as disconnected as possible.
2020

21-
For now, one can create a slave instance like this:
2221

23-
`./frontend.sh slave setup # Creates and starts a replication slave`
22+
### Replication slave
23+
24+
Set up a replication slave like this:
25+
26+
`sudo ./frontend.sh slave setup`
2427

2528
And remove it with
29+
2630
`./frontend.sh slave remove`
2731

28-
The container can be started/stopped using the start and stop commands
29-
```sh
30-
./frontend.sh slave stop
31-
./frontend.sh slave start
32-
```
32+
Additionnal commands available: `stop`, `start`
33+
34+
(Notice: you must take care of `start`ing the instance after a reboot yourself)
35+
36+
37+
### Master database
38+
39+
Master DB is deployed like this:
40+
41+
`sudo ./frontend.sh master setup -b backup_db.sql` where `backup_db.sql` is a sql
42+
file containing a backup of the production data.
43+
44+
Additionnal commands: `stop`, `start`, `remove`

db/master/conf/master_config.cnf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[mysqld]
2+
server-id = 1
3+
expire_logs_days = 10
4+
binlog_do_db = master
5+
log_bin = /var/lib/mysql/mysql-bin.log

db/master/master.sh

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/sh -e
2+
3+
. ../utils.sh
4+
5+
check_env() {
6+
if [ -z "$MASTER_ROOT_PASSWORD" ] || [ -z "$MASTER_DB_PASSWORD" ]; then
7+
echo "An environment variable is missing. Run --help for usage info." 1>&2
8+
return 1
9+
fi
10+
if [ -z "$MASTER_EXPORTED_PORT" ]; then
11+
export MASTER_EXPORTED_PORT=3306
12+
fi
13+
if [ -z "$MASTER_DB_CONTAINER_NAME" ]; then
14+
export MASTER_DB_CONTAINER_NAME="courseadvisor-master"
15+
fi
16+
}
17+
18+
setup() {
19+
# Create mysql docker image
20+
docker run --name "$MASTER_DB_CONTAINER_NAME" -p $MASTER_EXPORTED_PORT:3306 -e "MYSQL_ROOT_PASSWORD=$MASTER_ROOT_PASSWORD" \
21+
-e MYSQL_DATABASE=master -e MYSQL_USER=master -e "MYSQL_PASSWORD=$MASTER_DB_PASSWORD" -v `pwd`/conf:/etc/mysql/conf.d -d mysql:5.5
22+
echo "Waiting for mysql deamon to start"
23+
wait_for_mysql $MASTER_EXPORTED_PORT "$MASTER_ROOT_PASSWORD" 1>/dev/null
24+
# Creating slave user
25+
mysql -P$MASTER_EXPORTED_PORT -uroot "-p$MASTER_ROOT_PASSWORD" "-h127.0.0.1" <<EOF
26+
GRANT RELOAD, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO '$SLAVE_REPLICATION_USER'@'%' IDENTIFIED BY '$SLAVE_REPLICATION_PASSWORD';
27+
GRANT SELECT, LOCK TABLES, SHOW VIEW ON \`master\`.* TO '$SLAVE_REPLICATION_USER'@'%';
28+
EOF
29+
echo "Master database is running"
30+
}
31+
32+
stop() {
33+
docker stop "$MASTER_DB_CONTAINER_NAME"
34+
}
35+
36+
remove() {
37+
stop
38+
docker rm "$MASTER_DB_CONTAINER_NAME"
39+
}
40+
41+
start() {
42+
docker start "$MASTER_DB_CONTAINER_NAME"
43+
}
44+
45+
usage() {
46+
cat 1>&2 <<EOF
47+
usage: master (setup [-b <sqlfile>]|remove)
48+
49+
Sets up or removes the master db container. Sensible arguments are passed via env variables:
50+
51+
mandatory:
52+
MASTER_DB_PASSWORD : Password for master user
53+
MASTER_ROOT_PASSWORD : Password for root user
54+
55+
optionnal:
56+
MASTER_EXPORTED_PORT : Which host port to bind to master's SQL (default 3306)
57+
MASTER_DB_CONTAINER_NAME: Docker container name for the master (default courseadvisor-master)
58+
59+
options:
60+
-b <sqlfile> : executes the sql statements in sqlfile using master database
61+
EOF
62+
}
63+
64+
check_env
65+
66+
case "$1" in
67+
"setup")
68+
setup
69+
shift
70+
if [ "$#" -ge 2 ] && [ "$1" = "-b" ]; then
71+
echo "Seeding database with $2"
72+
echo "use master;" | cat - "$2" | mysql -P$MASTER_EXPORTED_PORT -umaster "-p$MASTER_DB_PASSWORD" "-h127.0.0.1"
73+
fi
74+
;;
75+
"remove")
76+
remove
77+
;;
78+
"stop")
79+
stop
80+
;;
81+
"start")
82+
start
83+
;;
84+
*)
85+
usage
86+
;;
87+
esac

db/slave/slave.sh

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,53 @@
11
#!/bin/sh -e
2-
# server-id = 2
3-
# relay-log = /var/log/mysql/mysql-relay-bin.log
4-
# log_bin = /var/log/mysql/mysql-bin.log
5-
# replicate-do-db = courseadvisor
6-
# report-host = hmil.fr
7-
#
8-
# position: 107
2+
3+
. ../utils.sh
94

105
check_env() {
11-
if [ -z "$SLAVE_ROOT_PASSWORD" ] || [ -z "$SLAVE_REPLICATION_PASSWORD" ] || [ -z "$SLAVE_REPLICATION_USER" ]; then
6+
if [ -z "$SLAVE_ROOT_PASSWORD" ] || [ -z "$SLAVE_REPLICATION_PASSWORD" ] || [ -z "$SLAVE_REPLICATION_USER" ] \
7+
|| [ -z "$MASTER_HOST" ]; then
128
echo "An environment variable is missing. Run --help for usage info." 1>&2
139
return 1
1410
fi
1511
if [ -z "$SLAVE_EXPORTED_PORT" ]; then
1612
export SLAVE_EXPORTED_PORT=3300
1713
fi
18-
if [ -z "$SLAVE_CONTAINER_NAME" ]; then
19-
export SLAVE_CONTAINER_NAME="courseadvisor-slave"
14+
if [ -z "$SLAVE_DB_CONTAINER_NAME" ]; then
15+
export SLAVE_DB_CONTAINER_NAME="courseadvisor-slave"
2016
fi
2117
}
2218

23-
wait_for_mysql() {
24-
set +e
25-
failure=1
26-
tries=30
27-
while [ "$failure" -ne 0 ]; do
28-
tries=`expr $tries - 1`
29-
if [ $tries -eq 0 ]; then
30-
echo "Could not connect to mysql server" 1>&2
31-
return 1
32-
fi
33-
sleep 1
34-
echo 'select 1;' | mysql -P$SLAVE_EXPORTED_PORT -h"127.0.0.1" -uroot -p$SLAVE_ROOT_PASSWORD 2>/dev/null
35-
failure=$?
36-
done
37-
return 0
38-
}
39-
4019
setup() {
4120
# prepare configuration with random slave number
4221
server_id=`tr -cd 0-9 </dev/urandom | head -c 6`
4322
server_name=`hostname`
4423
cat slave_config.cnf | sed "s/__SERVER_ID__/$server_id/" | sed "s/__SERVER_NAME__/$server_name/" > conf/slave_config.cnf
4524
# Create mysql docker image
46-
docker run --name ${SLAVE_CONTAINER_NAME} -p ${SLAVE_EXPORTED_PORT}:3306 -e MYSQL_ROOT_PASSWORD=${SLAVE_ROOT_PASSWORD} -e MYSQL_DATABASE=master -v `pwd`/conf:/etc/mysql/conf.d -d mysql:5.5
25+
docker run --name "$SLAVE_DB_CONTAINER_NAME" -p $SLAVE_EXPORTED_PORT:3306 -e MYSQL_ROOT_PASSWORD="$SLAVE_ROOT_PASSWORD" -e MYSQL_DATABASE=master -v `pwd`/conf:/etc/mysql/conf.d -d mysql:5.5
4726
echo "Waiting for mysql deamon to start"
48-
wait_for_mysql 1>/dev/null
27+
wait_for_mysql $SLAVE_EXPORTED_PORT "$SLAVE_ROOT_PASSWORD" 1>/dev/null
4928
echo "Dumping master state (warning: locks master DB)"
5029
# Seeds database with snapshot data, replication log position is set to take over right after this snapshot
51-
mysqldump -P5467 -u${SLAVE_REPLICATION_USER} -h"courseadvisor.ch" -p${SLAVE_REPLICATION_PASSWORD} --master-data -B master |
52-
sed "s/CHANGE MASTER TO/CHANGE MASTER TO MASTER_HOST='courseadvisor.ch',MASTER_PORT=5467,MASTER_USER='${SLAVE_REPLICATION_USER}',MASTER_PASSWORD='${SLAVE_REPLICATION_PASSWORD}',/" |
53-
mysql -P${SLAVE_EXPORTED_PORT} -h"127.0.0.1" -uroot -p${SLAVE_ROOT_PASSWORD}
30+
mysqldump -P$MASTER_EXPORTED_PORT "-u$SLAVE_REPLICATION_USER" "-h$MASTER_HOST" "-p$SLAVE_REPLICATION_PASSWORD" --master-data -B master |
31+
sed "s/CHANGE MASTER TO /CHANGE MASTER TO MASTER_HOST='$MASTER_HOST',MASTER_PORT=$MASTER_EXPORTED_PORT,MASTER_USER='$SLAVE_REPLICATION_USER',MASTER_PASSWORD='$SLAVE_REPLICATION_PASSWORD',/" |
32+
mysql -P$SLAVE_EXPORTED_PORT -h"127.0.0.1" -uroot "-p$SLAVE_ROOT_PASSWORD"
5433

5534
echo "Starting slave replication"
5635
# Sets up remote master
57-
echo "START SLAVE;" | mysql -P${SLAVE_EXPORTED_PORT} -h"127.0.0.1" -uroot -p${SLAVE_ROOT_PASSWORD}
36+
echo "START SLAVE;" | mysql -P$SLAVE_EXPORTED_PORT "-h127.0.0.1" -uroot "-p$SLAVE_ROOT_PASSWORD"
5837
echo "Slave instance is running"
5938
}
6039

6140
stop() {
62-
docker stop $SLAVE_CONTAINER_NAME
41+
docker stop "$SLAVE_DB_CONTAINER_NAME"
6342
}
6443

6544
remove() {
6645
stop
67-
docker rm $SLAVE_CONTAINER_NAME
46+
docker rm "$SLAVE_DB_CONTAINER_NAME"
6847
}
6948

7049
start() {
71-
docker start $SLAVE_CONTAINER_NAME
50+
docker start "$SLAVE_DB_CONTAINER_NAME"
7251
}
7352

7453
usage() {
@@ -81,10 +60,11 @@ mandatory:
8160
SLAVE_REPLICATION_USER : Replication user on master host
8261
SLAVE_REPLICATION_PASSWORD : Replication user password on master host
8362
SLAVE_ROOT_PASSWORD : Password for root user on local slave instance
63+
MASTER_HOST : Host of the master server
8464
8565
optionnal:
8666
SLAVE_EXPORTED_PORT : Which host port to bind to slave's SQL (default 3300)
87-
SLAVE_CONTAINER_NAME: Docker container name for the slave (default courseadvisor-slave)
67+
SLAVE_DB_CONTAINER_NAME: Docker container name for the slave (default courseadvisor-slave)
8868
EOF
8969
}
9070

db/slave/slave_config.cnf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
[mysqld]
22
server-id = __SERVER_ID__
3-
#relay-log = /var/log/mysql/mysql-relay-bin.log
4-
#log_bin = /var/log/mysql/mysql-bin.log
3+
relay-log = /var/lib/mysql/mysql-relay-bin.log
54
replicate-do-db = master
65
report_host = __SERVER_NAME__

db/utils.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
wait_for_mysql() {
2+
set +e
3+
if [ $# -ne 2 ]; then
4+
echo "usage: wait_for_mysql <port> <root_pwd>" 1>&2
5+
return 1
6+
fi
7+
port="$1"
8+
root_pwd="$2"
9+
failure=1
10+
tries=30
11+
while [ "$failure" -ne 0 ]; do
12+
tries=`expr "$tries" - 1`
13+
if [ "$tries" -eq 0 ]; then
14+
echo "Could not connect to mysql server" 1>&2
15+
return 1
16+
fi
17+
sleep 1
18+
echo 'select 1;' | mysql -P$port "-h127.0.0.1" -uroot "-p$root_pwd" 2>/dev/null
19+
failure=$?
20+
done
21+
return 0
22+
}

frontend.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ For command-specific usage instructions, run frontend.sh command --help
1212
EOF
1313
}
1414

15+
process_args() {
16+
# Poor man's path expansion in argument
17+
while [ $# -gt 0 ]; do
18+
if [ -e "$1" ]; then
19+
readlink -f "$1"
20+
else
21+
echo "$1"
22+
fi
23+
shift
24+
done
25+
}
26+
1527

1628
if [ $# -lt 2 ]; then
1729
usage
@@ -28,14 +40,20 @@ fi
2840
prog="$1"
2941
shift
3042

43+
args=`process_args $@`
44+
3145
case "$prog" in
3246
help|--help|-h)
3347
usage
3448
exit 0
3549
;;
3650
slave)
3751
cd db/slave
38-
./slave.sh $@
52+
./slave.sh $args
53+
;;
54+
master)
55+
cd db/master
56+
./master.sh $args
3957
;;
4058
*)
4159
usage

0 commit comments

Comments
 (0)