Cannot connect to MYSQL docker container - mysql

I have created an image of a MySQL database and run it in a container. I would like it on ports 3406/3407, so I call the docker run command like this :
docker run -d -p 3406:3407 --name db ollyw123/shape-shop-db:latest
If I look at my containers it looks like this :
C:\Users\owatkins.ext>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b907f878f82b ollyw123/shape-shop-db:latest "docker-entrypoint.s…" 12 minutes ago Up 12 minutes 3306/tcp, 33060/tcp, 0.0.0.0:3406->3407/tcp db
I would very much like to connect to my database now but I can't seem to get my URL working.
This is what my URL looks like :
jdbc:mysql://localhost:3406/db

Default port of mysql is 3306 not 3407, so you should use port-forwarding with 3306
docker run -d -p 3406:3306 --name db ollyw123/shape-shop-db:latest

Related

How to connect to mysql docker container from flask application using sql-alchemy

I have a flask application. This application is running locally. I have pulled mysql:5.7 and it is running. I want to create a database inside the mysql:5.7 container from the flask application and use that. I am using sql_alchemy.
I have given the uri as
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root#172.17.0.2:4000/document_generator'
where root and root are mysql username and password respectively. 172.17.0.2 is the ip address of the mysql container. I have run the container using
docker run -p 4000:4000 -e MY_SQL_ROOT_PASSWORD=root mysql:5.7
I have exposed the port 4000. But, when i get inisde the conatiner and try
show global variables like 'port';
it gave 3306 as the port.
I tried using both the ports in the SQL_ALCHEMY_URI, but none of them were able to create the database.I also tried changing mysql:// to mysql+pymsql://. Even that dint work.
I have written the table creation code inside models.py
class Users(db.Model):
__tablename__ = 'users'
username = db.Column(db.String(128), nullable=True)
password = db.Column(db.String(256), nullable=False)
private_id = db.Column(db.String(128), primary_key=True)
I want the database to be created in the mysql container when i run my flask application. I have no idea about docker-compose. Any help would be of great help. Thanks in advance.
Your docker run command specificities the port to be published as 4000:4000.
The left side of the : is 4000 which is the port on your local machine, and that's fine.
But the right side of the : which is also 4000 is the port inside the container where is running MySQL. The default port of MySQL is 3306, not 4000, so you're trying to reach MySQL on the wrong port.
To fix that:
Stop the docker container
# Show all the running containers. The last column should be the container name.
$ docker ps
# Stop and remove the container
$ docker rm -f <container name>
Re-create the container with the right port
$ docker run -p 4000:3306 -e MY_SQL_ROOT_PASSWORD=root mysql:5.7
Now it should work.
Bonus
Docker exposes the containers also on the localhost. You can also reach the database on localhost:4000.
Publishing port will not change the PORT in MySQL schema, if you want to connect with MySQL container using port 4000 you just need to map 4000 with 3306.
docker run -p 4000:3306 -e MY_SQL_ROOT_PASSWORD=root mysql:5.7
Also will suggest using docker network instead of IP of the container, as IP change frequently.
But still if you want to start MySQL on port 4000 you can try something like
docker run -it -p 4000:4000 -e MY_SQL_ROOT_PASSWORD=root mysql:5.7 --port=4000
And then if you query like
show global variables like 'port';
You will get 4000.

Can't access MySQL database in Docker container from another container

