How can I access my docker maria db?

My main question is: after I created the docker run --name db -e MYSQL_ROOT_PASSWORD=test -d -p 3306:3306 mariadb container for my mariadb using the docker run --name db -e MYSQL_ROOT_PASSWORD=test -d -p 3306:3306 mariadb , how can I access sql db?

Somewhere I saw a solution using a temporary (after the container exited) container, but I can no longer find it.

I am looking for a command like: sudo docker exec -it [other flags] [command] db .

+5
source share
2 answers

Connecting to MariaDB from the MySQL command line client The following command starts another instance of the mariadb container and launches the mysql command line client against your original mariadb container, allowing you to execute SQL statements on the database instance:

 $ docker run -it --link some-mariadb:mysql --rm mariadb sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"' 

... where some-mariadb is the name of your original mariadb container.

For more information on the MySQL command line client, see the MySQL documentation.

Contact: https://hub.docker.com/_/mariadb/

+2
source

Just a mysql client, without an additional docker container

Install the mysql client on your host,

 apt-get install mysql-client 

then use the following command to access the database container.

 mysql -u<user> -p<pass> -h $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' <db-container>) 

The team will automatically get the IP address of your docker container.

Be sure to replace <user> , <pass> and <db-container> with the appropriate values. In your case:

 mysql -uroot -ptest -h $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' db) 

Your command allows mariadb to run on standard port 3306. If not, you must tell the mysql command the new port.

+2
source

Source: https://habr.com/ru/post/1233864/


All Articles