@@ -54,14 +54,18 @@ _check_config() {
5454 fi
5555}
5656
57- _datadir () {
58- " $@ " --verbose --help --log-bin-index=" $( mktemp -u) " 2> /dev/null | awk ' $1 == "datadir" { print $2; exit }'
57+ # Fetch value from server config
58+ # We use mysqld --verbose --help instead of my_print_defaults because the
59+ # latter only show values present in config files, and not server defaults
60+ _get_config () {
61+ local conf=" $1 " ; shift
62+ " $@ " --verbose --help --log-bin-index=" $( mktemp -u) " 2> /dev/null | awk ' $1 == "' " $conf " ' " { print $2; exit }'
5963}
6064
6165# allow the container to be started with `--user`
6266if [ " $1 " = ' mysqld' -a -z " $wantHelp " -a " $( id -u) " = ' 0' ]; then
6367 _check_config " $@ "
64- DATADIR=" $( _datadir " $@ " ) "
68+ DATADIR=" $( _get_config ' datadir ' " $@ " ) "
6569 mkdir -p " $DATADIR "
6670 chown -R mysql:mysql " $DATADIR "
6771 exec gosu mysql " $BASH_SOURCE " " $@ "
@@ -71,7 +75,7 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
7175 # still need to check config, container may have started with --user
7276 _check_config " $@ "
7377 # Get config
74- DATADIR=" $( _datadir " $@ " ) "
78+ DATADIR=" $( _get_config ' datadir ' " $@ " ) "
7579
7680 if [ ! -d " $DATADIR /mysql" ]; then
7781 file_env ' MYSQL_ROOT_PASSWORD'
@@ -87,10 +91,11 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
8791 mysql_install_db --datadir=" $DATADIR " --rpm
8892 echo ' Database initialized'
8993
90- " $@ " --skip-networking --socket=/var/run/mysqld/mysqld.sock &
94+ SOCKET=" $( _get_config ' socket' " $@ " ) "
95+ " $@ " --skip-networking --socket=" ${SOCKET} " &
9196 pid=" $! "
9297
93- mysql=( mysql --protocol=socket -uroot -hlocalhost --socket=/var/run/mysqld/mysqld.sock )
98+ mysql=( mysql --protocol=socket -uroot -hlocalhost --socket=" ${SOCKET} " )
9499
95100 for i in {30..0}; do
96101 if echo ' SELECT 1' | " ${mysql[@]} " & > /dev/null; then
@@ -113,14 +118,28 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
113118 export MYSQL_ROOT_PASSWORD=" $( pwgen -1 32) "
114119 echo " GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD "
115120 fi
121+
122+ rootCreate=
123+ # default root to listen for connections from anywhere
124+ file_env ' MYSQL_ROOT_HOST' ' %'
125+ if [ ! -z " $MYSQL_ROOT_HOST " -a " $MYSQL_ROOT_HOST " != ' localhost' ]; then
126+ # no, we don't care if read finds a terminating character in this heredoc
127+ # https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151
128+ read -r -d ' ' rootCreate << -EOSQL || true
129+ CREATE USER 'root'@'${MYSQL_ROOT_HOST} ' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD} ' ;
130+ GRANT ALL ON *.* TO 'root'@'${MYSQL_ROOT_HOST} ' WITH GRANT OPTION ;
131+ EOSQL
132+ fi
133+
116134 " ${mysql[@]} " << -EOSQL
117135 -- What's done in this file shouldn't be replicated
118136 -- or products like mysql-fabric won't work
119137 SET @@SESSION.SQL_LOG_BIN=0;
120138
121- DELETE FROM mysql.user ;
122- CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD} ' ;
123- GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
139+ DELETE FROM mysql.user WHERE user NOT IN ('mysql.sys', 'mysqlxsys', 'root') OR host NOT IN ('localhost') ;
140+ SET PASSWORD FOR 'root'@'localhost'=PASSWORD('${MYSQL_ROOT_PASSWORD} ') ;
141+ GRANT ALL ON *.* TO 'root'@'localhost' WITH GRANT OPTION ;
142+ ${rootCreate}
124143 DROP DATABASE IF EXISTS test ;
125144 FLUSH PRIVILEGES ;
126145 EOSQL
0 commit comments