Skip to content

Instantly share code, notes, and snippets.

@yangadam
Last active October 13, 2020 12:19
Show Gist options
  • Save yangadam/30c2a43a46f619888ea29c0cbcb3e882 to your computer and use it in GitHub Desktop.
Save yangadam/30c2a43a46f619888ea29c0cbcb3e882 to your computer and use it in GitHub Desktop.
#!/bin/sh
function wait_mysql() {
while ! (docker exec -it $1 mysql -pmysql -e "SELECT 1;" > /dev/null)
do
echo "Wait 5s for $1..."
sleep 5
done
}
function mysql_single() {
mkdir -p ~/Data/mysql/single
if [[ `docker ps|grep mysql-single` = "" ]]; then
echo "Starting mysql-single..."
docker run --name mysql-single -p 3306:3306 -v ~/Data/mysql/single:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysql -d mysql
wait_mysql mysql-single
fi
docker run -it --link mysql-single:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'
}
function remove_mysql_single() {
docker stop mysql-single
docker rm mysql-single
}
function mysql_master_slave() {
mkdir -p ~/Data/mysql/master ~/Data/mysql/slave1
if [[ `docker ps|grep mysql-master` = "" ]]; then
echo "Starting mysql-master..."
docker run --name mysql-master -p 3307:3306 -v ~/Data/mysql/master:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysql -d mysql
wait_mysql mysql-master
docker exec mysql-master mysql -pmysql -e "CREATE USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY 'mysql'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; flush privileges;"
docker exec mysql-master sh -c 'printf "[mysqld]\nserver-id=1\nlog-bin=mysql-bin\n" > /etc/mysql/conf.d/master.cnf'
docker restart mysql-master
wait_mysql mysql-master
docker exec mysql-master mysql -pmysql -e "show master status;"
fi
if [[ `docker ps|grep mysql-slave1` = "" ]]; then
echo "Starting mysql-slave..."
docker run --link mysql-master:master --name mysql-slave1 -p 3308:3306 -v ~/Data/mysql/slave1:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysql -d mysql
wait_mysql mysql-slave1
docker exec mysql-slave1 sh -c 'printf "[mysqld]\nserver-id=2\nlog-bin=mysql-bin\n" > /etc/mysql/conf.d/slave.cnf'
docker restart mysql-slave1
wait_mysql mysql-slave1
docker exec mysql-slave1 mysql -pmysql -e "change master to master_host='master', master_user='repl', master_log_file='mysql-bin.000001', master_log_pos=155, master_port=3306, master_password='mysql'; start slave;"
docker exec mysql-slave1 mysql -pmysql -e "show slave status\G"
fi
# docker rm -f mysql-master mysql-slave1; rm -rf ~/Data/mysql/master ~/Data/mysql/slave1
}
case "$1" in
"mysql")
mysql_single
exit 0
;;
"mysqldown")
remove_mysql_single
exit 0
;;
"mysqlmaster")
mysql_master_slave
exit 0
;;
"zk")
mkdir -p ~/Data/zookeeper/data ~/Data/zookeeper/datalog ~/Data/zookeeper/logs
if [[ `docker ps|grep zookeeper-server` = "" ]]; then
docker run --name zookeeper-server -p 2181:2181 -p 2888:2888 -p 3888:3888 -v ~/Data/zookeeper/data:/data -v ~/Data/zookeeper/datalog:/datalog -v ~/Data/zookeeper/logs:/logs --restart always -d zookeeper
fi
docker run -it --rm --link zookeeper-server:zookeeper zookeeper zkCli.sh -server zookeeper
exit 0
;;
"zkdown")
docker stop zookeeper-server
docker rm zookeeper-server
exit 0
;;
"pg")
mkdir -p ~/Data/postgres
if [[ `docker ps|grep postgres-server` = "" ]]; then
docker run --name postgres-server -p 5432:5432 -v ~/Data/postgres:/var/lib/postgresql -e POSTGRES_PASSWORD=postgres -d postgres:alpine
fi
docker run -it --rm --link postgres-server:postgres -e PGPASSWORD=postgres postgres:alpine psql -h postgres -U postgres
exit 0
;;
"pgdown")
docker stop postgres-server
docker rm postgres-server
exit 0
;;
"cass")
mkdir -p ~/Data/cassandra
if [[ `docker ps|grep cassandra-server` = "" ]]; then
docker run --name cassandra-server -p 7000:7000 -p 7001:7001 -p 7199:7199 -p 9042:9042 -p 9160:9160 -v ~/Data/cassandra:/var/lib/cassandra -d cassandra
fi
docker run -it --link cassandra-server:cassandra --rm cassandra cqlsh cassandra
exit 0
;;
"cassdown")
docker stop cassandra-server
docker rm cassandra-server
exit 0
;;
"rd")
mkdir -p ~/Data/redis
if [[ `docker ps|grep redis-server` = "" ]]; then
docker run --name redis-server -p 6379:6379 -v ~/Data/redis:/data -d redis:alpine
fi
docker run -it --link redis-server:redis --rm redis:alpine redis-cli -h redis -p 6379
exit 0
;;
"rddown")
docker stop redis-server
docker rm redis-server
exit 0
;;
"rq")
mkdir -p ~/Data/rabbitmq
if [[ `docker ps|grep rabbitmq-server` = "" ]]; then
docker run --name rabbitmq-server -p 15671:15671 -p 15672:15672 -p 25672:25672 -p 4369:4369 -p 5671:5671 -p 5672:5672 -v ~/Data/rabbitmq:/var/lib/rabbitmq -d rabbitmq:management-alpine
fi
docker exec -it rabbitmq-server bash
exit 0
;;
"rqdown")
docker stop rabbitmq-server
docker rm rabbitmq-server
exit 0
;;
*)
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment