How to specify root username on docker-compose - mysql

I have a docker-compose.yml file working fine. I want to have a user that has root privileges, so that i can access the database from outside using that user instead of the root one. I can do it after i create te container, but i´d like to automatize it so that a new user with privileges is created or i can change the root name.
version: '3.8'
services:
mysql:
image: mysql
container_name: mysql
ports:
- '3307:3306'
environment:
MYSQL_ROOT_USER: 'palc'
MYSQL_ROOT_PASSWORD: 'palc'
MYSQL_DATABASE: 'palc'
MYSQL_USER: 'palc'
MYSQL_PASSWORD: 'palc'
volumes:
- .:/palc/var/lib/mysql
palc:
image: palc
container_name: palc
restart: always
build:
context: .
dockerfile: Dockerfile
command: bash -c " python manage.py migrate && python manage.py runserver 0:8000"
volumes:
- .:/palc
ports:
- '8000:8000'
depends_on:
- mysql

Related

How do I use my own my.cnf/mysqld.cnf inside a MYSQL docker-compose?

I want to use my own configuration file to better utilize my server and to set mysql to be case insensitive. This is the path to my cnf file /etc/mysql/mysql.conf.d/mysqld.cnf.
I assumed you would add it the same way you add /var/lib/mysql but it doesn't do anything. I also tried:
volumes:
- db:/var/lib/mysql
- ./data/db/mysql/mysql.conf.d/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf.
This is my docker-compose:
version: '3.3'
services:
db:
image: mysql:latest
restart: always
volumes:
- db:/var/lib/mysql
# focus on this
- db:/etc/mysql/my.cnf
env_file: ./.env
environment:
MYSQL_ROOT_PASSWORD: ${password}
MYSQL_DATABASE: ${database}
MYSQL_USER: ${user}
MYSQL_ROOT_USER: ${user}
MYSQL_PASSWORD: ${password}
#MYSQL_HOST: ${password}
MYSQL_ROOT_HOST: "%"
ports:
- ${db_port}:${db_port}
cap_add:
- SYS_NICE
app:
# ...
depends_on:
- db
volumes:
db:
How can I put my own cnf into a docker mysql container?
According to the docs on docker hub, you have to create the volume with the parent folder:
/my/custom:/etc/mysql/conf.d
Where /my/custom contains the config files.
https://hub.docker.com/_/mysql

How to add DBA account in docker-compose.yml?

In the following docker-compose.yml, how can I add DBA account (for example, username: abc password:123) with 'GRANT ALL PRIVILEGES' to it in MySQL?
I have tried adding 'MYSQL_DBA_USER: abc MYSQL_DBA_PASSWORD: 123' under 'MYSQL_PASSWORD: secret' but it does not work.
services:
mysql-server:
container_name: mysql
ports:
- "18080:8080"
environment:
MYSQL_ROOT_PASSWORD: 12345
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress_user
MYSQL_PASSWORD: secret
image: mysql/mysql-server
wordpress:
image: wordpress:latest
container_name: wordpress
ports:
- "20080:80"
environment:
WORDPRESS_DB_HOST: mysql-server:3306
WORDPRESS_DB_USER: wordpress_user
WORDPRESS_DB_PASSWORD: secret
depends_on:
- mysql-server
If you map any SQL script files ending in .sql or .sh into the directory /docker-entrypoint-initdb.d in the container, they will be run when the database is created.
So if you create a script called create-dba-user.sql in the same directory as your docker-compose file and add a volume mapping to your docker-compose file like this
mysql-server:
container_name: mysql
ports:
- "18080:8080"
environment:
MYSQL_ROOT_PASSWORD: 12345
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress_user
MYSQL_PASSWORD: secret
volumes:
- ./create-dba-user.sql:/docker-entrypoint-initdb.d/create-dba-user.sql
image: mysql/mysql-server
Then your script will be run when the database is created. If you have an existing database, then it won't be run. But your compose file looks like you don't persist the database, so you'll have a fresh database on each run.

DOCKER and WordPress: Error establishing a database connection

