108 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			108 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
| #!/bin/bash
 | |
| 
 | |
| ##################################################
 | |
| # Mount file                                     #
 | |
| # - /etc/my.cnf                                  #
 | |
| # Mount dir                                      #
 | |
| # - LOG_DIR                                      #
 | |
| # - DATA_DIR                                     #
 | |
| # - BINLOG_DIR                                   #
 | |
| ##################################################
 | |
| 
 | |
| set -euo pipefail
 | |
| export LANG=en_US.UTF-8
 | |
| trap Quit EXIT
 | |
| 
 | |
| PIDS=
 | |
| GOT_SIGTERM=
 | |
| LOG_DIR='/var/log/mysql'
 | |
| DATA_DIR='/var/lib/mysql'
 | |
| BINLOG_DIR='/var/lib/mysql-bin'
 | |
| SOCK_FILE='/run/mysqld/mysqld.sock'
 | |
| PID_FILE='/run/mysqld/mysqld.pid'
 | |
| INIT_FLAG=
 | |
| 
 | |
| function Print {
 | |
|     local file=/dev/null
 | |
|     [ '-f' = "$1" ] && file=$2 && shift && shift
 | |
|     date +"[%F %T] $*" | tee -a $file
 | |
| }
 | |
| 
 | |
| function Quit {
 | |
|     Print killing mysqld ...
 | |
|     mysqladmin shutdown || true
 | |
|     while :; do
 | |
|         pkill -f mysqld && Print killing mysqld ... || break
 | |
|         sleep 1
 | |
|     done
 | |
|     Print Container stopped.
 | |
|     test -n "$GOT_SIGTERM"
 | |
| }
 | |
| 
 | |
| function Init {
 | |
|     rm -f ${SOCK_FILE}* ${PID_FILE}
 | |
|     chown -R mysql:mysql $LOG_DIR $BINLOG_DIR $DATA_DIR
 | |
|     if [ ! -d "$DATA_DIR/mysql" ]; then
 | |
|         Print Initing mysql db files ...
 | |
|         mysqld_pre_systemd
 | |
|         INIT_FLAG=1
 | |
|     fi
 | |
| }
 | |
| 
 | |
| function ImportInitSql {
 | |
|     local sql_file= sql_files=
 | |
|     mysql -e "CREATE USER docker@localhost IDENTIFIED BY 'China_19\$(10)!'"
 | |
|     mysql -e "GRANT RELOAD,SHUTDOWN ON *.* TO docker@localhost"
 | |
|     if sql_files="$(ls $LOG_DIR/init_sql/*.sql 2>/dev/null)"; then
 | |
|         Print Importing the sql files ...
 | |
|         for sql_file in $sql_files; do
 | |
|              Print Importing $sql_file ...
 | |
|              mysql < $sql_file
 | |
|         done
 | |
|         Print Imported all sql files successfully.
 | |
|     fi
 | |
| }
 | |
| 
 | |
| function SideCar {
 | |
|     local day= last_day=$(date +%d)
 | |
|     while sleep 8; do
 | |
|         day=$(date +%d) \
 | |
|             && [ "$day" != "$last_day" ] \
 | |
|             && last_day=$day \
 | |
|             && find $LOG_DIR -type f -name "*.log" \
 | |
|                 | xargs -I ^ mv -f ^ ^.$(date +%F -d yesterday) \
 | |
|             && mysqladmin flush-logs
 | |
|     done
 | |
| }
 | |
| 
 | |
| function StartProc {
 | |
|     Print Starting mysql ...
 | |
|     mysqld -u mysql &
 | |
|     PIDS="$PIDS $!"
 | |
|     while sleep 1; do
 | |
|         [ -e $SOCK_FILE ] && break || echo -n .
 | |
|         [ ! -e /proc/$! ] && echo && Print unexpected error! && exit
 | |
|     done
 | |
|     echo
 | |
|     [ -z "$INIT_FLAG" ] || ImportInitSql
 | |
|     SideCar &
 | |
|     PIDS="$PIDS $!"
 | |
|     Print MySQL is ready for connections.
 | |
| }
 | |
| 
 | |
| function Main {
 | |
|     local pid=
 | |
|     Init
 | |
|     StartProc
 | |
|     trap "GOT_SIGTERM=1; Print Got SIGTERM ..." SIGTERM
 | |
|     while [ -z "$GOT_SIGTERM" ] && sleep 2; do
 | |
|         for pid in $PIDS; do
 | |
|             [ ! -e /proc/$pid ] && Print Unexpected error! && exit
 | |
|         done
 | |
|     done
 | |
| }
 | |
| 
 | |
| # Start here
 | |
| Main
 | |
| 
 |