Grant Privileges to Root via Dockerfile - mysql

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

Related

Can't change credentials (root, neither other user) via environment variables using docker compose command

This is how I start MySQL docker container from docker-compose.yml:
version: '3.9'
services:
mysql_db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_USER: my_user_1
MYSQL_PASSWORD: pwd_1
restart: always
volumes:
- db_vol:/var/lib/mysql
ports:
- "3306:3306"
- "33060:33060"
volumes:
db_vol:
The password for root is NOT changed => the default pwd: "root" for user root works.
Leave alone creating the other MYSQL_USER: user_1 with his password.
I can view the server status via for instance MySQL Workbench using default creds: root/root
The server status is green - running and when I click the Users and Privileged pane, this is what I see:
So there is another user1, instead of my_user_1 which I have created.
HOWEVER: When I run the container from cmd line like this:
docker run -d --rm -p 3306:3306 -p 33060:33060 -e
MYSQL_ROOT_PASSWORD=root123 --name=mysql_db mysql:latest
This also works even for user deli1
docker run -d --rm -p 3306:3306 -p 33060:33060 -e
MYSQL_ROOT_PASSWORD=root123 -e MYSQL_USER=deli1 -e
MYSQL_PASSWORD=deli_pwd --name=mysql_db mysql:latest
I do not want to use config files (shared via docker volumes to the container) to override default parameters of MySQL container. Instead, I'd like to use handy environment variables, which I read all around that works nice.. What could be I doing wrong ?
Ubuntu 22.04.1 using VSCode Version: 1.75.1
Date: 2023-02-08T21:35:30.018Z
Electron: 19.1.9
Chromium: 102.0.5005.194
Node.js: 16.14.2
V8: 10.2.154.23-electron.0
OS: Linux x64 5.15.0-60-generic
Sandboxed: No

Grant User Privileges on MySQL Service in GitHub Actions

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

docker-compose drupal mysql SQLSTATE[HY000] [2002] No such file or directory

I have tried all the solutions available on all forms but no success even tried using accepted answers docker-compose file but still getting the same error.
I am trying this on a freshly installed ubuntu 18 on VirtualBox. I have tried with all possible combinations of localhost, 127.0.0.1, changing user, etc.
Docker version 19.03.6, build 369ce74a3c
docker-compose version 1.25.4, build 8d51620a
docker-compose.yaml
version: '3.1'
services:
drupal:
image: drupal:8
container_name: drupal_container
environment:
DRUPAL_PROFILE: standard
DRUPAL_SITE_NAME: Drupal
DRUPAL_USER: user
DRUPAL_PASS: pass
DRUPAL_DBURL: mysql://user:pass#database:3306/db
#or mysql://user:pass#mysql_container:3306/db tried with both
links:
- "mysql:mysql"
depends_on:
- mysql
ports:
- 80:80
restart: always
volumes:
- /var/www/html/modules
- /var/www/html/profiles
- /var/www/html/themes
- /var/www/html/sites
restart: always
mysql:
image: mysql:5.7
container_name: mysql_container
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: db
MYSQL_USER: user
MYSQL_PASSWORD: pass
ports:
- 3306:3306
expose:
- 3306
I am able to connect MySQL via command line and the docker exec command
sudo mysql --protocol=TCP -h127.0.0.1 -p3306 -uroot -proot
sudo mysql --protocol=TCP -h127.0.0.1 -p3306 -uuser -ppass
sudo mysql --protocol=TCP -hlocalhost -p3306 -uroot -proot
sudo mysql --protocol=TCP -hlocalhost -p3306 -uuser -ppass
docker-compose exec mysql mysql -uroot -proot -e "Show databases;"
docker-compose exec mysql mysql -uuser -ppass -e "Show databases;"
How can I fix this?
it worked, not the way I expected but it did.
sudo docker inspect mysql_container
op->
"Gateway": "172.22.0.1",
"IPAddress": "172.22.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:16:00:02",
"DriverOpts": null
I tried with this IP add ie "IPAddress": "172.22.0.2"
drupal and below commands are able to connect
sudo mysql --protocol=TCP -h172.22.0.2 -p3306 -uroot -proot
sudo mysql --protocol=TCP -h172.22.0.2 -p3306 -uuser -ppass
but still would like to know what is the significance of this IP address, how is this different from localhost or 127.0.0.1 and will this remain the same every time I pull the same image on different machines or it changs??
I've attached the working compose file, you seem to be missing network through which the dockers should be connected.
Create the shared network for your containers
docker network create shared
version: '3.1'
networks:
shared:
external:
name: shared
services:
drupal:
image: drupal:8
container_name: drupal_container
environment:
DRUPAL_PROFILE: standard
DRUPAL_SITE_NAME: Drupal
DRUPAL_USER: user
DRUPAL_PASS: pass
DRUPAL_DBURL: mysql://user:pass#mysql:3306/db # MySQL host should be same as service name created for mysql.
#or mysql://user:pass#mysql_container:3306/db tried with both
links:
- "mysql:mysql"
depends_on:
- mysql
ports:
- 80:80
restart: always
volumes:
- /var/www/html/modules
- /var/www/html/profiles
- /var/www/html/themes
- /var/www/html/sites
networks:
- shared
mysql:
image: mysql:5.7
container_name: mysql_container
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: db
MYSQL_USER: user
MYSQL_PASSWORD: pass
ports:
- 3306:3306
expose:
- 3306
networks:
- shared
When you try to setup the database in drupal use the following value for host, rest should be same as what you have been using.
Host: mysql
Also, you could just change the 'DRUPAL_DBURL' value in your compose file. This way you are using the default network created by docker compose.
DRUPAL_DBURL: mysql://user:pass#mysql:3306/db

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

docker compose yaml - command

I want to run a creation of a new database using mysql
this is the snippet I have in my docker-compose.yml file
mysql:
image: mysql
container_name: mysql-machine
ports:
- 3306:3306
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_DATABASE: mxdb
MYSQL_USER: mxdb
MYSQL_PASSWORD: mxdb
command: mysqladmin create testing_db
Now when i run docker-compose up
I watch the console, and it says
mysql_1 | mysqladmin: connect to server at 'localhost' failed
mysql_1 | error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
mysql_1 | Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!
So how do I re-write the command piece, so i get the service working properly?
I want to create more than one database. So manually using commands is the easiest way.
I think you should run the mysql container and then have another container do the data import. Example:
.credentials
MYSQL_ALLOW_EMPTY_PASSWORD=true
MYSQL_USER=mxdb
MYSQL_PASSWORD=mxdb
MYSQL_DATABASE=mxdb
Run the container:
docker run --name mydb -d --env-file .credentials mysql
If you want to import data from file, create a new container, link to the one that is already running and do the import:
docker run --rm -t --link mydb:DB -v /path/to/dump.sql:/dump.sql mysql bash -c "mysql -h DB -u mxdb -pmxdb mxdb < /dump.sql"
If you just want to run a command, use:
docker run --rm -t --link mydb:DB mysql mysql -h DB -u mxdb -pmxdb -e "CREATE DATABASE bar"
or
docker exec -t mydb mysql -u mxdb -pmxdb -e "CREATE DATABASE bar"
I don't think you should override the command to create the database.
In docker-compose, the command should be the command to start the given service in the docker image. In your case, the service is a MySQL server. If you gives a command for the mysql service in your docker_compose.yml, the MySQL server will never start.
What you should do is start the mysql service, and then run commands in it.
mysql:
image: mysql
container_name: mysql-machine
ports:
- 3306:3306
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_DATABASE: mxdb
MYSQL_USER: mxdb
MYSQL_PASSWORD: mxdb
Start the service:
docker-compose up
Connect to it:
mysql -umxdb -pmxdb
Then create the database:
create database testing_db;
If you need to automatize this database creation, you could put these SQL commands in a file, and do when needed:
cat init_db.sql | mysql -umxdb -pmxdb
If you use this image of mysql: tutum/mysql
You can add the name of the database you want to create at startup as an environment variable:
environment:
-ON_CREATE_DB="newdatabase"
Another solution is to put a shell script on the command part. In the script you start mysql and then create databases and add users:
command : run.sh
And on you script:
/usr/bin/mysqld_safe &
mysqladmin create testing_db