phpmyadmin mysql docker simple link - mysql

I try simple link two container in docker: phpmyadmin and mysql.
What I do:
$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=admin -d mysql
$ docker run --name myadmin -d --link mysql:db -p 8080:80 phpmyadmin/phpmyadmin
But, wen login in phpmyadmin, getting an error:
mysqli_real_connect(): (HY000/2002): Connection refused
I dont want use docker-compose

From what I have seen/read, this is because of a bug, that causes it. Try it with the mariadb container, works fine, at least for me , on Ubuntu 16.04

Related

cant connect to mysql database while using laravel on docker(not found,pdo,connection refused)

i wanted to dockerize my laravel app.
i launched two containers of mysql and php like this
sudo docker pull mysql/mysql-server:latest
sudo docker run --name=mysql -e MYSQL_ROOT_PASSWORD=mypass -d mysql/mysql-server:latest
sudo docker pull phpmyadmin/phpmyadmin:latest
sudo docker run --name phpmyadmin -d --link mysql:db -p 8081:80 phpmyadmin/phpmyadmin
so i configured my user and imported my db manually in phpmyadmin
i launch my laravel container using my docker file etc
#some other code ...
EXPOSE 8000
CMD php artisan serve --host=0.0.0.0 --port=8000
my env file
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:aS838/4/6V7n9VQXDUdiQBDxDiM7sWhSc1ym0KahcZY=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=mypass
if i keep db_host as 127.0.0.1 i get connection refused error if i give db_host as mysql(which is my running mysql container name) i get pdo errors if i give db_host as localhost i get not found errors i saw on few stackoverflow fixes that u have to give your local network ips instead i dont wanna do it cause i have my own reasons any way around this? .any help will be appreciated
You should add --network to docker run command for all of your containers so that they will be in the same network, like:
docker network create SOME_NAME
sudo docker run --network SOME_NAME --name=mysql -e MYSQL_ROOT_PASSWORD=mypass -d mysql/mysql-server:latest
sudo docker run --network SOME_NAME --name phpmyadmin -d --link mysql:db -p 8081:80 phpmyadmin/phpmyadmin
Also DB_HOST's value should be changed from 127.0.0.1 to mysql.

how two link two docker containers, and how to use the mysql client on a mysql docker container

I'd like to test webtrees PHP docker. They suggest connecting to a mysql docker using --link mysql:db, as follows:
docker run -d -p 80:80 --name webtrees --link mysql:db -v /webtrees/data:/var/www/html/data -v /webtrees/media:/var/www/html/media -e DISABLE_SSL=TRUE -e PORT=80 --restart always dtjs48jkt/webtrees
Their README says:
The image does not contain a MySQL database. Instead you have to use a separate MySQL instance. For example you could use the
MySQL Docker Image. Using the --link parameter a direct connection to
the database in an other container could be established. If you use
the --link parameter it is sufficient to set as database hostname db
and port 3306. The database user must have all access rights to
create the necessary database and tables.
However the webtrees container cannot access the mysql server. How I correctly link these two docker containers?
I tried using the official mysql docker image as follows:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=webtrees -e MYSQL_USER=my_user -e MYSQL_PASSWORD=my_pwd -d mysql:5.5
but then I don't know how to link webtrees docker container with the mysql container.
Also, how can I use the mysql client? the documentation gives this example, but I don't understand what are the correct parameters for netowrk and -h:
$ docker run -it --network some-network --rm mysql mysql -hsome-mysql -uexample-user -p
Regarding your first question, the --link option for docker run is deprecated according to the documentation, so I wouldn't recommend using it.
With the amount of configuration required, I'd recommend setting up a docker-compose.yml instead. I set up the configuration you require like this:
version: '3.0'
services:
webtrees:
image: dtjs48jkt/webtrees
restart: always
ports:
- 80:80
environment:
- DISABLE_SSL=TRUE
- PORT=80
volumes:
- /webtrees/data:/var/www/html/data
- /webtrees/media:/var/www/html/media
networks:
- my-network
mysql:
image: mysql:5.5
environment:
- MYSQL_ROOT_PASSWORD=my-secret-pw
- MYSQL_DATABASE=webtrees
- MYSQL_USER=my_user
- MYSQL_PASSWORD=my_pwd
networks:
- my-network
networks:
my-network:
To run the containers, use:
docker-compose up --detach
What this will do is spin up a mysql container and a webtrees container according to the configuration you specified in your question with a network called my-network.
In the web interface of web trees on http://localhost/ you can make it connect to the mysql container with the following configuration, so it will connect to it through the docker network:
Since the service name in the docker-compose.yml is mysql, the required hostname is mysql.
Basically you need to have all the containers (mysql DB server, mysql client and application) in the same Docker network. By default they are not. Alternatively, --link can be used to link them (as shown in webtrees run example), but it's considred as legacy feature and network should be used instead of that.
So what you need to do:
Create custom Docker network:
docker network create user-network
Run mysql server in that network. Name should be db, because webtrees relies on that hostname for DB:
docker run --name db --network user-network -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=webtrees -e MYSQL_USER=my_user -e MYSQL_PASSWORD=my_pwd -d mysql:5.5
Run mysql client in the same network:
docker run -it --network user-network --rm mysql mysql -hdb -umy_user -p
Finally you can run an app in the same network:
docker run -d -p 80:80 --name webtrees --network user-network -v /webtrees/data:/var/www/html/data -v /webtrees/media:/var/www/html/media -e DISABLE_SSL=TRUE -e PORT=80 --restart always dtjs48jkt/webtrees
After that web app should be accessible from your browser under http://localhost/

