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

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.

Related

How to specify root username on docker-compose

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

Why edit contents of docker-compose.yml are not reflected?

I want to use MySQL by using Docker.
I wrote the following DockerFile and docker-compose.yml.
Dockerfile
FROM mysql:8.0
RUN mkdir /var/log/mysql
RUN touch /var/log/mysql/mysqld.log
docker-compose.yml
version: '3'
services:
dbserver:
build: ./docker/mysql
image: test-db:0.0.1
restart: always
environment:
MYSQL_DATABASE: prototype
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: rootpassword
ports:
- "3306:3306"
volumes:
- ./docker/mysql/initdb.d:/docker-entrypoint-initdb.d
- ./docker/mysql/conf.d:/etc/mysql/conf.d
- ./log/mysql:/var/log/mysql
- ./docker/mysql/data:/var/lib/mysql
volumes:
mysql-bd:
driver: local
I succeeded build and could confirm the database.
Then I wanted to change the database name, so I edited a part of the yml file following.
Before
MYSQL_DATABASE: prototype
After
MYSQL_DATABASE: test_db
Then, I confirmed the database but its name was not changed.
I removed the MySQL container and tried again, but the result was not changed.
Why edit contents of docker-compose.yml are not reflected?
You are using a host volume for your database, meaning that the databases are persisted between containers restarts.
...
volumes:
./docker/mysql/data:/var/lib/mysql
...
Delete the local directory ./docker/mysql/data and restart your services. The database change will be reflected.

docker mysql wordpress port doesn't connect

I downloaded the mysql and wordpress images. Mysql ports are
3306 localhost:32781
33060 localhost:32780
Wordpress configuration is
WORDPRESS_DB_HOST 192.168.99.100:32774
MYSQL_ROOT_PASSWORD and WORDPRESS_DB_PASSWORD are the same
I try to connect to wordpress with
http://192.168.99.100:32774/
I get the message
This site can’t be reached
How do I have to configure the ports of mysql and wordpress?
CONFIGURATION MYSQL
WORDPRESS
Error trace
From what you can find on the docker configuration page, you should take this example and modify it to your needs.
There is the following docker-compose file that will launch a wordpress in a minute:
https://docs.docker.com/compose/wordpress/
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
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:
db_data: {}
From that file you have various options like, using docker compose tool (https://docs.docker.com/compose), or if you have a swarm running you could use docker stack command(https://vsupalov.com/difference-docker-compose-and-docker-stack/) or you can divide the configuration of both elements and create separate Dockerfile's(the configuration of a Docker file differs from what you can see on docker-compose so take the information an create your own) and launch them separated, you should launch mysql first as wordpress depends on a bbdd running first.
The easiest way to implement this is using docker-compose. Here is an example:
version: '3.2'
services:
database:
image: mysql:5.7
volumes:
- my_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: wordpress
MYSQL_USER: wp_user
MYSQL_PASSWORD: password
wordpress:
depends_on:
- database
image: wordpress:php7.3-apache
ports:
- '8000:80'
restart: always
environment:
WORDPRESS_DB_HOST: database:3306
WORDPRESS_DB_USER: wp_user
WORDPRESS_DB_PASSWORD: password
working_dir: /var/www/html
volumes:
my_data: {}
A few notes: the database doesn't mount any port on host because it doesn't need to. If you don't want to use docker-compose you can run docker run commands for this but then you have to create your own network for the containers and attach them to it.
Wordpress will be available on http://localhost:8000.
WORDPRESS_DB_HOST is the connection to the database and you won't be able to access that through http anyway.
Hope this helps you.

Issue getting docker to access my database properly with wordpress

I'm new to docker all together - but am trying to setup a local test environment to play with some wordpress things.
So I went to the docker site and pulled up a default docker .yml file on how to get it going easily.
I've made just a couple changes, but mostly this is a straight forward document.
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql2
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: somerootwordpresspw
MYSQL_DATABASE: testdatabase
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
volumes:
- ./WP-TEST/:/var/www/html/
depends_on:
- db
image: wordpress:latest
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
When I run docker-compose up with the above .yml file, I see this error:
MySQL "CREATE DATABASE" Error: Access denied for user 'wordpress'#'%' to database 'wordpress'
Which I find odd, because I'm naming the database testdatabase, so why is it trying to create a database named wordpress?
When I connected with SQL Pro, I could see testdatabase, but according to the console it's trying to create wordpress db.
How do I get it to connect to my named DB, instead of constantly failing to create wordpress?
So I think I got it.
It was really simple. In my wordpress portion of my .yml file I needed to include WP_DB_NAME: testdatabase
By doing that, it used my named testdatabase to install wordpress to.
Hope this helps people who might stumble across this.
Now the .yml file looks like this:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql2
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: somerootwordpresspw
MYSQL_DATABASE: testdatabase
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
volumes:
- ./WP-TEST/:/var/www/html/
depends_on:
- db
image: wordpress:latest
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: testdatabase
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:

Docker: how to use SQL file in Directory

I'm trying to open a Wordpress website locally with Docker.
Here is the docker-compose.yml file for this container:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
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
volumes:
db_data:
The Dockerfile:
FROM orchardup/php5
ADD . /code
In the terminal, I enter docker-compose up -d. I can then visit the site at localhost:8080, but it's not the actual website - it's just a Wordpress template. I'm guessing I have to incorporate the .sql file in the directory somehow? How would I go about doing this? Do I need to specify this in the .yml file?
Just add a volume mapping to map a local folder to the /docker-entrypoint-initdb.d container folder, for example : ./init-db:/docker-entrypoint-initdb.d. This file will be loaded on the first container startup.
Considering the docker-compose.yml bellow :
drop your sql files into /path-to-sql-files-on-your-host host folder)
run docker-compose down -v to destroy containers and volumes
run docker-compose up to recreate them.
-
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
- /path-to-sql-files-on-your-host:/docker-entrypoint-initdb.d
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
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
volumes:
db_data:
According to the MySQL-Docker documentation, you have a couple of options.
After the 'db' docker is running, simply connect to it using the mysql client, and import your .sql dump. Read the section 'Connect to MySQL from the MySQL command line client' If you where not using docker, you would restore this backup like this mysql fooDB < fooDB_dump.sql It will be similar with docker commands.
Docs, "Initializing a fresh instance", says "...it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d" That looks more like what you want. Just copy your .sql file into that location within the docker image and then it will automatically parse it.