Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access my docker maria db?

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

Somewhere I have seen a solution using a temporal (after exit the container is deleted) container, but cannot find it anymore.

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

like image 809
MorRich Avatar asked Oct 16 '15 12:10

MorRich


4 Answers

Just mysql-client, no extra docker container

Install the mysql client on your host,

apt-get install mysql-client

then use the following command to access your database container.

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

The command will automatically get the IP of your docker container.

Make sure to replace <user>, <pass> and <db-container> with your respective values. In your case:

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

Your command lets mariadb run at the standard port 3306. If not, you have to tell the mysql command the new port.

like image 175
michaelbahr Avatar answered Nov 04 '22 23:11

michaelbahr


from Official Mariadb website:

Connecting to MariaDB from Outside the Container

If we try to connect to the MariaDB server on localhost, the client will bypass networking and attempt to connect to the server using a socket file in the local filesystem. However, this doesn't work when MariaDB is running inside a container because the server's filesystem is isolated from the host. The client can't access the socket file which is inside the container, so it fails to connect.

Therefore connections to the MariaDB server must be made using TCP, even when the client is running on the same machine as the server container.

Most MariaDB images, including the official one, have external TCP connections disabled using the bind-address option in their #my.cnf# file. The docker image used in this guide is based on Ubuntu, so the file is located at /etc/mysql/my.cnf.

To use MariaDB we will need to edit the configuration file to change the appropriate option, and then restart the container.

Inside the container, edit the file my.cnf and check for the line that begins bind-address. Put a hash at the start of the line to comment it out:

#bind-address            = 127.0.0.1

Save the file.

While still inside the container, send the shutdown command to MariaDB. This will shut down the server and also exit back out to the host:

mysqladmin -u root -p shutdown

Start the container again. This time the MariaDB server will have networking enabled:

docker start mariadbtest

Find the IP address that has been assigned to the container:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mariadbtest

You can now connect to the MariaDB server using a TCP connection to that IP address.

Forcing a TCP Connection

After enabling network connections in MariaDB as described above, we will be able to connect to the server from outside the container.

On the host, run the client and set the server address ("-h") to the container's IP address that you found in the previous step:

mysql -h 172.17.0.2 -u root -p

This simple form of the connection should work in most situations. Depending on your configuration, it may also be necessary to specify the port for the server or to force TCP mode:

mysql -h 172.17.0.2 -P 3306 --protocol=TCP -u root -p

Port Configuration for Clustered Containers and Replication

Multiple MariaDB servers running in separate Docker containers can connect to each other using TCP. This is useful for forming a Galera cluster or for replication.

When running a cluster or a replication setup via Docker, we will want the containers to use different ports. The fastest way to achieve this is mapping the containers ports to different port on our system. We can do this when creating the containers (docker run command), by using the -p option, several times if necessary. For example, for Galera nodes we will use a mapping similar to this one:

-p 4306:3306 -p 5567:5567 -p 5444:5444 -p 5568:5568
like image 21
Sunfloro Avatar answered Nov 04 '22 23:11

Sunfloro


First access the container terminal

 docker exec -it some-mariadb bash

'some-mariadb' is the mysql container name

Then access the db directly using the mysql terminal command

mysql -u root -p
like image 10
Akhil Clement Avatar answered Nov 04 '22 21:11

Akhil Clement


Connect to MariaDB from the MySQL command line client The following command starts another mariadb container instance and runs the mysql command line client against your original mariadb container, allowing you to execute SQL statements against your 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.

More information about the MySQL command line client can be found in the MySQL documentation

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

like image 4
BMW Avatar answered Nov 04 '22 23:11

BMW