Docker Administration: MySQL Server

Prerequisites:

Configurations:

Create a ubuntu/mysql container:

docker run -dit --name MySQL_DEV --network docker_dev_net --ip 192.168.1.101 --mount source=my_smb_vol,destination=/SharedVolume -p 3306:3306 -p 22:22 -e MYSQL_ROOT_PASSWORD=password ubuntu/mysql

Create users and databases:

# Connect to mysql instance:
mysql -u root -p

# Show databases:
SHOW DATABASES;

# Describe a table:
USE mysql;
DESC user;

# Show all the users:
SELECT user, host FROM mysql.user;

# Create databases (assuming that we'll be creating the databases for some WordPress sites):
CREATE DATABASE my_wordpress_db;

# Create users:
CREATE USER 'wordpress_user'@'192.168.1.103' IDENTIFIED BY 'password';
CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'password';

# Grant privileges to the user:
GRANT ALL ON my_wordpress_db.* TO 'wordpress_user'@'192.168.1.103' WITH GRANT OPTION;
FLUSH PRIVILEGES;

# Verify privileges:
SHOW GRANTS FOR 'wordpress_user'@'192.168.1.103';

Allow remote connection to the MySQL server:

# You will have to modify one of the .cnf files to allow remote connection to your mysql instance, if you list the files under /etc/mysql/, you'll find the following files and directories listed:

ls -al /etc/mysql/

conf.d
my.cnf
my.cnf.fallback

# View the content of my.cnf:
cat /etc/mysql/my.cnf

# On the last two lines of the output, you should see:

# Custom config should go here
!includedir /etc/mysql/conf.d/

# List files under the conf.d directory:
ls -al /etc/mysql/conf.d/

docker.cnf
mysql.cnf
mysqldump.cnf

# The mysql.cnf is what you're going to edit:
nano /etc/mysql/conf.d/mysql.cnf

# By default, the mysql.cnf file might only contain one line: [mysql]. Add a new line below it so it looks like the following:

[mysql]
bind-address = 0.0.0.0

Test connection to the mysql instance from the remote machine/container using the newly created wordpress_user user.

# I'm performing these steps on the Ubuntu Apache web server, 192.168.1.103:
# Install the mysql-client package if it's not been installed.

apt-get install -y mysql-client

# Connect to the mysql instance at 192.168.1.101:
mysql -h 192.168.1.101 -u wordpress_user -p