Unable to connect to Mysql hosted on Docker on Windows machine - mysql

I already executed mentioned here: Unable to connect to MYSQL from Docker Instance, but this time I'm running docker on windows machine.
pc#DESKTOP-NQ639DU MINGW64 /c/Program Files/Docker Toolbox
$ docker pull mysql/mysql-server
Using default tag: latest
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
pc#DESKTOP-NQ639DU MINGW64 /c/Program Files/Docker Toolbox
$ docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest
79ff1c03452ab2eac0d798b576ffeabde24d4c5aa6954d3d5c5bef99dcc40ce8
pc#DESKTOP-NQ639DU MINGW64 /c/Program Files/Docker Toolbox
$ mysql -uroot -ppassword
bash: mysql: command not found
pc#DESKTOP-NQ639DU MINGW64 /c/Program Files/Docker Toolbox
$ docker exec -it mysql bash
bash-4.2# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
bash-4.2# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
bash-4.2#

You should try to connect through the loopback interface, as you don't have access to the socket.
mysql -h 127.0.0.1 -uroot -p
In this case, it's like if your server is running on another machine, this only thing share with your host machine is the exposed port.

If you download the MySQL client from https://dev.mysql.com/downloads/windows/ then you'll be able to access the Docker-hosted MySQL in the same way you'd access any other MySQL database (of note without needing to get a root shell in the database container). Since you're using Docker Toolbox you'd probably use 192.168.99.100 as the IP address of the database server.

Try adding below in docker-compose.y\ml
services:
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: 'Dbname'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'username'
# You can use whatever password you like
MYSQL_PASSWORD: 'Password'
# Password for root access
MYSQL_ROOT_PASSWORD: 'RootPassword'
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3307:3306'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- my-db:/var/lib/mysql
volumes:
my-db:

Related

How to run a MySQL command terminal in Docker, with a docker-compose server

Let's say I have the following compose file :
networks:
my_network:
services:
...
mysql:
container_name: "mysql"
image: "mysql:5.7"
volumes:
- ./mysql.cnf:/etc/mysql/conf.d/mysql.cnf
environment:
MYSQL_ROOT_PASSWORD: "password"
MYSQL_USER: "user"
MYSQL_PASSWORD: "password"
MYSQL_DATABASE: "test-db"
ports:
- "3306:3306"
restart: always
networks:
- my_network
Once I run docker-compose up, my services get started and I can see that my MySQL server is ready to accept connections. Now what do I need to do to access the mysql terminal prompt from "outside" the container ? I remember seeing a teacher run another docker container (from a new terminal), and access the MySQL command prompt, enabling him to manage the database by hand, from another terminal window, but I can't remember the command exactly.
I tried running a new Docker container like this :
docker run -it --rm --name mysqlterm \
--network="compose_project_my_network" \
--link mysql mysql:5.7 \
sh -c 'exec mysql \
-h "localhost" -P 3306" \
-uroot \
-ppassword'
But unfortunatly, the container can't connect to the MySQL server, giving me the following error : ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2).
I guess the "localhost" or 3306 port are wrong ? What should I put in these parameters ?
Thanks in advance for your answers :)
You need an ordinary MySQL client; if your host's package manager (Debian/Ubuntu APT, MacOS Homebrew, ...) has a packaged named something like mysql-client, that's it.
When you run mysql, that tool does not interpret localhost as a host name but rather as a signal to use a Unix socket, so you need to use the corresponding IP address 127.0.0.1 instead. (If the database is running on a different system, use its DNS name or IP address: this is indistinguishable from running the database directly on the host outside of Docker.)
So from the host run:
mysql -h 127.0.0.1 -u root -p
The default port 3306 matches the published ports:, and you'll be prompted to interactively enter the password.

Is there any way to connect docker mysql from host?

