I'm trying to start Vault docker container with mysql storage using this command:
docker run --cap-add=IPC_LOCK -e 'VAULT_LOCAL_CONFIG={"storage": {"mysql": {"username":"root", "password":"hello", "database":"vault", "address":"127.0.0.1:3306"}}, "listener": {"tcp":{"address":"127.0.0.1:8200", "tls_disable":"1"}}, "default_lease_ttl": "168h", "max_lease_ttl": "720h"}' -e VAULT_SKIP_VERIFY=true vault server
This is the error I'm getting:
Error initializing storage of type mysql: failed to check mysql schema
exist: dial tcp 127.0.0.1:3306: connect: connection refused
I can connect to mysql using the username and password I am supplying to the previous command.
I also made sure that the mysql is running on the 3306 port
[root#jwahba]# netstat -tlpn | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 39552/mysqld
I checked out the vault official document (here) but it's not obvious what is wrong in my configuration. Any suggestions please ?
You are trying to connect to a db on localhost from a Docker container, but they are on different network stacks. Use --net="host" in your docker run command; 127.0.0.1 in your docker container will now point to your docker host.
Source: From inside of a Docker container, how do I connect to the localhost of the machine?
Related
I've a mysql inside docker container, which is running on the server.
And I can run it normally inside the container
using
mysql -u root -h localhost --port=3306 -p
I've mapped the container 3306 to host 33069 port.
netstat -tnlp | grep :33069
the above command runed on the server shows me the following result
tcp6 0 0 :::33069 :::* LISTEN 120562/docker-proxy
from which it's clear that mysql is available not only for localhost.
However when I try to connect to the mysql from my local machine using the command
mysql -u root -h myAddress.com --port=33069 -p
I get the error
Can't connect to MySQL server on ...
after timeout.
What I've missed?
if you are using the docker run command, the container's port 3306:33069 needs to be exposed, and the docker MySql container host's server port, if the firewall is enabled for the port 33069.
Afterwards on your local pc, the port 33069 needs to be open, to communicate to MySql docker container.
Following the guide to create a mysql NDB cluster on https://hub.docker.com/r/mysql/mysql-cluster/ after initializing docker server exposing 3306 and 33060 I'm still unable to connect it using MySQL Workbench but I'm able to access it through CLI. Workbench throws an error saying as if there wasn't a database to be connected.
docker run -d --net=cluster --name=mysql1 --ip=192.168.0.10 -e MYSQL_RANDOM_ROOT_PASSWORD=true -p 3306:3306 -p 33060:33060 mysql/mysql-cluster mysqld
Besides trying to login with root I've also created a new user to try to login with the same outcome.
I have setup a MySQL instance in a docker container using the instructions I found here. I'm able to connect to the instance by bashing into the container and then running the MySQL client like so:
docker exec -it mySQLContainer bash
and then
mysql -uroot -p
But I can't see to connect to the instance from the host machine. So far I've tried the following:
mysql localhost
mysql -h localhost -P 3306
which returns
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/tmp/mysql.sock' (2)
and
mysql -h localhost -P 3306 --protocol=tcp -u root
mysql -h 127.0.0.1 -P 3306 --protocol=tcp -u root
mysql -h 127.0.0.1 -P 3306 -u root
which returns
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (61)
I've found a few other questions about this same problem but the solutions don't seem to work. Can anyone see what I'm doing wrong here or what I need to do in order to connect?
Note:
Here is the container I spun up for MySQL - let me know if I need to include any other information:
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f1fc20d66c0 mysql:latest "docker-entrypoint.s…" 2 days ago Up 2 days 3306/tcp, 33060/tcp plugins
UPDATE:
These are the last two lines from my container log - not sure if it's relevant or not:
2020-02-07T20:02:22.887174Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.19' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
2020-02-07T20:02:22.982168Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
When I do this, I launch the container with an external port mapped to 3306.
For example, here's how I launch a container to run Percona Server 8.0:
docker run -d --name ps8 -e MYSQL_ROOT_PASSWORD=XXX -p 6603:3306 percona/percona-server:8.0
Then I can connect to the external port, and Docker will send it to 3306 inside the container:
mysql -h 127.0.0.1 -P 6603 -uroot -pXXX
I use port 6603 because I also have a MySQL instance running on my laptop using 3306. But if you want to use the default port 3306 and it doesn't conflict, that should work too.
Re your comment:
Unless you specify the port to use from outside the container, the ports are not exposed. You can't contact a port inside a docker container by default.
This is why the -p option is important. It tells docker you want it to allow connections from outside the container, and it tells docker what port it should listen on, to proxy to the mysqld process inside the container.
When I view my running container, I see there is a mapping from 6603->3306.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1c949e0a8ae percona/percona-server:8.0 "/docker-entrypoint.…" 44 seconds ago Up 43 seconds 33060/tcp, 0.0.0.0:6603->3306/tcp ps8
MySQL 8.0 also listens on 33060, which is the XDev Protocol port. I don't currently configure a mapping for this port, but I could.
I have a docker container running a Flask application that connects to a mySQL server. The mySQL server is hosted on the host machine at port 3308 on a windows 10 machine.
When executing
docker run -p 5000:5000 -p 3308:3308 -t webui
I receive the error
Ports are not available: listen tcp 0.0.0.0:3308: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
due to the port being used by the mySQL server on the host machine
How do I map the port of the mySQL to the docker container such that the Flask application can access the database?
There are 2 ways to achieve this. The first approach is the recommended one.
The first is to add an entry to /etc/hosts inside the container:
docker run -p 5000:5000 -p 3308:3308 --add-host database:<HOST_IP> -t webui
You need to replace HOST_IP with the network IP of your host. Then you can reference the database inside your container using the name "database" (you can also customize this one).
The second is to bind your container to your host's interface:
docker run -p 5000:5000 -p 3308:3308 --bind 127.0.0.1 -t webui
Then you can refer to your database with 127.0.0.1 inside your container.
The issue was caused by the host name. The mySQL database port did not need to be bound to the container as it did not need to receive any inbound calls, only outbound to the database. Resolved by adding a new entry into the container's /etc/hosts file as described here.
This question is in relation to
Dockerized web app connecting to MySQL DB on host
I am trying to open up a connection from a docker container to the host to support MySQL connections.
The way I understand it I should be able to execute the following in my container
nc.traditional -l -p 3306 -c "nc.traditional 172.17.42.1 3306" &
to open up a tunnel from the Docker container port 3306 to the host (IP 172.17.42.1) MySQL instance, running on port 3306.
Trouble is as soon as I try to connect from the container
mysql --host=127.0.0.1 --port=3306 -uroot -ppassword
I get an error and the tunnel exits
root#7ec710b77baf:/var/log# mysql --host=127.0.0.1 --port=3306 -uroot -pAcc355
(UNKNOWN) [172.17.42.1] 3306 (mysql) : Connection refused
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
[1]+ Exit 1 nc.traditional -l -p 3306 -c "nc.traditional 172.17.42.1 3306"
Why would the tunnel exit? What am I doing wrong? It certainly seems to contact the MySQL instance as I get a different error message when I try a different port.
I haven't been able to find any info in logs or on std out to help.
Any ideas?
From my experience you're probably after socat rather than netcat.
eg
socat TCP-LISTEN:3306,fork TCP:db-host:3306
I've found issues with netcat handling the connection