How to run mysql from docker

  devops
# create a place on the host system where to store the mysql volume data
sudo mkdir /opt/mysqldata
sudo chown "$USER":"$USER" /opt/mysqldata

# start the image, use 3307 instead of 3306 in order to avoid conflicts with other mysql instances
# (or use "-p 3306:3306" if you want to connect through default 3306)
#
docker run --name=mysqldb -v /opt/mysqldata:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=your-password-here -p 3307:3306 -d mysql:5.7

# Note: as of 7/30/2019, if you run a newer version of mysql for docker, like :latest or :8.0.15, you might see problems
# with: "Authentication plugin 'caching_sha2_password' cannot be loaded", and you'll need to start the image
# with docker run ... and add this as well: "--default-authentication-plugin=mysql_native_password" ... Good luck.

# to connect from the host OS, you need
sudo apt-get install mysql-client

# You need to be root to mysql connect as user root
# and you need to use the -h to specify the host, otherwise you can get this error:
#
# pi@host:~# mysql -u root -p --port=3307
# Enter password:
# ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
#
sudo mysql -h 127.0.0.1 -u root -p --port 3307

# create your database and user
# example:
create database springsocial;
create user 'springsocial'@'%' identified by 'springsocial';
grant all privileges on springsocial.* to 'springsocial'@'%';


# Connecting from Mysql Workbench:
#
# If you see this error: Authentication plugin 'caching_sha2_password' cannot be loaded
# then log in from $ root at the shell using: mysql -h 127.0.0.1 -u root -p --port 3307
#
# and alter the password for the user, ex:
#
alter user 'springsocial'@'%' IDENTIFIED WITH mysql_native_password BY 'springsocial';
#
# and then connecting with Mysql Workbench should work.