Basic MySQL Docker Container

I just created a mysql docker container using
docker run -p 3310:3310 --restart always --env MYSQL_ROOT_HOST=% --name=ipca-mysql -e MYSQL_ROOT_PASSWORD=admin -d mysql/mysql-server:5.7
I am able to connect to my database using docker exec -ti ipca-mysql bash and mysql -u root -p
But when I tried to connect to my database at localhost:3310 using mysql workbench I get:
lost connection to mysql server at 'reading initial communication
packet' system error 0
Any idea what is this issue?
MySQL container start using this command it's worked.
docker run -p 3310:3306 --restart always --env MYSQL_ROOT_HOST=% --name=ipca-mysql -e MYSQL_ROOT_PASSWORD=admin -d mysql/mysql-server:5.7
You could use this docker command:
docker run --name mysql-tacs -e MYSQL_ROOT_PASSWORD=root -d -p 3310:3306 mysql
In the link below there is a video which shows step by step the creation of mysql server with docker until the connection in its server using MySQL Workbench.
https://www.youtube.com/watch?v=ZZwvhMvUJiw

Unable to connect Docker PhpMyAdmin to MySQL Server Mac OS, error #2002

Description
I'm attempting to run a container with PhpMyAdmin that connects to the MySQL Community Server I installed on my Mac OS.
As can be seen below, MySQL is running.
As can be seen below, I can connect via terminal.
Using the following command:
mysql --host=localhost --port=3306 --user=root --password="o_oGLZDI<1-t"
Problem
I am unable to connect to MySQL properly with PhpMyAdmin from docker. I've tried the following command lines:
docker run --name myadmin -d -e PMA_HOST=127.0.0.1 -e PMA_PORT=3306 -p 8080:80 phpmyadmin/phpmyadmin
docker run --name myadmin -d -e PMA_HOST=localhost -e PMA_PORT=3306 -p 8080:80 phpmyadmin/phpmyadmin
They generate these errors, when I attempt login:
127.0.0.1 version
#2002 - Connection refused — The server is not responding (or the local server's socket is not correctly configured).
localhost version
#2002 - No such file or directory — The server is not responding (or the local server's socket is not correctly configured).
Question
What is the correct command line required to run docker with the correct configurations to connect to my MySQL Server Mac OS ?
Your command:
docker run --name myadmin -d -e PMA_HOST=127.0.0.1 -e PMA_PORT=3306 -p 8080:80 phpmyadmin/phpmyadmin
This will point to localhost but to the localhost inside the phpmyadmin container and not to the localhost of your machine (where mysql is running).
You can run your container on your host network which will disable container networking. This means you will be able to reach your mysql container using localhost:
docker run --name myadmin --network=host -d -e PMA_HOST=127.0.0.1 -e PMA_PORT=3306 phpmyadmin/phpmyadmin
You can access your phpmyadmin on port 80 (not 8080) because the container network is not used when you specify --network=host. (It could be you need to adapt your firewall to allow docker0)
Another option (a better one), especially for MacOS (since Docker version 17.06) is to use docker.for.mac.localhost as PMA_HOST. This should resolve to your mac internal address (but I was not able to test it for now).
docker run --name myadmin -d -e PMA_HOST=docker.for.mac.localhost -e PMA_PORT=3306 -p 8080:80 phpmyadmin/phpmyadmin

Connect to Docker container running mysql on Windows 10

I am using Docker for Windows, on Windows 10 Enterprise. I am trying to connect to a container that is running mysql. I followed the instruction here https://hub.docker.com/_/mysql/ and I used this command to start the container docker run --name memories -e MYSQL_ROOT_PASSWORD=password -d mysql:5.6
if I type docker ps I get
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
300248b56399 mysql:5.6 "docker-entrypoint.sh" About an hour ago Up About an hour 3306/tcp memories
However I cannot figure out how to connect to this container from the host. I have tried localhost and 127.0.0.1. Each time I get an error like this
/* Connecting to 127.0.0.1 via MySQL (TCP/IP), username root, using password: Yes ... */
/* Can't connect to MySQL server on '127.0.0.1' (10061) */
Any suggestions?
I guess it was more simple than I thought. I had to publish port 3306
docker run -p 3306:3306 --name memories -e MYSQL_ROOT_PASSWORD=password -d mysql:5.6
Docker MySQL and phpMyAdmin containers on Windows 10
Persist MySQL volume in “C:\mysql” folder.
Set username and password for MySQL database. Create database.
docker run --name mysql-server_v2 -d --restart=unless-stopped -v C:\mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=rootp -e MYSQL_USER=user1 -e MYSQL_PASSWORD=user1p -e MYSQL_DATABASE=projectX mysql:5.7.29
Connect phpMyAdmin and accessing it with localhost:8080
docker run --name phpmyadmin_v2 -d --link mysql-server_v2:db -p 8080:80 phpmyadmin/phpmyadmin
src: https://devwl.pl/docker-setup-mysql-with-phpmyadmin-on-windows10-11/