Connection refused to bitnami/mysql if user is not root - mysql

I have a mysql database and an app that is trying to connect to it. When I run docker-compose I get an error like this:
[error] failed to initialize database, got error dial tcp 192.168.128.3:3306: connect: connection refused
2023/01/14 19:52:53 Error when connecting to database: dial tcp 192.168.128.3:3306: connect: connection refused
It starts to work when I add line user: root to docker-compose, but I've read that's something I should avoid.
I tried running docker exec -it usermysql /bin/bash to try to connect to database from inside the container but it doesn't work as well (with user: root works fine).
I have no name!#usermysql:/$ mysql -u Slimo300 -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/bitnami/mysql/tmp/mysql.sock' (2)
Why it behaves like that?
How can I run mysql container as a nonroot?
Services definitions:
usermysql:
image: bitnami/mysql
container_name: usermysql
hostname: usermysql
user: root #<------- root
networks:
- chatnet
volumes:
- usermysql_data:/bitnami/mysql/data
environment:
- MYSQL_DATABASE=dbusers
- MYSQL_ROOT_PASSWORD=password123
- MYSQL_USER=Slimo300
- MYSQL_PASSWORD=password123
userservice:
build:
context: .
dockerfile: ./backend/user-service/Dockerfile
container_name: userservice
hostname: userservice
networks:
- chatnet
ports:
- "8083:8080"
depends_on:
- usermysql
restart: on-failure

Related

Why can't I run the mysql command in docker compose?

I have a project with a mysql database in a container. I use docker-compose to set my project up. And I want to run the mysql command to inspect te database.
So I did, and get:
docker-compose run --rm database mysql
Creating myproject_database_run ... done
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
However when I tried this it works:
docker exec -it myproject_database_1 mysql
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Can anybody explain me this?
My docker-compose file:
version: "3.7"
services:
database:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "127.0.0.1:3306:3306"
env_file: .env
volumes:
- type: volume
source: db_data
target: /var/lib/mysql
- type: bind
source: ./my.cnf
target: /etc/my.cnf
read_only: true
volumes:
db_data:
testing_images:
docker-compose run creates a new container. That's perfectly normal, but if your mysql client is configured to connect via a Unix socket, the new container will have a new filesystem and won't be able to see the main database container's /var/run directory.
When you use docker-compose run, you need to specify a TCP connection, using the setup described in Networking in Compose in the Docker documentation. For example,
docker-compose run --rm database \
mysql -h database
Since you publish ports: out of the container, you should be able to reach it from the host, without Docker. The trick here is that the mysql command-line client interprets localhost as a magic term to use a Unix socket and not a normal host name, so you need to specifically use the IP address 127.0.0.1 instead.
# From the same host, without anything Docker-specific
mysql -h 127.0.0.1
Try adding MYSQL_ROOT_PASSWORD in the environment.
environment:
MYSQL_ROOT_PASSWORD: root
This is from one of my working compose file
services:
## -----------------------------------------------
## MySql database
## -----------------------------------------------
db_mysql:
image: mysql:8.0
restart: always
volumes:
- db_mysql:/var/lib/mysql
- ./mysql:/docker-entrypoint-initdb.d
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- app-network
deploy:
mode: global
ports:
- "3306:3306"
## map volume
volumes:
db_mysql:
## in network, we can define any name under networks:
networks:
app-network:
driver: bridge
FYI: Official MySQL docker image - Docker Hub

Docker Compose: can't connect to my MySQL container

I'm getting the following error when I try to connect to my MySQL container from another container:
$ mysql -h127.0.0.1 -uroot
ERROR 2002 (HY000): Can't connect to MySQL server on '127.0.0.1' (115)
I can see that my container is running on port 3306:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a341fa5075c2 5961245d3135 "httpd-foreground" 3 minutes ago Up 3 minutes 127.0.0.1:8080->80/tcp sfcoding_cwj_1
e7683d546ad1 4da164162573 "docker-entrypoint.s…" 28 minutes ago Up 3 minutes 127.0.0.1:3306->3306/tcp sfcoding_mysql_1
Here is my docker-compose.yml:
---
version: '3.8'
services:
cwj:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/usr/src/app:cached
ports:
- "127.0.0.1:8080:80"
mysql:
image: mariadb:10.5.8
volumes:
- db_data:/var/lib/mariadb
ports:
- "127.0.0.1:3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
volumes:
db_data: {}
Any ideas as to why it won't connect?
Turns out I need to make my host match my service name, which is mysql.
mysql -h127.0.0.1 -uroot # does not work
mysql -hmysql -uroot # works

Airflow + MySQL in docker compose - Unknown host and Access denied

