docker-entrypoint.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/bin/bash
  2. # Copyright 2015 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # This script does the following:
  17. #
  18. # 1. Sets up database privileges by building an SQL script
  19. # 2. MySQL is initially started with this script a first time
  20. # 3. Modify my.cnf and cluster.cnf to reflect available nodes to join
  21. #
  22. # if NUM_NODES not passed, default to 3
  23. if [ -z "$NUM_NODES" ]; then
  24. NUM_NODES=3
  25. fi
  26. if [ "${1:0:1}" = '-' ]; then
  27. set -- mysqld "$@"
  28. fi
  29. # if the command passed is 'mysqld' via CMD, then begin processing.
  30. if [ "$1" = 'mysqld' ]; then
  31. # read DATADIR from the MySQL config
  32. DATADIR="$("$@" --verbose --help 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')"
  33. # only check if system tables not created from mysql_install_db and permissions
  34. # set with initial SQL script before proceeding to build SQL script
  35. if [ ! -d "$DATADIR/mysql" ]; then
  36. # fail if user didn't supply a root password
  37. if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" ]; then
  38. echo >&2 'error: database is uninitialized and MYSQL_ROOT_PASSWORD not set'
  39. echo >&2 ' Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?'
  40. exit 1
  41. fi
  42. # mysql_install_db installs system tables
  43. echo 'Running mysql_install_db ...'
  44. mysql_install_db --datadir="$DATADIR"
  45. echo 'Finished mysql_install_db'
  46. # this script will be run once when MySQL first starts to set up
  47. # prior to creating system tables and will ensure proper user permissions
  48. tempSqlFile='/tmp/mysql-first-time.sql'
  49. cat > "$tempSqlFile" <<-EOSQL
  50. DELETE FROM mysql.user ;
  51. CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
  52. GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
  53. EOSQL
  54. if [ "$MYSQL_DATABASE" ]; then
  55. echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" >> "$tempSqlFile"
  56. fi
  57. if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
  58. echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" >> "$tempSqlFile"
  59. if [ "$MYSQL_DATABASE" ]; then
  60. echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" >> "$tempSqlFile"
  61. fi
  62. fi
  63. # Add SST (Single State Transfer) user if Clustering is turned on
  64. if [ -n "$GALERA_CLUSTER" ]; then
  65. # this is the Single State Transfer user (SST, initial dump or xtrabackup user)
  66. WSREP_SST_USER=${WSREP_SST_USER:-"sst"}
  67. if [ -z "$WSREP_SST_PASSWORD" ]; then
  68. echo >&2 'error: Galera cluster is enabled and WSREP_SST_PASSWORD is not set'
  69. echo >&2 ' Did you forget to add -e WSREP_SST__PASSWORD=... ?'
  70. exit 1
  71. fi
  72. # add single state transfer (SST) user privileges
  73. echo "CREATE USER '${WSREP_SST_USER}'@'localhost' IDENTIFIED BY '${WSREP_SST_PASSWORD}';" >> "$tempSqlFile"
  74. echo "GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO '${WSREP_SST_USER}'@'localhost';" >> "$tempSqlFile"
  75. fi
  76. echo 'FLUSH PRIVILEGES ;' >> "$tempSqlFile"
  77. # Add the SQL file to mysqld's command line args
  78. set -- "$@" --init-file="$tempSqlFile"
  79. fi
  80. chown -R mysql:mysql "$DATADIR"
  81. fi
  82. # if cluster is turned on, then proceed to build cluster setting strings
  83. # that will be interpolated into the config files
  84. if [ -n "$GALERA_CLUSTER" ]; then
  85. # this is the Single State Transfer user (SST, initial dump or xtrabackup user)
  86. WSREP_SST_USER=${WSREP_SST_USER:-"sst"}
  87. if [ -z "$WSREP_SST_PASSWORD" ]; then
  88. echo >&2 'error: database is uninitialized and WSREP_SST_PASSWORD not set'
  89. echo >&2 ' Did you forget to add -e WSREP_SST_PASSWORD=xxx ?'
  90. exit 1
  91. fi
  92. # user/password for SST user
  93. sed -i -e "s|^wsrep_sst_auth=sstuser:changethis|wsrep_sst_auth=${WSREP_SST_USER}:${WSREP_SST_PASSWORD}|" /etc/mysql/conf.d/cluster.cnf
  94. # set nodes own address
  95. WSREP_NODE_ADDRESS=`ip addr show | grep -E '^[ ]*inet' | grep -m1 global | awk '{ print $2 }' | sed -e 's/\/.*//'`
  96. if [ -n "$WSREP_NODE_ADDRESS" ]; then
  97. sed -i -e "s|^wsrep_node_address=.*$|wsrep_node_address=${WSREP_NODE_ADDRESS}|" /etc/mysql/conf.d/cluster.cnf
  98. fi
  99. # if the string is not defined or it only is 'gcomm://', this means bootstrap
  100. if [ -z "$WSREP_CLUSTER_ADDRESS" -o "$WSREP_CLUSTER_ADDRESS" == "gcomm://" ]; then
  101. # if empty, set to 'gcomm://'
  102. # NOTE: this list does not imply membership.
  103. # It only means "obtain SST and join from one of these..."
  104. if [ -z "$WSREP_CLUSTER_ADDRESS" ]; then
  105. WSREP_CLUSTER_ADDRESS="gcomm://"
  106. fi
  107. # loop through number of nodes
  108. for NUM in `seq 1 $NUM_NODES`; do
  109. NODE_SERVICE_HOST="PXC_NODE${NUM}_SERVICE_HOST"
  110. # if set
  111. if [ -n "${!NODE_SERVICE_HOST}" ]; then
  112. # if not its own IP, then add it
  113. if [ $(expr "$HOSTNAME" : "pxc-node${NUM}") -eq 0 ]; then
  114. # if not the first bootstrap node add comma
  115. if [ $WSREP_CLUSTER_ADDRESS != "gcomm://" ]; then
  116. WSREP_CLUSTER_ADDRESS="${WSREP_CLUSTER_ADDRESS},"
  117. fi
  118. # append
  119. # if user specifies USE_IP, use that
  120. if [ -n "${USE_IP}" ]; then
  121. WSREP_CLUSTER_ADDRESS="${WSREP_CLUSTER_ADDRESS}"${!NODE_SERVICE_HOST}
  122. # otherwise use DNS
  123. else
  124. WSREP_CLUSTER_ADDRESS="${WSREP_CLUSTER_ADDRESS}pxc-node${NUM}"
  125. fi
  126. fi
  127. fi
  128. done
  129. fi
  130. # WSREP_CLUSTER_ADDRESS is now complete and will be interpolated into the
  131. # cluster address string (wsrep_cluster_address) in the cluster
  132. # configuration file, cluster.cnf
  133. if [ -n "$WSREP_CLUSTER_ADDRESS" -a "$WSREP_CLUSTER_ADDRESS" != "gcomm://" ]; then
  134. sed -i -e "s|^wsrep_cluster_address=gcomm://|wsrep_cluster_address=${WSREP_CLUSTER_ADDRESS}|" /etc/mysql/conf.d/cluster.cnf
  135. fi
  136. fi
  137. # random server ID needed
  138. sed -i -e "s/^server\-id=.*$/server-id=${RANDOM}/" /etc/mysql/my.cnf
  139. # finally, start mysql
  140. exec "$@"