I created mysql docker container
docker run -p 13306:3306 -d -e MYSQL_ROOT_PASSWORD="pass" -e MYSQL_DATABASE="db" --name mysql mysql:5.6.46
and I tried to connect to mysql
mysql -u root -p -h localhost -P 13306
but I can't connect to mysql.
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
I must connect like this for some reason. not use docker exec -i -t mysql bash
you should try and expand your question a bit and add some more information about the errors you receive.
In my local development environment in order to connect to my MySQL database I give it a static ip address. I tend to use docker-compose and not run it from the docker command directly for simplicity. This is what my docker-compose.yaml file looks like for a mariaDB container :
version: '3.7'
services:
maria:
container_name: maria
image: mariadb:10.4
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_USER=user
- MYSQL_PASSWORD=user_password
volumes:
- ./maria:/var/lib/mysql
networks:
static:
ipv4_address: 172.30.0.10
networks:
static:
ipam:
driver: default
config:
- subnet: 172.30.0.0/16
After running the docker-compose up command I can now connect to the mysql shell with the command
mysql -uuser -puser_password -h 172.30.0.10
if you are running linux you can also add a line to your /etc/hosts file like :
172.30.0.10 mysql
you can then connect with the command
mysql -uuser -puser_password -h mysql
I'm pretty confident you can get the same results with a pure docker command but it just seems easier with docker-compose. Anyway I hope ths helps

How to connect to mysql created by docker-compose

I can not connect to Mysql created with Docker in local environment (Mac OS X).
I have created the following configuration file.
version: '2'
services:
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: mysqldatabase
MYSQL_USER: mysql
MYSQL_PASSWORD: mysql
MYSQL_ROOT_PASSWORD: password
ports:
- "33333:3306"
container_name: mysql-db
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
driver: local
Then, I started the docker container of mysql with the following command.
$ docker-compose up -d
$ docker start mysql-db
Up to this point there was no problem, but an error occurred when trying to connect to mysql.
$ mysql -p 33333 -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Please tell me how to deal with it.
Try mysql -h 0.0.0.0 -P 33333 -u root -p.
Note port here has upper case P and password has lower case p.
Hope this helps!
I think if you connect from the host to the container. mysql will see the connection from non-local host, so disconnect it. You can change mysql config to allow other hosts, or connect from inside that container using docker exec -it mysql ...

Connect to remote MySQL db from docker container

I'm working to containerize a Django 1.5.x application that connects to a MySQL DB on a separate server via ODBC:
[mysql_default]
database = DB_NAME
driver = /usr/lib64/libmyodbc5.so
server = REMOTE_DB_SERVER
user = DB_USER
password = DB_USER_PWD
port = 3306
I'm able to run the Django app on my local machine (outside docker) with a connection to the remote DB via port forwarding & SSH:
ssh -L 3307:127.0.0.1:3306 MYID#REMOTE_DB_SERVER
I've set up a Docker container for the app using Centos 6.x, but can't get the MySQL connection working. The container has MySQL installed and the mysqld running.
My docker-compose.yml file looks like this:
version: "2"
services:
web:
build: .
image: MY_IMAGE
container_name: MY_CONTAINER
network_mode: "host"
ports:
- "3307:3306"
command: /bin/bash
With the container running, I can execute the following command (outside the container) to show databases on the remote DB:
docker exec MY_CONTAINER echo "show databases" | mysql -u DB_USER -pDB_USER_PWD -h 127.0.0.1 --port=3307
But from inside the container the same command fails:
echo "show databases" | mysql -u DB_USER -pDB_USER_PWD -h 127.0.0.1 --port=3306
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
The Docker works like a virtual machine. It has a local storage and a local environment. When you connect to 127.0.0.1 from the Docker it tries to connect to this Docker (not to local machine where the Docker was runned) because the localhost for the Docker is the Docker.
Please, read the following answer:
From inside of a Docker container, how do I connect to the localhost of the machine?
I solved this by using the docker host address instead of '127.0.0.1' for queries from within the container:
echo "show databases" | mysql -u DB_USER -pDB_USER_PWD -h 10.0.2.2 --port=3306
Because Docker host ip can vary, this post describes steps to get the right address:
How to get the IP address of the docker host from inside a docker container
I've created a bridge network:
$ docker network create -d bridge mynetwork
After, see the IP and port
$ docker inspect mysql
...
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "32031"
}
],
"33060/tcp": null
}
...
Connect with 0.0.0.0:32031
Solved by Basit

