How to connect to mysql running in container from host machine - mysql

I'm using https://github.com/sameersbn/docker-mysql to run a mysql container using docker-machine in OSX with virtualbox.
I created a new machine
docker-machine create --driver virtualbox mytest
The IP is
docker-machine ip mytest
192.168.99.103
I run the container like this:
docker run -p 3306:3306 --name mysql -d \
-v /opt/mysql/data:/var/lib/mysql \
-e 'DB_USER=sampleuser' -e 'DB_PASS=samplepass' -e 'DB_NAME=sampledb' -e 'DB_REMOTE_ROOT_NAME=root' -e 'DB_REMOTE_ROOT_PASS=samplerootpass' \
sameersbn/mysql:latest
Now, when I try to connect to the mysql in the container from my hostmachine I can connect using user sampleuser but not as user root.
▶ mysql -u root -p -h 192.168.99.103
Enter password:
ERROR 1045 (28000): Access denied for user 'root'#'192.168.99.1' (using password: YES)
192.168.99.1 is my local laptops ip address
▶ ifconfig | grep "192"
inet 192.168.99.1 netmask 0xffffff00 broadcast 192.168.99.255

By default, root only has access from the localhost, 127.0.0.1 & ::1, you need to specifically allow access from 192.168.99.1 or from anywhere using '%' in the user setup : see here http://dev.mysql.com/doc/refman/5.5/en/default-privileges.html

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

Connecting to a MySQL server docker instance from localhost

I created a MySQL server docker instance using the following command:
[me#centos7 ~]$ docker run --name mysql_db -p 6604:3306 -e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD='jose' -d mysql/mysql-server:5.7
3d7d2c6231cbc2a8f96d1c965588c4349113c18aa01c8322dc79c670d6d02105
When I try to connect to the server, I get the following error:
[me#centos7 ~]$ mysql -uroot -pjose -h localhost -P6604
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
How should I fix my commands to be able to connect to the server instance using my local MySQL client?
you can read about bind-address
sudo lsof -i -P -n | grep LISTEN
docker-pr 673318 root 4u IPv4 8564315 0t0 TCP *:6604 (LISTEN)
try
mysql -uroot -pjose -h 0.0.0.0 -P6604

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 ..!!

mysql cli to 127.0.0.1 but redirect to 172.17.0.1

I have installed mysql-client in my Mac by the following command:
brew install mysql-client
And when I run the following command:
mysql -h 127.0.0.1 -u root -p
An ERROR 1045 occured:
ERROR 1045 (28000): Access denied for user 'root'#'172.17.0.1' (using password: NO)
but when I use Sequel Pro I can login the local mysql server.
Why the 127.0.0.1 becomes 172.17.0.1 ?
MORE DETAIL INFO
I run this command on local host iTerm. I use the docker pull to pull a mysql:5.7 image from docker registry:
docker run --name mysql -e MYSQL_ROOT_PASSWORD=xxxxxx -d -p 0.0.0.0:3306:3306 mysql:5.7
And the mysql container is just running, then I use Sequel Pro GUI client to connect into the database in docker, and it works. And then I try to use
brew install mysql-client
to install the mysql command (because I want to use it to run a sql file to prepare data for my project's tests suite). after installed mysql-client, i use the command
mysql -h 127.0.0.1 -u root -p
try to connect database, but it promote the
**ERROR 1045 (28000): Access denied for user 'root'#'172.17.0.1' (using password: NO)**.
I did not do any configuration for the mysql docker container.
After hours try and search, finally I found the solution:
mysql -h 127.0.0.1 -P 3306 -u root -p
you need a -P parameter

PhpMyadmin on Docker/MySQL on host

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.