Docker: how to use SQL file in Directory - mysql

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.

Related

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.

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.

logging mysql queries from docker container

i am new to docker and I am still figuring out how it works. I have setup through docker-compose a wordpress site and the yaml file i am using is the following.
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- ./db_data/db/backup.sql:/docker-entrypoint-initdb.d/backup.sql #it loads an already existing database
#- ./db_data/all.log:/var/log/mysql/all.log # i want here to have the logs of 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/wp-content:/var/www/html/wp-content # loads the files from an existing installation of wordpress
volumes:
db_data:
as you can see by this line
- ./db_data/all.log:/var/log/mysql/all.log
i am trying to somehow make it so i can log all the queries made. I have managed to do that by doing the following:
1) docker-compose up, while having commented out the line with all.log from yaml file.
2) logging to the container of the mysql with bash and running the following
mysql -u root -psomewordpress -e "SET global log_output = 'FILE'; SET global general_log_file='/var/log/mysql/all.log'; SET global general_log = 1;"
3) Going to the /var/lib/docker/volumes and getting the file created.
What i am trying to achieve is to make somehow this command part of the yaml file so I can also have the all.log file mounted at the location where the yaml file is.
What I did to solve my problem was using the following:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- ./db_data/db/backup.sql:/docker-entrypoint-initdb.d/backup.sql #it loads an already existing database
-./logs/mysql:/var/log/mysql # to get the folder of mysql in my logs/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/wp-content:/var/www/html/wp-content
This code would sync what is inside the docker container of mysql with my folder logs/mysql. So I put a little shell script in there.
#!/bin/bash
if [ ! -f /var/log/mysql/all.log ]; then
touch /var/log/mysql/all.log
fi
chmod 777 /var/log/mysql/all.log
mysql -u root -psomewordpress -e "SET global log_output = 'FILE'; SET global general_log_file='/var/log/mysql/all.log'; SET global general_log = 1;"
It checks if the all.log file is there, if not it creates it and then starts mysql logging. Thats it, after that all queries are logged directly to all.log in which i have direct access.
One more thing, to execute that shell script you might want to use
docker exec 5.7-mysql "./var/log/mysql/initlogs.sh"
where initlogs.sh is the name of the shell script.
I really hope it would help someone!

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: