├── .env ├── .gitignore ├── README.md ├── create_network.sh ├── docker-compose.yaml ├── ks1.yaml ├── start.sh └── wait-for-it.sh /.env: -------------------------------------------------------------------------------- 1 | MYSQL1_SAVE_PATH=mysql1 2 | MYSQL2_SAVE_PATH=mysql2 3 | ROOT_PASSWORD=123456 4 | DATABASE=kingshard 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | mysql1/ 2 | mysql2/ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker-mysql-cluster 2 | use docker and kingshard build mysql cluster 3 | 4 | ### How to use 5 | * sh start.sh 6 | 7 | 8 | ### print 9 | 10 | ``` 11 | sh start.sh 12 | 13 | Creating dockermysqlcluster_mysql1_1 ... 14 | Creating dockermysqlcluster_mysql1_1 ... done 15 | wait-for-it.sh: waiting 15 seconds for 172.17.0.13:3306 16 | wait-for-it.sh: 172.17.0.13:3306 is available after 10 seconds 17 | mysql1 is start success 18 | Creating dockermysqlcluster_mysql2_1 ... 19 | Creating dockermysqlcluster_mysql2_1 ... done 20 | wait-for-it.sh: waiting 15 seconds for 172.17.0.14:3306 21 | wait-for-it.sh: 172.17.0.14:3306 is available after 11 seconds 22 | mysql2 is start success 23 | start kingshard 24 | dockermysqlcluster_mysql1_1 is up-to-date 25 | dockermysqlcluster_mysql2_1 is up-to-date 26 | Creating dockermysqlcluster_kingshard_1 ... 27 | Creating dockermysqlcluster_kingshard_1 ... done 28 | 29 | 30 | docker-compose ps 31 | 32 | Name Command State Ports 33 | --------------------------------------------------------------------------------------------- 34 | dockermysqlcluster_kingshard_1 /kingshard Up 0.0.0.0:9696->9696/tcp 35 | dockermysqlcluster_mysql1_1 docker-entrypoint.sh mysqld Up 3306/tcp 36 | dockermysqlcluster_mysql2_1 docker-entrypoint.sh mysqld Up 3306/tcp 37 | ``` 38 | -------------------------------------------------------------------------------- /create_network.sh: -------------------------------------------------------------------------------- 1 | docker network create -d bridge testwifi 2 | docker network ls 3 | docker inspect testwifi 4 | #http://blog.csdn.net/sisiy2015/article/details/50401973 5 | -------------------------------------------------------------------------------- /docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | networks: 3 | default: 4 | external: 5 | name: testwifi 6 | services: 7 | mysql1: 8 | container_name: "mysql1" 9 | image: mysql:latest 10 | volumes: 11 | - ./${MYSQL1_SAVE_PATH}:/var/lib/mysql 12 | expose: 13 | - "3306" 14 | environment: 15 | - MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD} 16 | - MYSQL_DATABASE=${DATABASE} 17 | restart: always 18 | 19 | mysql2: 20 | container_name: "mysql2" 21 | image: mysql:latest 22 | volumes: 23 | - ./${MYSQL2_SAVE_PATH}:/var/lib/mysql 24 | expose: 25 | - "3306" 26 | environment: 27 | - MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD} 28 | - MYSQL_DATABASE=${DATABASE} 29 | restart: always 30 | 31 | kingshard: 32 | container_name: "kingshard" 33 | image: mysql:latest 34 | image: matjazmav/kingshard 35 | volumes: 36 | - ./ks1.yaml:/etc/ks.yaml 37 | ports: 38 | - "9696:9696" 39 | expose: 40 | - "9696" 41 | links: 42 | - mysql1 43 | - mysql2 44 | restart: always 45 | -------------------------------------------------------------------------------- /ks1.yaml: -------------------------------------------------------------------------------- 1 | # server listen addr 2 | addr : 0.0.0.0:9696 3 | 4 | # server user and password 5 | user : kingshard 6 | password : kingshard 7 | 8 | # if set log_path, the sql log will write into log_path/sql.log,the system log 9 | # will write into log_path/sys.log 10 | #log_path : /Users/flike/log 11 | 12 | # log level[debug|info|warn|error],default error 13 | log_level : info 14 | 15 | # if set log_sql(on|off) off,the sql log will not output 16 | log_sql: on 17 | 18 | # only log the query that take more than slow_log_time ms 19 | #slow_log_time : 100 20 | 21 | # the path of blacklist sql file 22 | # all these sqls in the file will been forbidden by kingshard 23 | #blacklist_sql_file: /Users/flike/blacklist 24 | 25 | # only allow this ip list ip to connect kingshard 26 | #allow_ips: 127.0.0.1 27 | 28 | # the charset of kingshard, if you don't set this item 29 | # the default charset of kingshard is utf8. 30 | #proxy_charset: gbk 31 | 32 | # node is an agenda for real remote mysql server. 33 | nodes : 34 | - 35 | name : mysql1 36 | 37 | # default max conns for mysql server 38 | max_conns_limit : 32 39 | 40 | # all mysql in a node must have the same user and password 41 | user : root 42 | password : 123456 43 | 44 | # master represents a real mysql master server 45 | master : mysql1:3306 46 | 47 | # slave represents a real mysql salve server,and the number after '@' is 48 | # read load weight of this slave. 49 | #slave : 192.168.59.101:3307@2,192.168.59.101:3307@3 50 | down_after_noalive : 32 51 | - 52 | name : mysql2 53 | 54 | # default max conns for mysql server 55 | max_conns_limit : 32 56 | 57 | # all mysql in a node must have the same user and password 58 | user : root 59 | password : 123456 60 | 61 | # master represents a real mysql master server 62 | master : mysql2:3306 63 | 64 | # slave represents a real mysql salve server,and the number after '@' is 65 | # read load weight of this slave. 66 | #slave : 192.168.59.101:3307@2,192.168.59.101:3307@3 67 | down_after_noalive : 32 68 | 69 | # schema defines sharding rules, the db is the sharding table database. 70 | schema : 71 | db : kingshard 72 | nodes: [mysql1,mysql2] 73 | default: mysql1 74 | shard: 75 | - 76 | table: test_shard_hash 77 | key: id 78 | nodes: [mysql1,mysql2] 79 | type: hash 80 | locations: [4,4] 81 | 82 | - 83 | table: test_shard_range 84 | key: id 85 | type: range 86 | nodes: [mysql1,mysql2] 87 | locations: [4,4] 88 | table_row_limit: 10000 89 | -------------------------------------------------------------------------------- /start.sh: -------------------------------------------------------------------------------- 1 | tmpnetworkname="testwifi" 2 | tmpcontainername="mysql1" 3 | docker-compose up -d $tmpcontainername 4 | tmpcontainerName=$(docker-compose ps | grep $tmpcontainername | awk '{print $1}') 5 | tmpnetworkIp=$(docker inspect --format '{{ .NetworkSettings.Networks.'$tmpnetworkname'.IPAddress }}' $tmpcontainerName) 6 | ./wait-for-it.sh $tmpnetworkIp:3306 -- echo "$tmpcontainername is start success" 7 | 8 | tmpcontainername="mysql2" 9 | docker-compose up -d $tmpcontainername 10 | tmpcontainerName=$(docker-compose ps | grep $tmpcontainername | awk '{print $1}') 11 | tmpnetworkIp=$(docker inspect --format '{{ .NetworkSettings.Networks.'$tmpnetworkname'.IPAddress }}' $tmpcontainerName) 12 | ./wait-for-it.sh $tmpnetworkIp:3306 -- echo "$tmpcontainername is start success" 13 | 14 | 15 | 16 | tmpcontainername="kingshard" 17 | echo "start kingshard" 18 | docker-compose up -d $tmpcontainername 19 | -------------------------------------------------------------------------------- /wait-for-it.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Use this script to test if a given TCP host/port are available 3 | 4 | cmdname=$(basename $0) 5 | 6 | echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } 7 | 8 | usage() 9 | { 10 | cat << USAGE >&2 11 | Usage: 12 | $cmdname host:port [-s] [-t timeout] [-- command args] 13 | -h HOST | --host=HOST Host or IP under test 14 | -p PORT | --port=PORT TCP port under test 15 | Alternatively, you specify the host and port as host:port 16 | -s | --strict Only execute subcommand if the test succeeds 17 | -q | --quiet Don't output any status messages 18 | -t TIMEOUT | --timeout=TIMEOUT 19 | Timeout in seconds, zero for no timeout 20 | -- COMMAND ARGS Execute command with args after the test finishes 21 | USAGE 22 | exit 1 23 | } 24 | wait_for() 25 | { 26 | if [[ $TIMEOUT -gt 0 ]]; then 27 | echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT" 28 | else 29 | echoerr "$cmdname: waiting for $HOST:$PORT without a timeout" 30 | fi 31 | start_ts=$(date +%s) 32 | while : 33 | do 34 | (echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1 35 | result=$? 36 | if [[ $result -eq 0 ]]; then 37 | end_ts=$(date +%s) 38 | echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds" 39 | break 40 | fi 41 | sleep 1 42 | done 43 | return $result 44 | } 45 | wait_for_wrapper() 46 | { 47 | # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 48 | if [[ $QUIET -eq 1 ]]; then 49 | timeout $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & 50 | else 51 | timeout $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & 52 | fi 53 | PID=$! 54 | trap "kill -INT -$PID" INT 55 | wait $PID 56 | RESULT=$? 57 | if [[ $RESULT -ne 0 ]]; then 58 | echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT" 59 | fi 60 | return $RESULT 61 | } 62 | # process arguments 63 | while [[ $# -gt 0 ]] 64 | do 65 | case "$1" in 66 | *:* ) 67 | hostport=(${1//:/ }) 68 | HOST=${hostport[0]} 69 | PORT=${hostport[1]} 70 | shift 1 71 | ;; 72 | --child) 73 | CHILD=1 74 | shift 1 75 | ;; 76 | -q | --quiet) 77 | QUIET=1 78 | shift 1 79 | ;; 80 | -s | --strict) 81 | STRICT=1 82 | shift 1 83 | ;; 84 | -h) 85 | HOST="$2" 86 | if [[ $HOST == "" ]]; then break; fi 87 | shift 2 88 | ;; 89 | --host=*) 90 | HOST="${1#*=}" 91 | shift 1 92 | ;; 93 | -p) 94 | PORT="$2" 95 | if [[ $PORT == "" ]]; then break; fi 96 | shift 2 97 | ;; 98 | --port=*) 99 | PORT="${1#*=}" 100 | shift 1 101 | ;; 102 | -t) 103 | TIMEOUT="$2" 104 | if [[ $TIMEOUT == "" ]]; then break; fi 105 | shift 2 106 | ;; 107 | --timeout=*) 108 | TIMEOUT="${1#*=}" 109 | shift 1 110 | ;; 111 | --) 112 | shift 113 | CLI="$@" 114 | break 115 | ;; 116 | --help) 117 | usage 118 | ;; 119 | *) 120 | echoerr "Unknown argument: $1" 121 | usage 122 | ;; 123 | esac 124 | done 125 | if [[ "$HOST" == "" || "$PORT" == "" ]]; then 126 | echoerr "Error: you need to provide a host and port to test." 127 | usage 128 | fi 129 | TIMEOUT=${TIMEOUT:-15} 130 | STRICT=${STRICT:-0} 131 | CHILD=${CHILD:-0} 132 | QUIET=${QUIET:-0} 133 | if [[ $CHILD -gt 0 ]]; then 134 | wait_for 135 | RESULT=$? 136 | exit $RESULT 137 | else 138 | if [[ $TIMEOUT -gt 0 ]]; then 139 | wait_for_wrapper 140 | RESULT=$? 141 | else 142 | wait_for 143 | RESULT=$? 144 | fi 145 | fi 146 | if [[ $CLI != "" ]]; then 147 | if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then 148 | echoerr "$cmdname: strict mode, refusing to execute subprocess" 149 | exit $RESULT 150 | fi 151 | exec $CLI 152 | else 153 | exit $RESULT 154 | fi 155 | --------------------------------------------------------------------------------