I have MySQL 5.7 container pulled from here: https://hub.docker.com/_/mysql/
Here's how I run it:
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pwd -d mysql:5.7
It works good, I'm able to connect to MySQL db from my host machine.
However, when I try to run another container with mysql container linked like this:
docker run --link mysql:mysql -p 8080:8080 -d app:dev
my container can't connect to mysql:
# 172.17.0.3 is mysql's ip taken from /etc/hosts of another container.
mysql -h 172.17.0.3 -u root -ppwd
ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.3'
I tried to use docker networks but I'm getting the same error.
Here's nmap -p 3306 172.17.0.2 output:
Starting Nmap 7.01 ( https://nmap.org ) at 2018-06-03 08:34 UTC
Nmap scan report for e66874413058 (172.17.0.2)
Host is up (0.00012s latency).
PORT STATE SERVICE
3306/tcp closed mysql
Nmap done: 1 IP address (1 host up) scanned in 0.39 seconds
For unknown reason, the port is closed. If I run nmap command from my host, it's open.
How to connect to MySQL server from another docker container?
I have to admit I don't see immediately where it's going wrong because also IP based communication should work but let me explain the recommended way to let containers communicate.
When you link your app container with the mysql container (like your doing) you can access the mysql just on it's container name without using ip's.
In the default bridge network:
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pwd -d mysql:5.7
Now I start a random app and link it with mysql. curl and ping are installed in this container.
docker run -d -p 8080:8080 --link mysql:mysql randomapp
Now I go inside my randomapp container and try to ping the mysql container which works.
docker exec -it 7c4bc6f1ca7a bash
xxx#7c4bc6f1ca7a:/$ ping mysql
PING mysql (172.17.0.3) 56(84) bytes of data.
64 bytes from mysql (172.17.0.3): icmp_seq=1 ttl=64 time=0.076 ms
64 bytes from mysql (172.17.0.3): icmp_seq=2 ttl=64 time=0.049 ms
I can verify with an nmap container too
docker#default:~$ docker run --rm --link mysql:mysql uzyexe/nmap mysql 3306
Starting Nmap 7.60 ( https://nmap.org ) at 2018-06-06 05:54 GMT
setup_target: failed to determine route to 3306 (0.0.12.234)
Nmap scan report for mysql (172.17.0.3)
Host is up (0.000010s latency).
Not shown: 999 closed ports
PORT STATE SERVICE
3306/tcp open mysql
MAC Address: 02:42:AC:11:00:03 (Unknown)
Nmap done: 1 IP address (1 host up) scanned in 1.65 seconds
docker#default:~$
If you deploy your app and mysql in the same user defined bridge network you don't need to define the --link option and your containers can talk with each other by using their container name.
docker network create my-bridge
docker run --name mysql --net my-bridge -p 3306:3306 -e MYSQL_ROOT_PASSWORD=pwd -d mysql:5.7
docker run -d -p 8080:8080 --net my-bridge randomapp
It's recommended to use user defined networks and not the 'deprecated' --link feature in the default bridge network.

running mysql server and phpmyadmin in docker containers

I spin up a docker container for MySQL server
docker run --detach --name=mysql_db_server --env="MYSQL_ROOT_PASSWORD=password" mysql
Then I run another container for phpmyadmin that is linked to MySQL server as follows
docker run --name myadmin -d --link mysql_db_server:mysql -p 8080:80 phpmyadmin/phpmyadmin
I am able to see phpmyadmin on http://localhost:8080 but I am not able to log into it using either
root --- password or
admin --- password
$docker ps -a
output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85e68b8bab30 phpmyadmin/phpmyadmin "/run.sh phpmyadmin" 3 hours ago Up 3 hours 0.0.0.0:8080->80/tcp myadmin
b4d130cdb230 mysql "docker-entrypoint.s…" 3 hours ago Up 3 hours 3306/tcp mysql_db_server
What am I doing wrong?
Use myadmin instead of mysql
docker run --name myadmin -d --link mysql_db_server:myadmin -p 8080:80 phpmyadmin/phpmyadmin
Log in to MySQL console with your user:
For that run the command -- docker exec -it app_db_1 /bin/bash
and now you can login to MYSQL console with your user:
root#5f1d313df243:/# mysql -uroot -ppassword
and change the Authentication Plugin with the password here:
mysql> ALTER USER root IDENTIFIED WITH mysql_native_password BY 'PASS';
you will get output something like this -- Query OK, 0 rows affected (0.08 sec)
exit
exit
Read more about the Preferred Authentication Plugin on the MySQL 8.0 Reference Manual
https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password
It will work perfectly with a docker as well as docker-compose:
now you can log in to phpMyAdmin on http://localhost:8080 with root & PASS.
(Don't use port 8080 because it might get a problem when your system already uses 8080 port other processes.)

Running two Mysql docker containers

I am trying to run two different mysql containers for master->slave replication. I start by building and running the master:
docker build --no-cache -t mysql-master .
docker run -it --name mysql-master -h mysql-master -p 3306:3309 mysql-master /bin/bash
Which works fine and runs the container correctly. I can get as far as getting the information to set up the second container, mysql-slave. When I run the following command:
docker build --no-cache -t mysql-slave .
docker run -it -p 3308:3309 mysql-slave --name mysql-slave --link mysql-master:mysql-slave /bin/bash
The mysql-master container disconnects. I am not sure why but I am sure that there is some kind of conflictions going on with the containers that I may not be aware of. Can anyone suggest what docker command I should be running so that both containers can run simultaneously?
I have a feeling this is because both containers are attempting to access the same port:
root#test2net:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1942b5e1f69 mysql-slave:latest "/tmp/makeSlaveSQL.s 40 seconds ago Up 40 seconds 3306/tcp, 0.0.0.0:32773->3307/tcp mysql-slave
c9a7632d9cae mysql-master:latest "/tmp/makeMasterSQL. 2 minutes ago Up 2 minutes 0.0.0.0:32769->3306/tcp mysql-master
Is there a way to explicitly cast each container to a specific port. I have tried using EXPOSE in the Dockerfile and the -p to designate different ports but as you can see from above, mysql-slave is still binding to port 3306.

unable to connect to dockerized mysql container locally

I am still a beginner with docker, trying to use docker to help in my development prototyping. My environment is Mac using boot2docker, version as below
Client version: 1.3.1
Client API version: 1.15
Go version (client): go1.3.3
Git commit (client): 4e9bbfa
OS/Arch (client): darwin/amd64
Server version: 1.3.2
Server API version: 1.15
Go version (server): go1.3.3
Git commit (server): 39fa2fa
I ran the command as below:
docker run --name mymysql -e MYSQL_ROOT_PASSWORD=mypw -e MYSQL_DATABASE=bullshit -d mysql -p 3306:3306
docker start mymysql
I can see the process running as below:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22d3f780c270 mysql:5 "/entrypoint.sh -p 3 2 minutes ago Up 2 seconds 3306/tcp mymysql
However I still could not connect to the mysql instance running in the docker. I tried connect to the ip retrieved by :
$ boot2docker ip
The VM's Host only interface IP address is: 192.168.59.103
Please give me a pointer on how to solve this issue, I went through the tutorial but I am not sure what went wrong.
The command you used should give an error. The syntax for docker run is as follow:
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
You have to submit the options to docker run before specifying the image used (mysql in your case), and if it's the case, the command and possible argument(s) to that command.
Not specifying a command will run the default command in the image.
Before running again the container you should stop and remove the old one:
docker kill mymysql
docker rm mymysql
And, following your example you should run:
docker run --name mymysql -e MYSQL_ROOT_PASSWORD=mypw -e MYSQL_DATABASE=bullshit -p 3306:3306 -d mysql
As you set manually a port mapping from container's port 3306 to the same port of your Boot2docker VM, you should can access to MySQL using the IP of the Boot2docker instance, typically 192.168.59.103, and connecting to port 3306.