Skip to content

Instantly share code, notes, and snippets.

@trnila
Created September 9, 2017 16:48
Show Gist options
  • Save trnila/ae5b43341b75a08cbbea7eceb043fbef to your computer and use it in GitHub Desktop.
Save trnila/ae5b43341b75a08cbbea7eceb043fbef to your computer and use it in GitHub Desktop.
#!/bin/sh
container=$1
pass=$2
if [ -z "$container" ] || [ -z "$pass" ]; then
echo "usage: $0 container newRootPassword"
exit
fi
mysql_image=$(docker inspect $container --format "{{ .Config.Image }}")
mount=$(docker inspect $container --format "{{ .Mounts }}" | grep /var/lib/mysql | awk '{print $2}')
docker stop $container
docker run -d --rm --name mysql-recover -v $mount:/var/lib/mysql $mysql_image --skip-grant-tables
sleep 5
docker exec -it mysql-recover mysql -u root -e "FLUSH PRIVILEGES;ALTER USER 'root'@'localhost' IDENTIFIED BY '$pass';"
docker stop mysql-recover
docker start $container
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment