MySQL

How to setup and use MySQL in docker container.

Create MySQL server inside container

docker run --name mysql-test -p 3377:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql:latest

This runs the latest version of MySQL on port 3377 (host) with root user, and password set to ‘password’.

After that we create a new user inside a container.

docker exec -it mysql-test mysql -u root -p
CREATE USER 'imagdic' IDENTIFIED BY 'imagdic123';
GRANT ALL ON *.* TO 'imagdic'@'%';
FLUSH PRIVILEGES;

Now we are ready to use our MySQL container with user ‘imagdic’ and password ‘imagdic123’ from our host machine.

Laravel application: if you are getting driver not found, make sure your ‘extension=pdo_mysql’ is uncommented in php.ini (/etc/php/php.ini)

DBeaver: If you are using DBeaver and MySQL 8+ version, you might get following error:

Public Key Retrieval is not allowed

You need to add ‘allowPublicKeyRetrieval’ and set it to ‘true’ inside connection properties. Visit https://community.atlassian.com/t5/Confluence-questions/MySQL-Public-Key-Retrieval-is-not-allowed/qaq-p/778956 for more information.

How to enable Remote access to your MariaDB/MySQL database

Use this on your own, better method is SSH Tunnel

Edit /etc/mysql/my.cnf like following:

# Add to end of file
[mysqld]
skip-networking=0
skip-bind-address

Verify MySQL Server:

netstat -ant | grep 3306

You should see the following:

tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN

You may also need to allow port in firewall sudo ufw allow 3306