Installing MySQL in Docker fails with error message "Can't connect to local MySQL server through socket"

I'm trying to install mysql inside a docker container,Tried various images from github, it seems they all manage to successfully install the mysql but when I try to run the mysql it gives an error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
System specifications:
Ubuntu 12,04 its on AWS
Docker 0.10.0
Packages I tried so far:
https://github.com/eugeneware/docker-wordpress-nginx
https://github.com/tutumcloud/tutum-docker-mysql
Remember that you will need to connect to running docker container. So you probably want to use tcp instead of unix socket. Check output of docker ps command and look for running mysql containers. If you find one then use mysql command like this: mysql -h 127.0.0.1 -P <mysql_port> (you will find port in docker ps output).
If you can't find any running mysql container in docker ps output then try docker images to find mysql image name and try something like this to run it:
docker run -d -p 3306:3306 tutum/mysql where "tutum/mysql" is image name found in docker images.
I had the same problem, in fact, I juste forgot to run the service after installation ..
Start mysql server :
/etc/init.d/mysql start
Don't know how do i achieve this, but, i've be able to reach MYSQL by typing
$ mysql -u root -h
mywebsite:
image: benftwc/pldev-webserver
volumes:
- ./mywebsite.fr/:/var/www/
working_dir: /var/www/
ports:
- "8009:8009"
command: php -S 0.0.0.0:8009
links:
- database
database:
image: library/mysql
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3310:3306
root#422f4d1f454a:/# mysql -u root -h 127.0.0.1 -p3310
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
root#422f4d1f454a:/# mysql -u root -h database -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g...........
In my case I tried to connect to DB (which was inside docker) like this:
mysql -ppass -u root
but got same error as OP.
Specifying host and port helped:
mysql --host 0.0.0.0 --port 3306 -ppass -u root
I might be little late for answer and probably world knows about this now.
All you have to open your ports of docker container to access it. For example while running the container :
docker run --name mysql_container -e MYSQL_ROOT_PASSWORD=root -d
-p 3306:3306 mysql/mysql-server:5.7
This will allow your container's mysql to be accessible from the host machine. Later you can connect to it.
docker exec -it mysql_container mysql -u root -p
use ip address '127.0.0.1' instead hostname 'localhost'
If you don't have MySQL installed on your host, you have to execute it in the container (https://docs.docker.com/engine/reference/commandline/exec/#/examples gives explanation about docker run vs docker exec).
Considering your container is running, you might use
docker exec yourcontainername mysql -u root -p
to access to the client.
Also, if you are using Docker Compose, and you've declared a mysql db service named database, you can use :
docker-compose exec database mysql -u root -p
Assuming you're using docker-compose, where your docker-compose.yml file looks like:
version: '3.7'
services:
mysql_db_container:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: rootpassword
ports:
- 3307:3306
volumes:
- mysql_db_data_container:/var/lib/mysql
web:
image: ${DOCKER_IMAGE_NAME-eis}:latest
build:
context: .
links:
- mysql_db_container
ports:
- 4000:3000
command: ["./scripts/wait-for-it.sh", "mysql_db_container:3306", "--", "./scripts/start_web_server.sh"]
volumes:
- .:/opt/eis:cached
env_file:
- .env
volumes:
mysql_db_data_container:
Notice the ports definition for mysql_db_container
ports:
- 3307:3306
<= That indicates that mysql will be accessible via port 3307 to the localhost workstation and via port 3306 within the docker net
Run the following to see your container names:
$ dc config --services
mysql_db_container
web
In this case, we have two containers.
Errors
If you connect to mysql_db_container from your localhost workstation and try to access the mysql console there, you'll get that error:
docker-compose run mysql_db_container bash
root#8880ffe47962:/# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
root#8880ffe47962:/# exit
Also, if you try to connect from your local workstation, you'll also get that error:
$ mysql -u root -p -P 3307
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Solutions
Connecting from local workstation
Just add the --protocol=tcp parameter (otherwise mysql assumes you want to connect via the mysql socket):
$ mysql --protocol=tcp -u root -p -P 3307
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.21 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Connecting from web container
Reference the docker hostname -h mysql_db_container. Note that when you're running within the context of Docker that the TCP protocol is assumed.
$ dc run web bash
Starting eligibility-service_mysql_db_container_1_d625308b5a77 ... done
root#e7852ff02683:/opt/eis# mysql -h mysql_db_container -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 8.0.21 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
Connecting from mysql container
Assuming your mysql container name is eis_mysql_db_container_1_d625308b5a77 (that you can see when running docker ps), the following should work:
$ docker exec -it eis_mysql_db_container_1_d625308b5a77 bash
root#3738cf6eb3e9:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.21 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Specifying the host as 0.0.0.0 did work for me.
I created docker container using below command
docker run --detach --name=mysql --env="MYSQL_ROOT_PASSWORD=root" --publish 3306:3306 mysql
Then installed mysql client
sudo apt-get install mysql-client
Connected to mysql via terminal using below command
mysql --host 0.0.0.0 --port 3306 -proot -u root
Check out what's in your database.yml file. If you already have your plain Rails app and simply wrapping it with Docker, you should change (inside database.yml):
socket: /var/run/mysqld/mysqld.sock #just comment it out
to
host: db
where db is the name of my db-service from docker-compose.yml. And here's my docker-compose.yml:
version: '3'
services:
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
links:
- db
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root
You start your app in console (in app folder) as docker-compose up. Then WAIT 1 MINUTE (let your mysql service to completely load) until some new logs stop appearing in console. Usually the last line should be like
db_1 | 2017-12-24T12:25:20.397174Z 0 [Note] End of list of
non-natively partitioned tables
Then (in a new terminal window) apply:
docker-compose run web rake db:create
and then
docker-compose run web rake db:migrate
After you finish your work stop the loaded images with
docker-compose stop
Don't use docker-compose down here instead because if you do, you will erase your database content.
Next time when you want to resume your work apply:
docker-compose start
The rest of the things do exactly as explained here: https://docs.docker.com/compose/rails/
Months after this question, I've levelup my Docker skills. I should use Docker container name instead.
That use dokerized-nginx as bridge to expose ip+port of the container.
Within WEB configuration, I now use mysql://USERNAME:PASSWORD#docker_container_name/DB_NAME to access to Mysql socket through docker (also works with docker-compose, use compose-name instead of container one)
Try defining hostname like this
mysql -h mysql -u root -p
where
mysql -h <hostname(could be localhost,127.0.0.1,mysql)> -u root -p
My problem was that I was trying to connect from a version of mysql client that seems to be incompatible with the mysql server I installed (mysql:latest which installed version 8.0.22 at the time of this writing).
my mysql client version:
$ mysql --version
mysql Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using EditLine wrapper
The docker command that I used to install mysql:latest:
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=somerootpassword -e MYSQL_USER=someuser -e MYSQL_PASSWORD=someuserpassword -d -p 3306:3306 mysql:latest
The errors I got when connecting from my local mysql client to the mysql server:
$ mysql -u someuser -p -h 127.0.0.1
ERROR 2026 (HY000): SSL connection error: unknown error number
(sometimes I would get another error: "ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2". But I think this happens when I try to connect to the server too early after I started it)
My solution was to install mysql:5.7 instead:
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=somerootpassword -e MYSQL_USER=someuser -e MYSQL_PASSWORD=someuserpassword -d -p 3306:3306 mysql:5.7
and then I can connect to the server (after waiting perhaps 1 minute until the server is ready to accept connections):
$ mysql -u someuser -p -h 127.0.0.1
For me it was simply a matter of restarting the docker daemon..
I ran into the same issue today,
try running ur container with this command.
docker run --name mariadbtest -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mypass -d mariadb/server:10.3
Basically, it takes some time to start after pulling MySQL. Make sure you tried after a few minutes (maybe 2-3 minutes).
To connect to mysql from your machine use 127.0.0.1
To connect to mysql-container from inside another container you need to figure out the internal ip.
docker inspect mysqlcontainername | grep Gateway