Unable to connect to MYSQL from Docker Instance - mysql

I'm new to Docker and pulled the mysql from the docker hub using following command
docker pull mysql
Now, started mysql instance using following commands.
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest
Unable to find image 'mysql/mysql-server:latest' locally
latest: Pulling from mysql/mysql-server
e64f6e679e1a: Pull complete
799d60100a25: Pull complete
85ce9d0534d0: Pull complete
d3565df0a804: Pull complete
Digest: sha256:59a5854dca16488305aee60c8dea4d88b68d816aee627de022b19d9bead48d04
Status: Downloaded newer image for mysql/mysql-server:latest
1be4f5dde2fb4e1d99e23ee2de5e3b663c7c2d0fabe7ef459cf87385071a2e0d
Now, I've checked what all services are up and running.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1be4f5dde2fb mysql/mysql-server:latest "/entrypoint.sh mysql" 3 minutes ago Up 3 minutes (healthy) 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
e7d9e3713f5c ubuntu "/bin/bash" 6 days ago Up 6 days angry_hodgkin
and now I execute the following commands and unable to connect to mysql -
mysql -h127.0.0.1 -ppassword -uroot
-bash: mysql: command not found
Docker version..
docker --version
Docker version 1.12.3, build 6b644ec
Now, I'm getting below erro - EDIT-1
docker exec -it 1be4f5dde2fb bash
bash-4.2# mysql
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
bash-4.2# sudo mysql
bash: sudo: command not found
https://hub.docker.com/r/mysql/mysql-server/

Get in the docker container using the following command
docker exec -it mysql bash
You can then easily connect to MySQL, by executing the following command
mysql -uroot -p
If you are trying to connect from the host machine, then you will have to install the mysql client.
On mac, the command is as follows
brew install mysql

Related

MySQL shell cannot connect to MySQL docker container

I have a MySQL docker image running on Windows. I created it like this
docker pull mysql
docker run --name localmysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=r00t -d MySQL
and docker ps returns the ports as
33060/tcp, 0.0.0.0:3307->3306/tcp
When I go into MySQL shell and try to connect, I get an error. This is how I connect
\c -h localhost -P 3307 -u root -p r00t
If I am connecting wrongly, how should I connect in MySQL shell and should it work? Is there anything in my docker setup that would stop the connection working?

Accessing MySQL5.7 Docker container results in zsh: command not found: mysql

I am simply trying to run MySQL container using Docker and access it in my localhost.
This may be simple and easy to solve, but i am really struggling to find solution.
I have been googling around with the following searches, but i have no luck.
"zsh:command not found: mysql using Docker"
"docker, z shell problem"
"docker, zsh:command not found"
"etc..."
I am assuming this has something to do with z shell, but I have no clue how to fix this.
Here are the steps I did in my iTerm2 terminal.
Run MySQL 5.7 docker container
docker run -d -p 3306:3306 \
-e MYSQL_ALLOW_EMPTY_PASSWORD=true \
--name mysql \
mysql:5.7
Access MySQL to test, but failed
$ mysql -h127.0.0.1 -uroot
zsh: command not found: mysql
You should install mysql-client to use mysql to connect on your container.
Because your MySQL is installed in a docker container and not in local via brew, you can access your MySQL interface by running
docker exec -it nameofyourMySQLContainer mysql -u root -p
Enter your password if you've set one up for the container and then you're in šŸ‘

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

mySQL Docker "ERROR 2002 (HY000): Can't connect to local MySQL server through socket"

got a little script to pull up some mySQL server variants and test my scripts on different versions. My shellscript is:
docker pull mysql:$version
docker run -d -v $(pwd)/mysql.sh:/mysql.sh --name=mysql."$version" -e MYSQL_ROOT_PASSWORD='root' mysql:"$version"
docker exec -it mysql."$version" bash -c 'bash ./mysql.sh'
The mysql.sh file is:
/etc/init.d/mysql restart || service mysql restart || service mysqld restart
mysql -proot -e "SELECT ##version"
I get the following error:
./mysql.sh: line 1: /etc/init.d/mysql: No such file or directory
mysql: unrecognized service
mysqld: unrecognized service
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Anyone knows what's wrong?
docker does not use init.d structure for the services since it has no services. If you check inside the Dockerfile, you'll see that the mysql is started in foreground.
In your first script, the command docker exec -it mysql."$version" bash -c 'bash ./mysql.sh' is not needed. You should replace it with:
docker exec -it mysql."$version" mysql -proot -e "SELECT ##version"
The error indicates that /etc/init.d/mysql is not found. You need to change that to /usr/bin/mysql and it should work.

Docker 1.9.1 - ERROR 2005 (HY000): Unknown MySQL server host

We have a two docker setup, one is running a django app and the other running MySQL. Earlier we were using docker 1.8.3 and everything was working fine, after upgrading to 1.9.1 we are facing this issue.
We are creating a MySQL docker:
docker run --name <mysql docker name> -e MYSQL_ROOT_PASSWORD={} -d mysql:5.5.44
Next we are running a batchfile, which is nothing but creating a DB, granting access to the user
docker run -it --rm -v <setup_file>:/mnt mysql:5.5.44 sh -c 'exec mysql -h"<db server name>" -P"3306" -uroot -p"<password>" < /mnt/batchfile'
Next we are creating the django docker
docker run --name <django server name> --link <db server name> -it --rm ubuntu /bin/bash
When the django docker comes up we get the error message
ERROR 2005 (HY000): Unknown MySQL server host <DB server> (0)
Is there any additional parameter that we need to send for docker 1.9.1?