how to access mySQL server in kubernetes - mysql

I have created a MySQL deployment in kubernetes and exposed it as nodes-port.
What I can do:
Access it from inside the cluster using
kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -ppassword
What I want to do:
Access the MySQL server from outside the cluster(like accessing a normal MySQL server).
Kubernetes v1.13 in DigitalOcean Cloud.
Guide me, please.

You can access it by mysql -u {username} -p {password} -h {any kubernetes worker ip} -P {nodePort}. After you start mysql container and expose it ad node port through a service.

You need to specify the MYSQL_ROOT_PASSWORD while bringing up the pod. How were you able to bring it up in Docker without it?

Related

Delphi Firedac connect to MySQL in docker container

I have created a MySQL image on my Windows 10 using the default settings from Docker.
I started the container using this command:
docker run --name local-mysql --network="host" -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d <your-docker-Image>
I used the --network parameter in the hope that I could connect to the container from my host computer.
Then I ran this command to connect to the container from the MySQL shell
docker exec -it mysql bash -l
I was able to connect using this
mysql -h localhost -P 3306 --protocol=tcp -u root -p
Using Delphi and setting FireDac to use DriverId MySQL, I specified host as localhost, port 3306, user as root and the password.
But I get this connection error
[FireDAC][Phys][MySQL] Cannot connect to MySQL server on 'localhost:3306' (10061)
I have tried using 127.0.0.1 and 0.0.0.0 without success and with the same error.
I would appreciate it if anyone has tried it with Delphi FireDac to connect to a MySQL container hosted on the same computer.
Thank you in advance.
I was able to solve the issue by using this command to run the image
docker run --name local-mysql -p 127.0.0.1:3307:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d <your-docker-Image>
I used a different host port (3307) to map to the default 3306
I was able to test it using the bash first
mysql -h 127.0.0.1 -P 3306 -u root -p
And in Delphi FireDac, I used the following to connect
Host=127.0.0.1
Port=3307
User_Name=root
Password=my-secret-pw
And all is good. Hope this helps someone who is trying to the same.

Connect to mysql in a Docker container from another Docker container in a MacOS host

I have been trying to set up several Docker containers for different mysql databases in my MacOS host system.
So, I have 2 different mysql databases in containers built from the mysql/mysql-server:5.7 image.
docker run --name=db-1 -d -p 1200:3306 mysql/mysql-server:5.7
docker run --name=db-2 -d -p 1201:3306 mysql/mysql-server:5.7
Since they are binded to 0.0.0.0:120x, I am able to access the mysql cli using
mysql -uroot -p -h 0.0.0.0 -P 120x (WORKS)
However, I really wanted to try and access the databases using the IP of the container which I found using
docker inspect db-1 | grep IPAddress
I found that the IP of the container is 172.17.0.8
So logically
mysql -uroot -p -h 172.17.0.8 -P 3306
should have worked, but it doesn't.
I get an error that reads out ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.8' (60)
I was trying to access using the container IP so I could access the databases from other containers by linking them. However this doesn't seem to work.
What am I missing?

How to run Docker's container in order to access MySQL Database

I need an old version of Mysql Server : 5.1.73
I would like to use it inside a container, but I can't find an image since the first version available is 5.5. So I decided to install a CentOS 6 image and then install Mysql 5.1.73 using yum.
At then end I have a container with CentOS 6 and Mysql installed and configured according to my needs. At this step I commit the image in order to run the container.
I try the following command :
docker run -p 3307:3306 --name test-mysql --mount source=databases-vol,target=/databases -d centos6-mysql
But when I try to connect to my database I have an error message like this :
MySQL said: Host '172.17.0.1' is not allowed to connect to this MySQL server
What did I miss ?
by default.. mysql doesnt allow connections from remote host (even though you have docker container your machine is remote host)
this may work..
Login to docker and grant remote access..
$ docker exec -it mysql_singstep bash
# mysql -h localhost -u root -p
https://medium.com/#gchandra/install-mysql-8-using-docker-easily-10091d575441

Cannot connect to MySQL server inside Docker

First I run mysql image:
docker run -e MYSQL_ROOT_PASSWORD=password -d -p 127.0.0.1:3308:3306 mysql
Then I use container bash:
docker exec -it my_container_name bash
In Bash I can successfully connect to MySQL server via command:
mysql -uroot -ppassword
But when I try to connect to MySQL container from Windows cmd:
mysql -uroot -ppassword -h127.0.0.1 -P3308
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (10061)
If I connect to 192.168.99.100 instead (this ip is returned by docker-machine ip), then the result is the same.
The question is: How do I correctly expose my MySQL port inside Docker to outside Windows?
The error is in your port mapping in the original docker run command, you just need to provide the ports, not the IP address:
docker run -e MYSQL_ROOT_PASSWORD=password -d -p 3308:3306 mysql
You can run docker ps -a to check for the port mapping in the running containers.
You should now be able to connect to MySQL using
mysql -uroot -ppassword -h192.168.99.100 -P3308
First, check netstat -an to make sure the port is open in Windows. If it is, also check the Windows firewall to make sure nothing is blocking connections to the port.
Most of my Docker experience is in CoreOS, so I'm not exactly sure how Windows handles routing traffic into the container. In CoreOS, it uses a proxy. If there is a proxy in Windows, make sure nothing is interfering with it.
Changing the port in which I was running the image worked.
I checked if this port was used by something else, but it was not used. so I just -desperately- run a new container in a different port '3309'. and it worked fine!
Make sure you have entered the port with -P.

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.
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.
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
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
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/
Slightly different syntax, docker 18.05.0-ce on ubuntu 18.04:
sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' db