When using a dev container in Visual Studio Code (VSC) I can't connect to the MySQL database, not even from inside the container. However, when I manually start the service with docker-compose up -d mysql I can connect to the database from within the container.
I'm using the Bitnami MySQL from the Docker Hub registry.
This is the content of my .devcontainer.json:
{
"name": "Devcontainer",
"dockerComposeFile": "docker-compose.yml",
"service": "devcontainer",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}"
}
Kept is as basic as possible.
The contents of the docker-compose.yml file:
version: '3.8'
services:
devcontainer:
image: mcr.microsoft.com/devcontainers/base:ubuntu
volumes:
- ../..:/workspaces:cached
command: sleep infinity
mysql:
image: docker.io/bitnami/mysql:8.0
ports:
- 3306:3306
expose:
- 3306
volumes:
- 'mysql_data:/bitnami/mysql/data'
environment:
- ALLOW_EMPTY_PASSWORD=no
- MYSQL_ROOT_PASSWORD=deOCyZ4cnRa4bRNDAFak8
- MYSQL_USER=user
- MYSQL_PASSWORD=deOCyZ4cnRa4bRNDAFak8
- MYSQL_DATABASE=name
healthcheck:
test: ['CMD', '/opt/bitnami/scripts/mysql/healthcheck.sh']
interval: 15s
timeout: 5s
retries: 6
volumes:
mysql_data:
driver: local
So when I do a Rebuild without cache and reopen in container in VSC it starts up the docker container. After that I enter the container using docker exec -it <hash> bash and try to login to the server:
$ mysql -u user -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/bitnami/mysql/tmp/mysql.sock' (2)
When I close the remote connection and make sure the container is not running anymore I start the container with docker-compose up -d db.
When it's running I start bash inside the container the same way and then I can connect to the server:
$ mysql -u user -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.31 Source distribution
I'm 100% sure I supply the correct password with copy/paste.
The same connection problems also exists for root user. I've also tried to use the ALLOW_EMPTY_PASSWORD=true but that has the same result.
I've tried to use MariaDB version supplied by Bitnami but is has the same problem. I haven't tried the original MySQL image but I never had this problem before.
No other docker containers are running when I start the dev container.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$
Any idea what's happening and how to fix it? Am I doing something wrong?
Update: It looks like the original MySQL images from the Docker Hub are working properly.
Related
I created grafana with a docker container. And I created mysql with docker container. After that, I tried to access grafana and add mysql container via 'data source'. However, the following error occurred. What is the problem?
db query error: query failed - please inspect Grafana server log for details
[grafana's data sources setting]
Host : 10.0.7.35:3306
And I set bind-address at '/etc/mysql/my.cnf' for 0.0.0.0:3306 in mysql container.
You can do it in three ways
You can run docker with MySQL and expose 3306 (by adding -p 3306:3306 port and find IP of docker and connect to that port on that IP from the same network
Expose MySQL port and then run app docker with --link name_of_mysql_container:mysql and then on the app docker just connect to name_of_mysql_container
Use Docker composer create
docker-composer.yml with content similar to:
version: '3.2'
services:
app:
build: .
image: lap
environment:
- DB_HOST=mysql
mysql:
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
And then you can use in your app container database connection to host named 'mysql'
I am running a mysql server inside a docker container using docker-compose, here is my yaml file:
version: '3'
services:
mysqltest:
image: mysql
network_mode: host
ports:
- "3306:3306"
volumes:
- "/home/myuser/bds/mysql/:/var/lib/mysql"
user: "1000:1000"
environment:
- "MYSQL_ROOT_PASSWORD=mysecret"
The container loads file with the docker-compose up command, but when i try to connect from the host machine to the mysql server with user root, it fails:
mysql -h 127.0.0.1 -u root -pmysecret
ERROR 1045 (28000): Access denied for user 'root'#'127.0.0.1' (using password: YES)
However, i can connect to the server inside the container using the same command, if i start the container using docker command line:
docker run -it --rm --name mysqltest --user 1000:1000 -v /home/myuser/bds/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysecret -p 3306:3306 -d mysql
I tried setting network_mode to host, none and also without specifying it, but the result is the same using docker-compose.
What could be wrong with my YAML file so that i cannot connect as when i use docker command ?
Thanks in advance
I assume you didn't change your default Docker configuration. By default Docker will run in the network mode: 'bridge' and not host. See the difference here.
You can check using docker inspect container.
When you just start the container using the command you will start your container in bridge mode and the container port 3306 will be mapped on 3306. This will not happen when you try host. Again see the link for the difference.
So update your docker-compose.yaml and define bridge as network mode:
version: '3'
services:
mysqltest:
image: mysql
network_mode: bridge
ports:
- "3306:3306"
volumes:
- "/home/myuser/bds/mysql/:/var/lib/mysql"
user: "1000:1000"
environment:
- "MYSQL_ROOT_PASSWORD=mysecret"
SOLVED !! The issue was originated from setting different mysql root passwords with the different docker commands:
The first time i was testing the container i user std docker command and the environment MYSQL_ROOT_PASSWORD=othersecret. Everything worked fine. i connected from host using mysql command and restored a database
Once i finished testing, i wrote yaml file, but with the root password i wanted to use for producction:
environment:
MYSQL_ROOT_PASSWORD: "mysecret"
But then, when i started the container with docker-compose and the yaml file, i could not connect from host using mysql command. It seems the original root password ("othersecret") was written to the user table inside the mysql schema and that is why i could not connect using the password set in the yaml file. I cleaned up the mysql directory and started the container using docker-compose (without specifying a network mode) and finally i was able to connect from host.
I'm running a Docker mysql container on my Mac laptop. Previously I was able to connect to it from the host OS with the mysql client. However, somehow it got deleted, and now after I re-created it, I can no longer to connect to it. I've searched dozens of similar questions, but am completely stumped.
Here's how I created it:
docker container run --name mysql-zhxw-dev -p 3306:3306 --expose=3306 -v zhxw-local-db-:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql:5.7.30
Every time I run mysql -u root -h 127.0.0.1 the following from my host OS, I get:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (61)
I can login to the container, and connect to mysql from within:
docker container exec -it mysql-zhxw-dev bash
mysql -u root <-- connects fine
I've tried:
Omitting the named volume
Specifying a password
Various versions of mysql, including 5.6 and 5.7
Logging in to the container with docker container exec, installing vi, editing /etc/mysql/mysql.conf.d/mysqld.cnf and uncommenting the line that contains bind-address. I tried it with both bind-address = 0.0.0.0 and bind-address = 127.0.0.1, then obviously exiting and running docker container restart mysql-zhxw-dev.
Specifying port to connect to with -P 3306
Connecting with -h localhost, -h 127.0.0.1, -h 0.0.0.0, and omitting the -h
Specifying --protocol=TCP when connecting
I'm at a loss as to what else to try.
i have a template in docker-compose with mysql, maybe it can help you.
docker-compose.yml
version: "3.2"
services:
mysql:
image: mysql:latest
ports:
- "3306:3306"
volumes:
- /path-persistent-volumen:/var/lib/mysql/
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
It turns out docker must have been in a strange state. Rebooting my laptop solved the problem.
Before rebooting, I tried restarting Docker Desktop, and that did not fix it. Only a full reboot resolved it.
One thing that I did notice was before the reboot, when I ran docker container ls -a, there were no containers, apart from the one mysql one I was trying to get working. I thought I had perhaps pruned them from some cleanup command. After the reboot, all my containers came back.
I did recently upgrade docker using Homebrew, so perhaps that put it in a weird state.
This error generally occurs due to problems related to port on the host.
Try these things:
Check logs of the container
Start the container in attached mode using -a flag
Run docker inspect mysql-zhxw-dev and check HostPort and HostIp and try to find something anomalous.
You can also change the host port in port mapping to something else like -v 3308:3306.
Also, this might help https://stackoverflow.com/a/32361238/9586997
P.S. I have copied and run the same command you have given, and it is running fine.
I had this issue after modifying the docker images and container configuration. Turns out the local copy of the MySQL data instance was corrupt.
Removing the ./data directory noted in this compose file and rebuilding worked.
The compose file
# /docker/docker-compose.yml
---
services:
db:
container_name: 'wp-mysql'
image: 'mysql:5.7.37'
platform: linux/amd64
volumes:
- './data/mysql:/var/lib/mysql'
ports:
- "18766:3306"
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress_db
MYSQL_USER: wordpress_user
MYSQL_PASSWORD: wordpress_password
The rebuild command for docker
docker-compose -f "docker-compose.yml" up -d --build
I'm trying to create mySQL container for development DB.
My app is made by golang.
Now, I created docker-compose.yml to run APP & DB.
mySQL container was created and I can start it, but Access denied shows up if when I tried to connect with this DB, via DB client (Sequel Pro).
it's my docker-compose.yml
version: '3'
services: app:
build:
context: .
dockerfile: "Dockerfile"
ports:
- "8080:8080"
volumes: mysql:
image: mysql:5.7.10
environment:
MYSQL_ROOT_PASSWORD: 'pass'
ports:
- '3306:3306'
volumes:
- mysql-data:/var/lib/mysql volumes: mysql-data:
driver: local
I did
$ docker-compose build
and
$ docker-compose up -d
Then containers are created, mySQL is as below, if exec command
$ docker ps -a
1f7540fdedc1 mysql:5.7.10 "/entrypoint.sh mysq…" 1 second ago Up 2 seconds 0.0.0.0:3306->3306/tcp XXX_mysql_1
After that,If I tried to connect this DB with sequel pro, I can see this message in kitematic.
Access denied for user 'root'#'172.18.0.1' (using password: YES)
ip, user name, password, port must be correct.
Also, if I create docker container for mySQL with using kitematic, I can connect with the container.
I don't know why access denied shows up.
Screenshot when access denied shows up
I have this similar problem fixed. Make sure you have the latest image of mysql version. docker pull mysql:5.7.10.
Then delete the associated volume (backup if there is existing data). If you are using the docker-compose yml version 3 you should map your environment like MYSQL_ROOT_PASSWORD=password. Verify that it is correctly set by going to the container and run command echo $MYSQL_ROOT_PASSWORD.
If you are logging in from the host machine try to pass the protocol flag.
mysql -u root -p --protocol=tcp (you may also pass the host localhost or 127.0.0.1) -h 127.0.0.1
I have a little problem, I can't seem to connect to mysql server community edition running inside a docker container.
I can easily connect to mysql server using the cli by using:
docker exec -it mysqlserver mysql -uroot -p
But if I try to connect with any other database connector, like DataGrip or MySql Workbench, I get an access denied.
But I changed nothing in the configuration files. I set ip as localhost, using the default 3306 port that the container exposed. username I keep as root and the password is exactly the same but it still keeps failing.
Am I missing something, not understanding anything properly?
Some help would be greatly appreciated!
Extra info: I am using MacOS, running the container with Docker for Mac and I am using as of this moment the latest MySql database version.
your container doesn't contain other connectors, try to publish port when you run your container with docker run --name mysql -p 127.0.0.1:3306:3306 .... to link port. And you can connect you to the container with your local cli. Try this doc
Below is the docker-compose.yml that I used to start up a MariaDB instance for testing some queries. MariaDB is API-compatible with MySQL, so no difference.
version: "2"
services:
db:
image: bitnami/mariadb:latest
volumes:
- ./mariadb/data:/bitnami
ports:
- "9001:3306"
environment:
MARIADB_ROOT_PASSWORD: ChangeMeIfYouWant
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- "9010:80"
links:
- db:db
The image is downloaded and the container created and started via docker-compose up. Afterwards, I can easily connect to it using JetBrains DataGrip.
host: localhost
port: 9001
user: root
password: ChangeMeIfYouWant