newbies for airflow and mysql settings here. I am trying to set up airflow in docker-compose with MySQL as backend.
I have my mySQL connection string as sql_alchemy_conn_cmd=mysql://localuser:localpassword#mock_mysql/metastore in airflow.cfg
I have my docker-compose.yml as
version: "3.9"
networks:
airflow:
services:
redis_local:
image: redis:latest
container_name: redis_server
ports:
- 6379:6379
command: redis-server
restart: on-failure
networks:
- airflow
mysql_local:
image: mysql:5.7
container_name: mysql_local
environment:
- MYSQL_ROOT_HOST=%
- MYSQL_USER=localuser
- MYSQL_PASSWORD=localpassword
- MYSQL_ROOT_PASSWORD=localpassword
- MYSQL_DATABASE=metastore
volumes:
- ./script/init.sql:/data/application/init.sql
- ./dbdata:/var/lib/mysql
ports:
- 3306:3306
restart: on-failure
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 1s
retries: 10
command:
--init-file /data/application/init.sql
--explicit_defaults_for_timestamp=1
networks:
- airflow
airflow_init_db:
depends_on:
- redis_local
- mysql_local
container_name: airflow_init_db
image: apache/airflow:2.0.0-python3.8
command: airflow initdb
since it's connecting to the metastore database, it will required to set up the database in mysql when the database is starting. i have my init.sql as
CREATE DATABASE IF NOT EXISTS metastore;
CREATE USER 'localuser'#'localhost' IDENTIFIED BY 'localpassword';
GRANT ALL PRIVILEGES ON metastore . * TO 'localuser'#'localhost';
FLUSH PRIVILEGES;
However from the healthcheck, mysql itself was failing with
mysql_local | 2021-07-06T04:29:04.011037Z 3 [Note] Access denied for user 'root'#'localhost' (using password: NO)
init db and scheduler both failed with
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (2005, "Unknown MySQL server host 'mock_mysql' (-2)")
I wasn't really sure what are missing at this point.
really appreciate any help!
Thanks
Two problems.
you should use mysql_local as hostname in your connection string not mock_mysql as this is the name of your MySQL container (and this.is the name defined in DNS by docker-compose)
the healthecheck you use assumes password-less authentication, and likely in default db image of MySQL it is.disabled for security. You should pass user/password as.parameters to your healthecheck command or enable root password-less authentication

mysqld: dial tcp 127.0.0.1:3306: connect: connection refused

I'm trying to dockerize monitoring of mysql using prom/mysqld-exporter and prom/prometheus. I have configured the docker-compose.yml file like the following:
version: '3'
services:
mysql:
image: mysql
container_name: mysql
restart: always
volumes:
- mysql:/var/lib/mysql
# command:
# - CREATE USER 'root'#'localhost' IDENTIFIED BY 'password' WITH MAX_USER_CONNECTIONS 3;
# - GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'root'#'localhost';
environment:
- MYSQL_ROOT_PASSWORD= password
- MYSQL_DATABASE= db #Defining a new Database
- MYSQL_USER= mostafa
- MYSQL_PASSWORD= ghadimi
ports:
- 3306:3306
- 33060:33060
prometheus:
image: prom/prometheus
container_name: prometheus
ports:
- 9090:9090
volumes:
- prometheus:/prometheus
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
command:
- --config.file=/etc/prometheus/prometheus.yml
mysql-exporter:
image: prom/mysqld-exporter
container_name: mysql-exporter
ports:
- 9104:9104
volumes:
- ./mysql-exporter/.my.cnf:/root/.my.cnf
depends_on:
- mysql
volumes:
mysql:
prometheus:
and here is my ./mysql-exporter/.my.cnf file:
[client]
user=mostafa
password=ghadimi
After running docker-compose up command, everything works properly except the following error!:
mysqld: dial tcp 127.0.0.1:3306: connect: connection refused
I don't know how can I fix it!
PS: I have also tried to connect to mysql through its container and docker exec -it <container-id> bash command and I have attempted all the possible passwords (like empty string and the password I have set in docker-compose file), but I face with another error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Mysql-d exporter is looking for mysql instance on localhost, but it is not running on the localhost inside mysqld container. You will have to pass external connection string with the host of mysql because this is the name of mysql service in docker-compose. From mysqld-exporter docs - you will have to pass environment variable to mysqld-exporter container with this datasource connection string :
mysql-exporter:
image: prom/mysqld-exporter
container_name: mysql-exporter
ports:
- 9104:9104
volumes:
- ./mysql-exporter/.my.cnf:/root/.my.cnf
environment:
- DATA_SOURCE_NAME="user:password#mysql:3306/database"
depends_on:
- mysql
And change user, password and database name to your desired ones.

Docker MySQL: Connecting a Rails app to MySQL

I have a rails app that I'm running in Docker. I want to run MySQL in a separate container and connect the two.
My docker-compose.yml file looks like this:
# docker-compose.yml
db:
image: "mysql:5.6"
ports:
- 3306
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: dbname
MYSQL_USER: user
MYSQL_PASSWORD: pass
web:
build: .
ports:
- "80:80"
env_file:
- .env.development
links:
- db
volumes:
- "/webapp:/home/app/webapp"
When I run docker-machine ip default I get 192.168.99.100.
When I run docker ps I see mysql is running on PORT: 3306/tcp, 0.0.0.0:32782->32781/tcp Edit: After removing the mysql container and restarting it, the port is actually 0.0.0.0:32784->3306/tcp
I'm using the Sequel gem, and using the following params to connect to my db:
Sequel::Model.db = Sequel.connect(adapter: 'mysql2',
database: 'dbname',
user: 'user',
password: 'pass',
host: '192.168.99.100',
port: '3306',
loggers: [logger] )
When I run my app, I get:
rake aborted!
Sequel::DatabaseConnectionError: Mysql2::Error: Can't connect to MySQL server on '192.168.99.100' (111)
/var/lib/gems/2.0.0/gems/mysql2-0.3.18/lib/mysql2/client.rb:70:in `connect'
/var/lib/gems/2.0.0/gems/mysql2-0.3.18/lib/mysql2/client.rb:70:in `initialize'
/var/lib/gems/2.0.0/gems/sequel-4.23.0/lib/sequel/adapters/mysql2.rb:36:in `new'
/var/lib/gems/2.0.0/gems/sequel-4.23.0/lib/sequel/adapters/mysql2.rb:36:in `connect'
/var/lib/gems/2.0.0/gems/sequel-4.23.0/lib/sequel/connection_pool.rb:101:in `make_new'
// Lots more traces
Any ideas what I'm doing wrong here?
Try using the link name as "host" in your connection settings. Change the IP address to "db".