I am new to docker and i have this docker .yml file (below) and i have a local set up of my WordPress site, i have imported a sql dump into a database in the 'container_web'. The problem i am having is that when i try to connect WordPress to the database inside the container i get this error "Error establishing a database connection." For the host i've used localhost, 127.0.0.1, mysql and none of them seems to work.
Do you know or could direct me in the right direction on what i should be checking in order to connect my WordPress install to the database?
services:
web:
build: .
container_name: 'container_web'
dns: '8.8.8.8'
volumes:
- ./:/var/www/html
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
depends_on:
- db
ports:
- 80:80
restart: always
environment:
WORDPRESS_ENV: 'development'
db:
container_name: 'container_db'
image: mysql:5.7
ports:
- 3306:3306
volumes:
- ./:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mydatabase
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
mysql-data: {}

Setup Docker containers for existing Wordpress site

I'm trying to setup a dev environment for an existing wordpress website hosted on cPanel.
I've exported the test data from the existing pre-production database to be imported into the mysql running in one of the containers.
version: '3.3'
services:
db:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: P#ssw0rd
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
- /local/path/to/wordpress/src/:/var/www/html
volumes:
db_data: {}
everything starts up fine. Now I'm inserting the db dump into the mysql db in the container
cat dump.sql | docker exec -i docker_db_1 /usr/bin/mysql -u wordpress --password=wordpress wordpress
which finishes without error. When trying to access the website now on localhost:8000 the Apache Ubuntu default page pops up but I can't see anything from the existing wordpress site.
This setup worked for me
docker-compose.yml
version: '3.6'
services:
wordpress:
image: wordpress:${WORDPRESS_VERSION}
container_name: wordpress
volumes:
- ${WORDPRESS_DATA_DIR}:/var/www/html
environment:
- WORDPRESS_DB_NAME=${WORDPRESS_DB_NAME}
- WORDPRESS_DB_HOST=${WORDPRESS_DB_HOST}
- WORDPRESS_DB_USER=${WORDPRESS_DB_USER}
- WORDPRESS_DB_PASSWORD=${WORDPRESS_DB_PASSWORD}
depends_on:
- mysql
restart: always
mysql:
image: mysql:${MYSQLDB_VERSION}
container_name: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
ports:
- 3306:3306
volumes:
- ./mysql:/var/lib/mysql
- ./mysql_config:/tmp/mysql_config
nginx:
image: nginx:${NGINX_VERSION:-latest}
container_name: nginx
ports:
- '80:80'
- '443:443'
volumes:
- ${NGINX_CONF_DIR}:/etc/nginx/conf.d
- ${NGINX_LOG_DIR}:/var/log/nginx
- ${WORDPRESS_DATA_DIR}:/var/www/html
depends_on:
- wordpress
restart: always
All used variables are set as environment variables first.
To connect to any container with use the following command:
docker exec -i -t <wordpress|mysql|nginx> /bin/bash

Authentication error using phpmyadmin and docker-compose

I'm trying to user docker-compose for mysql and phpmyadmin, but I'm getting an authentication error when trying to log in to phpmyadmin.
I've tried several configurations on the yaml file, but without any success.
version: '3'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_DATABASE: mydb
MYSQL_USER: root
MYSQL_PASSWORD: admin
MYSQL_ROOT_PASSWORD: admin
volumes:
- my-db:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
links:
- db
ports:
- 8080:80
restart: always
environment:
PMA_USER: root
PMA_PASSWORD: admin
volumes:
my-db: {}
When I try logging in to phpmyadmin i get the following errors:
mysqli_real_connect(): The server requested authentication method unknown to the client [caching_sha2_password]
mysqli_real_connect(): (HY000/2054): The server requested authentication method unknown to the client
I tried to change the yml to:
version: '3'
services:
db:
image: mysql:57
restart: always
environment:
After this, the container no longer starts, and it gives the following message:
docker_db_1 exited with code 1
This works fine:
version: '3'
services:
db:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_DATABASE: mydb
MYSQL_USER: root
MYSQL_PASSWORD: admin
MYSQL_ROOT_PASSWORD: admin
volumes:
- my-db:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
links:
- db
ports:
- 8080:80
restart: always
environment:
PMA_USER: root
PMA_PASSWORD: admin
volumes:
my-db: {}
Remember to delete the volume before running (in case you run into issues).
I just copied this code in docker-compose.yml
version: '3'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_DATABASE: mydb
MYSQL_USER: root
MYSQL_PASSWORD: admin
MYSQL_ROOT_PASSWORD: admin
volumes:
- my-db:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
links:
- db
ports:
- 8080:80
restart: always
environment:
PMA_USER: root
PMA_PASSWORD: admin
volumes:
my-db: {}
and ran the command docker stack deploy -c docker-compose.yml mysqllab
This is the result
enter image description here