mySQL / MariaDB - Configure mySQL or MariaDB to allow remote connections on Docker

After a clean install of mySQL or MariaDB on Docker, you should be able to connect to the database as root on the Docker system. The docker exec command with the -it or --interactive --tty files can be used to get a shell on the container.

docker exec -it mariadb bash

 

You should be able to connect to MariaDB as root, using the password that you defined in the MARIADB_ROOT_PASSWORD variable.

root@c132e43cd93a:/# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.6.4-MariaDB-1:10.6.4+maria~focal mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

 


Remote connection

To be able to make a remote connection to the container, the /etc/mysql/mariadb.conf.d/50-server.conf file in the container will need to have the bind address of the container. For this reason, you may want to use the docker cp command to copy the files in the /etc/mysql/mariadb.conf.d directory in the container to a directory on your Docker system, such as /usr/local/docker/mariadb/mariadb.conf.d.

docker cp mariadb:/etc/mysql/mariadb.conf.d /usr/local/docker/mariadb

 

Then edit the 50-server.conf file to have the IP address 0.0.0.0 so that all connections are allowed. Or, this could instead have the IP address of the container.

bind-address = 0.0.0.0

 

You would then use the --volume option to mount /usr/local/docker/mariadb/mariadb.conf.d directory on your Docker system to /etc/mysql/mariadb.conf.d in the container, and also use the --ip option to assign an IP address to the container.

docker run
--detach
--publish 3306:3306
--volume /usr/local/docker/mariadb/root:/root
--volume /usr/local/docker/mariadb/mariadb.conf.d:/etc/mysql/mariadb.conf.d
--ip 172.16.0.2
--env MARIADB_ROOT_PASSWORD=itsasecret
--name mariadb
--restart unless-stopped
mariadb

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter f2475 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |