PhpMyadmin on Docker/MySQL on host - mysql

I'm trying to install/configure phpmyadmin using docker inside Ubuntu 14.04. I started like this:
docker run --name myadmin -d -e PMA_HOST=localhost -e PMA_PORT=3306 -p 8282:80 phpmyadmin/phpmyadmin
When I try to login I get the following error:
#2002 - Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory") — The server is not responding (or the local server's socket is not correctly configured).
mysqli_real_connect(): (HY000/2002): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory")
The MySQL server is installed directly in Ubuntu not in docker.
Any ideas?

You cannot use localhost in your docker container.
docker run --rm --name myadmin -it -e PMA_HOST=172.17.0.1 -e PMA_PORT=3306 -p 8282:80 phpmyadmin/phpmyadmin
Where 172.17.0.1 is my host ip of the docker0 bridge.

This is what worked for me:
Check you have your db up and running:
"service mysql status" or "systemctl status mysqld"
(It should say active(running))
Run phpMyAdmin in a container:
docker run --name="phpMyAdmin-local" -itd -e PMA_HOST=$(ip route show | grep docker0 | awk '{print $9}') -p 8283:80 phpmyadmin/phpmyadmin
Check container is up and running:
docker ps -a
(check phpMyAdmin-local status is up)
Go to "localhost:8283" and check phpMyAdmin is there
Let your db receive external requests:
Edit file as sudo:
MariaDb:
nano /etc/mysql/mariadb.conf.d/50-server.cnf
MySQL:
nano /etc/mysql/my.cnf
(nano is the text editor and can be changed)
and change this line:
bind-address = 127.0.0.1
To this line: (note the "#")
# bind-address = 127.0.0.1
(ctrl+x to exit, "y" to save it and "enter" to confirm)
Give permision to users/user to connect from a different location (different from localhost):
Modify users and login to mysql/mariadb cli with root privileges:
mysql -uroot -p -P3306
(enter password after running command)
Use mysql database to edit users:
use mysql;
Give pribvileges to specific user: (can be different from root if you have other users):
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
Then:
FLUSH PRIVILEGES;
and:
exit;
Restart mysql service:
service mysqld stop
service mysqld start
And now you would be able to login to phpMyAdmin with the user and password you specify in prev. commands and mysql/mariadb would accept connections.

For mac OS users, use host.docker.internal as the host address which docker will resolve to the host's IP address.
docker run --rm --name myadmin -it -e PMA_HOST=host.docker.internal -e PMA_PORT=3306 -p 8282:80 phpmyadmin/phpmyadmin

Instead of connecting through socket file try to connect using IP (127.0.0.1) and for PMA port use your machine IP which you can get through ifconfig command.

Related

Connecting to MySQL host DB from docker containers in host mode not work

I try connect my mysql host DB from docker container by using host mode and I get the error:
docker run --rm -it --network=host mysql mysql -h 127.0.0.1 -utestuser -p
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)
The base of my work is stackoverflow-answer https://stackoverflow.com/a/24326540/1514029
My host mysql version is 8.0.25.
I tried in my.cnf bind-address = 0.0.0.0 and bind-address = 172.17.42.1
Every binding have the same problem.
I grant the testuser user access to 127.0.0.1 by statements
CREATE USER testuser#127.0.0.1 IDENTIFIED BY 'blah'
grant all privileges on *.* to testuser#127.0.0.1 with grant option
On docker bridge mode my connection work fine only on docker host mode it fails!
It must be a network problem!?
you can use:
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.25

How to connect to MySQL created with docker to another port (not port 3306)?

I already have installed mySql on my pc so port 3306 is already busy. This is the reason why I have to use a different port from 3306. I want to be able to connect with my machine to my docekr instance without using docker commands so I will be able to connect to that instance with my application (Spring web app).
Docker commands that I used:
docker run --name jt-mysql -e MYSQL_ROOT_PASSWORD=password -p 3307:3307 -d mysql
Then I tried to connect to that istance with:
mysql --user=root -P 3307 -p
In this case I get the following error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using
password: YES)
Please note that if I tried to use the instance installed on my pc it works, using with:
mysql --user=root -P 3306 -p
Other information about my docker instance using:
docker ps
I get:
f52a94aa63da mysql "docker-entrypoint.s…" 4
minutes ago Up 4 minutes 3306/tcp, 33060/tcp,
0.0.0.0:3307->3307/tcp jt-mysql
with status insided my docker image (entering using docker commands) I get:
Connection id: 11
Current database:
Current user: root#localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.19 MySQL Community Server - GPL
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: latin1
Conn. characterset: latin1
UNIX socket: /var/run/mysqld/mysqld.sock
Binary data as: Hexadecimal
Uptime: 16 min 50 sec
using env command:
HOSTNAME=f52a94aa63da
MYSQL_ROOT_PASSWORD=password
PWD=/
HOME=/root
MYSQL_MAJOR=8.0
GOSU_VERSION=1.7
MYSQL_VERSION=8.0.19-1debian9
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
Command to start MySQL container at port 3306 and expose at port 3307
docker container run -d --name=LocalMySQLDB -p 3307:3306 -e MYSQL_ROOT_PASSWORD=password mysql
OR
docker run -d --name=LocalMySQLDB -p 3307:3306 -e MYSQL_ROOT_PASSWORD=password mysql
The above command with start the MySQL database server inside "LocalMySQLDB" container
Now to connect to the containerized mysql instance use below attached command
mysql -h 127.0.0.1 -uroot -P 3307 -ppassword
I have tried this many a times on my local machine for testing purposes. It will definitely work for you as well.
Please comment if it will not work in your case.
Start Docker container using following command:
docker run -d -p 3307:3306 --name mysql_server -e MYSQL_ROOT_PASSWORD=123456 mysql
Connect to container from host using following command:
mysql -u root -P 3307 --protocol=tcp -p
When you run docker container, please try to add this param at the end.
docker run --name jt-mysql -e MYSQL_ROOT_PASSWORD=password -p 3307:3306 -d mysql --network host

