Docker Compose - Creating and connecting to mysql container - mysql

Having a hard time trying to get to grips with mysql in docker. I have got the container running with docker-compose, but I am not able to connect to the database via any tools such as phpmyadmin, workbench or tableplus.
I have connected directly to the running container and run
mysql -uroot -p
and entered the root password which I have passed, but this fails with this error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Here is my docker-compose.yml file:
version: '3'
services:
db:
image: mysql
restart: always
environment:
MYSQL_DATABASE: quotes
MYSQL_USER: quotes
MYSQL_PASSWORD: P#KhzKZp)56sU8n+
MYSQL_ROOT_PASSWORD: +\VrLG*<t5sq[\\shR29u#n~A3^Jp*
ports:
- '3306:3306'
volumes:
- /private/mdbdata/quotes:/etc/mysql/conf.d
expose:
- '3306'
Been on this for days... hope someone can help!

I think your container is looking for a MySQL server on 'localhost', which WILL NOT WORK. 'localhost' to a container is the container itself - NOT the host machine it's running on.
You should be able to access the MySQL server using the host machine IP address.

Finally solved this one ... I had to remove all special characters from the password strings.
I tired adding single and double quotes are around the strings to see if that would allow the special characters, but that still failed. Passwords needed to be alphanumeric.

Have you tried the solution provided here? Basically, if you had a container running previously, you have to explicitly purge it since docker keeps track of root passwds (via volumes) from previously instantiated containers. This happens with a lot of tools, it's not unique to MySQL containers. A simple docker-compose rm -v should suffice, afterwards bring up your container. This basically deletes the old volume from the disk, removing all data from previous container instantiation.
You can also call a docker ps, find your container and execute docker rm -v <CONTAINER_NAME>. Bring up your container afterwards.

Related

How to connect to mysql docker container on a remote host from mysql workbench?

I've set up a docker container running a mysql instance on a remote computer I have. In the past this hasn't been an issue but for some reason I can't get it to work now. I am unsure what the issue might be. I am using docker compose and I can't seem to connect through mysql work bench on a different computer even those the container is running. Here are my details:
docker-compose.yaml
version: '3.7'
services:
api:
image: api
restart: unless-stopped
container_name: api
build: ./node/
ports:
- 3008:3008
mysql:
image: mysql
restart: unless-stopped
container_name: mysql
environment:
MYSQL_DATABASE: pitapaldb
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
build: ./database/
ports:
- 3306:3306
networks:
default:
external:
name: my-net
database/Dockerfile
FROM mysql
COPY init.sql /docker-entrypoint-initdb.d
database/init.sql
CREATE DATABASE mydb;
USE mydb;
SET SQL_SAFE_UPDATES = 0;
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'password';
flush privileges;
CREATE TABLE carts (
id int PRIMARY KEY AUTO_INCREMENT,
lat float,
lon float,
address varchar(255),
status boolean,
city_id int
);
container is definitely running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
784cf75183f4 mysql "docker-entrypoint.s…" 2 minutes ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
But when I try to connect via workbench I get 'unable to connect'. I've tried both username user and root with password password. The IP address I use definitely should work because I have other services operating from it with no issue:
#LoF10 Here is a quick list of things to check:
Can you connect within the docker network on the machine running the MySQL docker? An easy way of testing this is by running a command such as this on your remote machine:
docker run --rm -it --network my-net mysql:5.7 mysql -h mysql -uroot -ppassword
If not, there may be a problem with your MySQL config, MySQL data, or the initialization of the container. These are what #JorgeCampos is suggesting you verify. Since you are pulling directly from MySQL's Docker Hub entry, the config should be set properly to allow remote connections. If good, proceed. FYI, you will know you've connected successfully if you see mysql> on the terminal. To exit: \q.
Can you connect on exposed port on the localhost of the machine running the MySQL docker? An easy way of testing this is by running a command such as this on your remote machine:
docker run --rm -it --network host mysql:5.7 mysql -h 127.0.0.1 -uroot -ppassword
Make sure to use the IP used above and NOT localhost. This is b/c the MySQL client has special handling of the 'localhost' keyword by looking for mysqld locally. Using the 127.0.0.1 forces MySQL to connect via a proper socket connection. If you are not able to connect, then there is a problem with mapping the container's port to your host. If good, proceed.
Assuming both machines are on the same network and the machine that has MySQL Workbench also has docker, can you connect using the IP of the machine running MySQL container e.g. 10.0.0.4? An easy way of testing this is by running a command such as this on your remote machine:
docker run --rm -it mysql:5.7 mysql -h 10.0.0.4 -uroot -ppassword
If not, you may want to verify if you can:
Ping the 10.0.0.4 machine
If there are any firewall rules that prevent its proper exposure to the network. This happens commonly with Windows' default Firewall...
If on AWS, there are a number of reasons why you might not be able to reach if it has been properly assigned a Public Port e.g. Security Groups, Route Tables, Internet Gateway, etc.
Once you are able to proceed from 3 above, then you should be able to connect using MySQL Workbench as you've described.
Hope those help. Any more detailed recommendation will require you sharing more about your local networking setup (OS, Physical/Virtual, how you are determining IP's, etc).

Not able to login to mysql docker container with proper credentials

Here is my docker-compose file to create mysql container
Though, it really sounds a easy problem but I have tried all possible solutions and than posted it after nothing has worked.
The container is running successfully. Before this the mysql username and pass was different.
After changing I am having the issue.
I have totally removed the images I have removed all running
containers I have removed the volumes made
Still I am not able to login to the running container. Either using root username password or the created user and pass
mysql:
image: mysql5.6image(comes from ECR)
container_name: mysqlcont
restart:always
environment:
MYSQL_ROOT_PASSWORD: some_pass
MYSQL_DATABASE: databasename
MYSQL_USER: username
MYSQL_PASSWORD: some_pass
volumes:
- ./sqldata:/var/lib/mysql
Error Message:
Access denied for user 'root'#'localhost'(using password:yes)
It works only when:
I have to set new volume, every time I run docker-compose up else
the user that I am passing via environment is not working.

Locally run Docker container unable to make connection to remote MySQL database

I am trying to develop a micro service that gets information from a remote database but when I run the container it fails to make a connection to that database.
The container is running locally (I'm still developing it) and the database is hosted in AWS RDS Aurora MySQL.
The database is in use on multiple production websites using the same user I'm trying to use in the container. The user has full permission to the database and my local PHPMyAdmin connects to the database using that same user and I've had no trouble managing the db with it.
The problem is that the database connection in the container fails with an Access denied error.
The database user is setup as dbuser#% yet the error says:
Access denied for user 'dbuser'#'[my public ip]' (using password:
YES).
I attempted to add another account for dbuser#[my public ip] and gave it the same permissions as the wildcard host account and that makes no difference.
As another test I added a curl call inside the container to load an external page to make sure it can make external connections and that succeeds. It's just the db connection failure that makes no sense.
My dockerfile looks like this:
FROM php:7.2-apache
RUN apt-get update
RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN a2enmod rewrite
I'm hoping someone has come across this and/or knows what I'm missing here.
Thanks in advance!
Some more info:
I'm using Docker for Windows and docker-compose to run my container. My docker-compose.yml file looks like:
version: "3"
services:
web:
image: repository/container:latest
volumes:
- ./src:/var/www/html
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- inv
networks:
inv:
Starting the container with docker run --network host ... will make the container share the network stack of the host. That should solve the problem.
I ended up fixing this by copying the original connection code from my API and replacing the connection code in my container with it.
I commented out the broken code and compared the two but see no difference. No quote marks in the host string, no typos, nothing that makes sense.
The only difference is the way I'm setting the variables that build the host string. The values of those variables are the same so it really doesn't make sense.
Regardless, doing this fixed it.

Can't connect to mysql container from localhost

I'm trying to set up a MySQL container for developing.
So I used docker-compose to set it up.
The container and the mysql looks OK. The thing is that I want to connect to it from a DBeaver client and I can't find how I do it.
Here is my docker-compose.yml:
version: '2'
services:
db:
image: 'mysql:5.7'
volumes:
- '~/dev/dbs-data/mysql:/var/lib/mysql'
restart: 'always'
expose:
- '3306'
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: 'pass'
MYSQL_DATABASE: 'db'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'pass'
When I try to connect it from DBeaver I get:
java.sql.SQLException: null, message from server:
"Host '172.18.0.1' is not allowed to connect to this MySQL server"
UPDATE
I'm not trying to connect using the IP 172.18.0.1. I tried to connect using localhost:3306, 127.0.0.1:3306 and with the sub IP docker gave it 0.0.0.0:3306
UPDATE
After having success connecting on my Mac, I tried again with DBeaver on my linux and again:
Tried to connect with other tool, mysql workbench:
As you can see in the official image documention :
MYSQL_ROOT_HOST : By default, MySQL creates the 'root'#'localhost' account. This account can only be connected to from inside the container, requiring the use of the docker exec command as noted under Connect to MySQL from the MySQL Command Line Client. To allow connections from other hosts, set this environment variable. As an example, the value "172.17.0.1", which is the default Docker gateway IP, will allow connections from the Docker host machine.
So you have to set the MYSQL_ROOT_HOST variable with the address 172.18.0.1 that you can see in the error message.
On Docker, run this command to create a MySql container and it will expose the ports to the outside world of docker.
docker run --name <mysql-container-name> -p 3306:3306 -e MYSQL_ROOT_PASSWORD=<root-password> -e MYSQL_USER=root -e MYSQL_PASSWORD=<user-password> -d mysql:latest
Few points to note:
You may see below error when trying to connect with DBeaver:
Public Key Retrieval is not allowed
Solution: When creating a new connection on DBeaver, go to Driver Properties look for allowPublicKeyRetrievel and set it to TRUE. Also, if needed set useSSL to FALSE.
Test your connection from DBeaver or any other clients, and it should be working.
I am new to docker and was experiencing the same issue in Linux, it was an issue with the addresses allowed to accept connection; here is what worked out for me:
Find the MySql configuration file named mysqld.cnf
It would be: /etc/mysql/mysql.conf.d/mysqld.cnfOr if you have your own configuration file.
Edit the bind-address key in it. Set it as: bind-address = 0.0.0.0
This will allow to accept connections from any IP address Restart docker-compose by $ docker-compose down$ docker-compose up
Wait for MySQL to start, it should work fine now.

Access denied for mysql docker image in bamboo

I've got this configuration for a MySQL Docker image:
mysqldata:
image: busybox
volumes:
- /var/lib/mysql
mysql:
image: mysql:5.6
ports:
- "8203:3306"
volumes_from:
- mysqldata
environment:
- "MYSQL_ALLOW_EMPTY_PASSWORD=yes"
- "MYSQL_USER=root"
and I lift it up with docker-compose up -d mysql, and I can run commands against the DB in the image, access it without problems, etc.
Then I setup a docker image to be lifted in Bamboo:
But it fails when running commands against the DB in that image, with the error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
From the build logs of the test run I can see that the env variables are applied:
/usr/bin/docker run --workdir /data --detach --name mysql-user-service -p 8203:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -e MYSQL_USER=root mysql:5.6
But still no success.
Anyone knows how to overcome this problem?
Where are you connecting from? The host, another container?
Did you give permissions to the user#localhost MySQL user so it has access to the database? That error usually means the user you are connecting with doesn't have access to the db, and you need to grant the permissions.
If you do a docker ps you will see the name and the ID of the container you started for MySQL. Take that name or ID and run
docker exec -it <name or id> sh
That will get you into the container and you can run your MySQL commands to grant access something like this.
GRANT ALL ON *.* TO 'root'#'localhost
Since you are using compose you can do something like this as well
docker-compose run mysql GRANT ALL ON *.* TO 'root'#'localhost
You might also have to create the database of there isn't one already, and you would use the same steps just change the sql that is run