Grant User Privileges on MySQL Service in GitHub Actions - mysql

In GitHub Actions, I have defined a MySQL service like this:
env:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_DATABASE: localdb
services:
mysql:
image: mysql/mysql-server:5.7
ports:
- 3306:3306
Now, when I connect to this service I get:
ERROR 1130 (HY000): Host '172.18.0.1' is not allowed to connect to this MySQL server
When I installed the same service locally using Docker, I solved the very same error with this code:
$ docker exec -it mysqldb bash# mysql -h localhost -u root -p
mysql> create user 'root'#'%' identified WITH mysql_native_password by '';
mysql> grant all privileges on *.* to 'root'#'%' with grant option;
Yet I have no idea how I would do the same inside the CI pipeline, since connecting to the server to execute queries already throws the above error.
How do I configure the MySQL server to accept connections?

The service probably just had the wrong image, this works:
services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: localdb
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

Related

Cannot access Mysql DB as root using phpmyadmin

I have this docker-compose file:
version: "3"
services:
db:
image: mysql/mysql-server:5.7
container_name: mysqldb
restart: always
environment:
MYSQL_ROOT_PASSWORD: 4dfLtRah2C
MYSQL_DATABASE: db
MYSQL_PASSWORD: le562BplPk
MYSQL_USER: db_user
networks:
- backend
volumes:
- ./db_data:/var/lib/mysql
phpmyadmin:
container_name: phpmyadmin
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '9006:80'
environment:
MYSQL_ROOT_PASSWORD: 4dfLtRah2C
PMA_PORT: 3306
PMA_HOST: db
networks:
- backend
networks:
backend:
When I try to use the db_user with the MYSQL_PASSWORD it works, but when I try to login as the root user and use the MYSQL_ROOT_PASSWORD it does not work, gives me a connection refused error.
I can login in to the db via the container using mysql -u root --password=$MYSQL_ROOT_PASSWORD and it works just fine...
Already tried to do docker system prune -f -a --volumes as well as removing the db_data folder multiple times to ensure that the container runs as a fresh instance.
How may I fix this? What is wrong?
The problem here was that phpmyadmin was trying to use root#localhost, but we want to use root#db, to fix this we simply need to perform a few modifications on the database, in order to change the host of the user root to %, like this:
(inside the db container: mysql -u root -p)
use mysql;
UPDATE mysql.user SET host='%' WHERE host='localhost' AND user='root';
FLUSH privileges;
exit;
Now I can use phpmyadmin to manage all the databases using root.
Please note that you should not abuse the root user use, instead try to use the dedicated user for your specific database, but as this is a development environment and everything is containerized no harm should come from using root.

Grant Privileges to Root via Dockerfile

Is there some way to grant all privileges to root via Dockerfile ou docker-compose.yml?
I'm trying to boot up a mysql:latest image like this:
docker-compose.yml
version: "3.9"
services:
mysql_database:
container_name: mysql_test_server
restart: always
build: .
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
ports:
- 3306:3306
volumes:
- ./data:/var/lib/mysql
- ./config:/etc/mysql/conf.d
environment:
MYSQL_ROOT_PASSWORD: root
dockerfile (the build: . on .yml)
FROM mysql:latest
RUN mysql -u root -proot -h localhost -p 3306
RUN grant all privileges on *.* to 'root'#'%' identified by 'root';
RUN flush all privileges;
RUN exit;
EXPOSE 3306
TLDR; I just want to grant privileges for remote connections (SQLYog, DBeaver, etc) automatically... if docker-compose up, run this command so I don't need to do manually, like docker exet -it mysql_test_server mysql -u root ...
I awalys get this:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Your MySQL server is not running in this docker file, so you are getting errors.
The default docker image has root user and root user has all privileges.
You have to use environment variables to achieve what you try to do:
Run this command:
docker run --name some-mysql \
-v /my/own/datadir:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=rootPassword \
-e MYSQL_ROOT_HOST="%" \
-p 3306:3306 \
-d mysql:tag
You have to replace paths in the -v parameter.
Then you can login to MySQL with command:
mysql -uroot -prootPassword -h127.0.0.1
The parameter you should take a look is MYSQL_ROOT_HOST. That variable is used in the MySQL container entrypoint: https://github.com/docker-library/mysql/blob/master/8.0/docker-entrypoint.sh#L232-L239

Unable to connect to MySQL database in docker container