Unable to connect to Docker Mysql from windows host

I have taken the latest docker image of mysql but I am unable to connect to it from windows host machine.
Executed the following commands:
docker run -p 3306:3306 --hostname=sql --name=mysql_working -d mysql/mysql-server:latest
I can see the IP address with the following command:
docker inspect --format "{{ .NetworkSettings.IPAddress }}" 3ddbeeeb27e9enter
When I do telnet, it is timing out
telnet sql 3306
same for ping
ping <ip address from docker>
Can anyone please advise on whats missing?
You are exposing the port 3306 so the Sql container is available to your host.
If you are on Windows machine type ipconfig
Or for Linux:
ifconfig or ip addr to find your host machine's IP Address and use that IP to connect to Sql.
You can also check docker container logs by docker logs -f container_id here -f is for following the logs.
step1: you need to the changed the default password of MySQL after the first install in docker container
docker logs <container_name or container_id>
docker logs <container_name or container_id> 2>&1 | grep GENERATED
step2:notedown default password
step3:
docker exec -it <container_name or container_id> mysql -uroot -p
Enter default password
ALTER USER 'root'#'localhost' IDENTIFIED BY 'password';
For more info of step1 to step 3 check here
step4:Add new user in mysql as username root and host any with password
create user 'root'#'%' identified by 'password';
step5:Grant all permission to that user
grant all privileges on *.* to 'root'#'%' with grant option;
For more info of step4 to step 5 check here
step 6: Exit from docker container: press ctrl+p+q keys (not plus key combination of ctrl with p and q)
step7: suppose you are on hostmachine then
(else you give ipaddress of hostmachine instead of localhost)
telenet -l root localhost 3306
It asks for password enter password (we given password as password in step4)
press ctrl+] key (not plus key combination of ctrl with ])
Telent connect successfully ..!!

Docker mysql: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I am trying to connect to mysql server using mysql cli. Image was created using following command:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=****** -d mysql
The docker container is running. docker ps prints:
johnd#john-pc:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
598c0f8680dc mysql "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 3306/tcp, 33060/tcp some-mysql
When i enter the following command:
mysql -h 127.0.0.1 -P 3306 -u root -p it returns:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
I also tried with --protocol=tcp attribute. How do i connect from client to mysql server running on docker using terminal from client machine (not from another docker)
EDIT:
I also tried connecting to mysql using this command:
docker run -it --rm mysql mysql -h127.0.0.1 -uexample-user -p
It returns the same error
mysql -h 127.0.0.1 -P 3306 -u root -p
You are connecting to your localhost's sql server but you didn't map the docker's container port to the host.
Try mapping the port to your host by this:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=****** -d -p 3306:3306 mysql
Then retry:
mysql -h 127.0.0.1 -P 3306 -u root -p
same error to me, just remember start mysql service first in your container.
exec in your container
/etc/init.d/mysql start
Using internal docker IP like 127.0.0.1 didn't work for me
I used my POD's IP and it worked
mysql -h 10.10.10.41 -P 3306 -u root -p
I still don't understand why I wasn't able to login and before and after this command I was able to login

Localhost connect MySQL Docker confusion

I created a test_mysql using the following command:
docker run -d -p 3306:3306 --name=test_mysql --env="MYSQL_ROOT_PASSWORD=123" mysql
I got the IP address using docker inspect test_mysql. The IP is 172.17.0.2.
The strange thing is that when I tried to connect to mysql server on my local using
mysql -uroot -p123 -h 172.17.0.2 -P 3306
An error raised:
ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.2' (51)
However, if I use the localhost IP address instead it did work:
mysql -uroot -p123 -h 127.0.0.1 -P 3306
My question is why I can't connect to the container use docker inspect result while localhost IP works?
1) while localhost IP works?
Let's see again your command to start container:
docker run -d -p 3306:3306 --name=test_mysql --env="MYSQL_ROOT_PASSWORD=123" mysql
The -p 3306:3306 will "bind" the port 3306 of host to the port 3306 of the container. As a result, we can see that if there is any connections come to port 3306, they will be forwarded to the port of the container.
So, your connections on local IP will work:
mysql -uroot -p123 -h 127.0.0.1 -P 3306
See more detail on Docker page
2) why I can't connect to the container use docker inspect result
It seems your container is connected to the default bridge network(docker0 may have IP:172.17.0.1 in your case ) which is often created by default when you install Docker.
You can find more detail in Docker network page.Therefore, inside your container, you may "see" the bridge (you can try to use ping command", but from your local host, it may not know how to find/resolve the 172.17.0.2 and you got that error.