I'm honestly tired of this. I've tried every possible solution but it still refuses to connect.
Here is my docker-compose file:
version: '3'
services:
# Database
db:
image: mysql:5.7
container_name: db
restart: always
env_file: .env
environment:
- MYSQL_ROOT_HOST=%
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=$MYSQL_DATABASE
- MYSQL_USER=$MYSQL_USER
- MYSQL_PASSWORD=$MYSQL_PASSWORD
networks:
- backend
# Wordpress
wp:
depends_on:
- db
image: wordpress:php7.3-fpm
container_name: wordpress
restart: always
env_file: .env
environment:
- WORDPRESS_DB_HOST=db:3306
- WORDPRESS_DB_USER=$MYSQL_USER
- WORDPRESS_DB_PASSWORD=$MYSQL_PASSWORD
- WORDPRESS_DB_NAME=$MYSQL_DATABASE
volumes:
- ./wordpress:/var/www/html
networks:
- backend
# Nginx
nginx:
depends_on:
- wp
image: nginx
restart: always
ports:
- "80:80"
volumes:
- ./wordpress:/var/www/html
- ./nginx-conf:/etc/nginx/conf.d
networks:
- backend
networks:
backend:
driver: bridge
I've tried 127.0.0.1, 0.0.0.0, db, docker inspect db to get ip address of the container. All of them fail to connect. I've used Sequel Pro, MySQL Workbench and DataGrip.
The setup works completely fine. Its just that I cant connect to the database outside the container.
I even checked the mysql host privileges in the container and got:
% root
% wordpress (name of the user I created)
...
Am I missing something?
In order to reach your database from the host machine with an sql client you need to map the MYSQL database port to the host machine.
In your Compose file add the port mapping to your db service.
db:
...
ports:
- "3306:3306"
I believe you are using the default port based on your Wordpress service.
Then configure your SQL client to 127.0.0.1 and port 3306.
MYSQL Changes
MY.ini changes in c:\programdata\mysql\mysql server 8.0
Add Bind_address=0.0.0.0
Save as Admin - Check timestam
Create one App user - Dont user mysql Root user for app
drop user 'username'#localhost;
FLUSH PRIVILEGES;
CREATE USER 'username'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'localhost' WITH GRANT OPTION;
CREATE USER 'username'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Restart the MYSQL Server
Restart Docker for Windows
Check Hosts files in C:\Windows\System32\drivers\etc if host.docker.internal variable is updated correctly
# Added by Docker Desktop
10.0.0.6 host.docker.internal
10.0.0.6 gateway.docker.internal
Application Changes:
User host.docker.internal in JDBC URL in app.properties
spring.datasource.url=jdbc:mysql://host.docker.internal:3306/test
spring.datasource.username=username
spring.datasource.password=password
maven install should work
docker run <repo/repo>:image should work fine. now

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 rails mysql is not connecting

I am trying to connect my rails app which is on my host to docker mysql image. But I am getting this error:
Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/Cellar/mysql/5.7.22/lib/plugin/caching_sha2_password.so, 2): image not found
My Docker compose file is like this:
db:
image: mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: password
adminer:
image: adminer
restart: always
ports:
- 8080:8080
I am using this inside my database.yml:
default: &default
adapter: mysql2
encoding: utf8
host: 127.0.0.1
username: root
password: password
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
# socket: /Applications/MAMP/tmp/mysql/mysql.sock
development:
<<: *default
database: meal_plan_development
What else I should do in order to connect my rails app to mysql docker image.
MySQL default-authentication-plugin
as of version 8, MySQL uses caching_sha2_password as the default authentication plugin. You can override it to use mysql_native_password by adding a command instruction in your docker-compose.yml file like this:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: password
adminer:
image: adminer
restart: always
ports:
- 8080:8080
As #vstm pointed out, this seems to be the same problem I was having with a PHP client. After the container has been created you could try changing the authentication scheme to one which will likely be supported e.g.
docker exec <container_id> /bin/sh -c "mysql -uroot -ppassword
-e ALTER USER root#localhost IDENTIFIED WITH mysql_native_password BY 'PASSWORD'"
I'm not overly familiar with Docker, but I believe you can also add something like this to a Dockerfile so that the authentication method will be changed during the container initialization:
RUN /bin/bash -c "mysql -uroot -ppassword
-e ALTER USER root#localhost IDENTIFIED WITH mysql_native_password BY 'PASSWORD'"