I have following docker-compose.yml
version: '2'
services:
storage:
image: library/mysql:5.5
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=storage
ports:
- 3306:3306
volumes:
- ./mysql-initdb:/docker-entrypoint-initdb.d
- ./mysql-volume:/var/lib/mysql
Inside docker-entrypoint-initdb.d directory following .sql script:
BEGIN;
DROP DATABASE IF EXISTS `storage`;
CREATE DATABASE `storage`;
USE `storage`;
DROP TABLE IF EXISTS SETTINGS;
CREATE TABLE SETTINGS (
`NAME` VARCHAR(100) NOT NULL,
`VALUE` VARCHAR(100) NOT NULL,
PRIMARY KEY(`NAME`)
) ENGINE=MEMORY DEFAULT CHARSET=utf8;
INSERT INTO
`storage`.`SETTINGS` (`NAME`, `VALUE`)
VALUES
('AAAAA','BBBBB');
COMMIT;
When I executes docker-compose up I get information that .sql script is executed:
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/1_storage_schema.sql
When I connect to that database from MySQL Workbench I see only empty tables without data. It looks like INSERT statement was not executed.
I tried also:
Naming columns with and without "`" character
Using and not BEGIN/COMMIT
In the volumes you are missing a /, the correct syntax will be:
volumes:
- ./mysql/docker-entrypoint-initdb.d/:/docker-entrypoint-initdb.d
What is happening is, the content inside the folder is not getting copied, instead a folder is made inside the docker-entrypoint-initdb.d. To ensure it is present in correct place, look inside the container by using docker exec.
I was having the same issue. Mysql wasn't populating the tables. Mysql was defined as a Docker volume and the import sql was bind mounted. These definitions were fine. I dug a little bit more and found that I had MYSQL_USER and MYSQL_PASSWORD set in my environment. I was also trying to run some sql in a file called 0users.sql inside the /docker-entrypoint-initdb.d directory. That file was attempting to RECREATE the user defined in MYSQL_USER environment variable. I removed the 2 environment variables. Then, I removed the Docker container and mysql volume. Then, tried again. Worked.
My assumption is that if the container runs into any kind of SQL error, the entire import appears to stop. Since my file was at the top of the alphabetical order, the import died on user creation and no tables were imported.
Related
I have such a problem, I'm trying to do master-slave replication according to this tutorial https://developpaper.com/master-slave-replication-of-mysql-based-on-docker/ .
There is no problem with the building of the image, but there is a problem with running this container. I can't get access to the mysql commands of mysql container in docker, it seems to be built from mysql (image from docker hub), but can not be started.
There are the following files:
Dockerfile:
#Using MySQL image to create a new image
FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD Kohc9hai
COPY start.sh /mysql/start.sh
COPY my.cnf /etc/mysql/my.cnf
COPY init.sql /mysql/init.sql
EXPOSE 6603
CMD ["sh", "/mysql/start.sh"]
init.sql:
--Create data_ Copy database
DROP DATABASE IF EXISTS `data_copy`;
CREATE DATABASE `data_copy` /*!40100 DEFAULT CHARACTER SET utf8mb4 collate utf8mb4_general_ci */;
--Create person table
USE `data_copy`;
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
`id` int(32) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
my.cnf:
[mysqld]
log-bin = mysql-bin
server-id = 1
start.sh:
#!/bin/sh
echo "start MySQL"
service mysql start
sleep 5
echo "initialize database"
mysql -uroot -pKohc9hai < /mysql/init.sql
echo "initialization complete"
tail -f /dev/null
By getting deeper, I think that I determined that the problem is in the file start.sh (Maybe I'm wrong).
There are logs of this container:
start MySQL
mysql: unrecognized service
initialize database
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
initialization complete
Maybe someone can help me with the solution to this problem.
I'v just started to study Docker.
mysql: unrecognized service
From next, you could see there is no mysql service in container, so you surely failure:
$ docker run --rm mysql ls /etc/init.d
hwclock.sh
In fact, mysql image use next to start mysql service:
exec gosu mysql "$BASH_SOURCE" "$#"
So you need to follow the same way.
BUT, for your scenario, looks you just customize start.sh to init some sql, while it's in fact already supported in official image:
Initializing a fresh instance
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.
So, what you need to do is next in Dockerfile (Maybe need to specify MYSQL_DATABASE also):
COPY init.sql /docker-entrypoint-initdb.d
The document is quite old (docker version 1.13.1! Now 19.03.13!!). No special setting is required to use MySQL image. All you need to do is:
docker run --name foo -d -p 6603:6603 mysql:latest
I Have watched approximately 23.74 docker-compose tutorials for laravel and mysql containers!
Please can someone explain to me???
When I create my docker-compose file I create an mysql container from a mysql image.
THEN
I have to enter variables that look like this:
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
What is the difference between these variables and the variables that I enter into the .env file of my laravel app.
THESE ONES:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laradb
DB_USERNAME=root
DB_PASSWORD=secret
WHAT IS THE DIFFERENCE????
What is the docker-compose variables doing. And what are these .env variables doing. And Why am I setting these up twice?
The reason I am asking is because whenever I follow docker tutorials to set up mysql I can only get it to work if I use the variety of variables provided to me by the teacher?? This makes no sense. What about if I want to use my own variable values??!
As soon as I try use my own variables the db breaks and I can't connect to it.
Is homestead some special db instance that is for laravel? This was not an issue when I do it all locally without docker.
For example. The above docker-compose variables were used to create a mysql container and then when i connect to it with SQL workbench I see a schema called 'homestead'. Now what do I do if I don't want that Schema to be called homestead, or what if I want to add another Schema?? It doesn't let me??(permission denied).
I have now spent 3 days trying to create an empty laravel app that connects to a db in a separate container that uses mysql that I can connect to via SQL workbench to see the actual db. I want to be able to create the schema name I want to use in SQL workbench and then be able to set that schema as the db name in my laravel .env file.
Please HELP! You don't have to solve this problem for me but can you point me towards some helpful material that explains this stuff!! For docker-compose specifically mysql. No looking to use std docker commands in the terminal if possible.
.env file vs docker-compose.yml environment::
https://docs.docker.com/compose/env-file
https://docs.docker.com/compose/environment-variables
They have different scopes / precedence.
Passing environment variables in for the benefit of MySQL:
https://hub.docker.com/_/mysql
The MySQL container expects those environment variables to exist and have values.
Likewise for the Laravel container needs to be able to talk to the MySQL container, hence it needs the values to match and that is why there is overlap.
The bash command printenv might help tighten this up, as you can see what environment variables are exposed to which containers (docker run msql_container_name bash -c 'printenv' vs docker run laravel_container_name bash -c 'printenv').
https://github.com/reflexions/docker-laravel (as an example)
You've mentioned you don't mind being sent to references, so I've primarily done that - but I'm happy to elaborate in here or in comments if it still isn't making sense / I'm not addressing the main issue.
I want to create an image with docker for my app.
The app uses MySQL. I need my image to be based on MySQL image (mysql/mysql-server ?) .
IN the Dockerfile i need to set some instructions to create a DB with specific user/password . So, my app can work with that DB .
I don't need tables, only empty DB with specific name and user/password that can access this DB.
How can i do this?
I wanted something like
FROM mysql/mysql-server
# Create MySQL DB
mysql -u root -e "CREATE DATABASE MyDB"
But i don't know root user password here. It seems it is autogenerated ?
How can i do this?
That image auto-generates the root password by default, as stated in the image github repository (https://github.com/mysql/mysql-docker). You can set the MYSQL_ROOT_PASSWORD environment variable in your Dockerfile with the password you want.
Apart from that, if what you want is to create a database at image startup, you can use the environment variable MYSQL_DATABASE.
More info about the supported environment variables here:
https://github.com/mysql/mysql-docker#docker-environment-variables
You have 2 solutions here:
[Easy] Using the docker-compose and create a docker-compose.yml file like this one:
version: '3'
services:
mariadb:
container_name: mariadb
image: mariadb:latest
environment:
- MYSQL_DATABASE=_YOUR_DB_
- MYSQL_ROOT_PASSWORD="_YOUR_PASSWORD_"
- MYSQL_USER=_YOUR_USER_
- MYSQL_PASSWORD=_YOUR_USER_PASSWORD_
...
This configuration will bring a MariaDB database to you. also if you want to use it, you can simply check this page for the installation guide:
https://github.com/docker/compose
The final step is just to go into the directory you saved the docker-compose.yml and just run:
docker-compose up
or if you don't want to see the log inside the terminal just add -d flag to it.
2. [Little Complicated] You can create a custom image for your needs. in this case, it is better if you check the Dockerfile documents and then see this autogenerated default MariaDB Dockerfile for understanding what to do exactly to achieve your goal.
I have imported an SQL file contains my schema and all its tables, By using:
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
- ./resources/file.sql:/docker-entrypoint-initdb.d/file.sql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: db
The problem is, when I trying to retrieve data from some tables an exception in the backend appear:
throws exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table
'db.Configuration' doesn't exist
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'db.Configuration' doesn't exist
And some tables work perfectly like user table.
Although I have tested the SQL file in MySQL Workbench.
The question is, Is there a way I can see what tables are inside the db_data volume?
Yes, you can see All Table information from docker command line.
First, go inside docker container, run below command.
docker exec -it mysql_container_name mysql -uroot -p
where “root” is the username for MySQL database.
After running above command it will ask you a password.
Then Select Database, run below command
USE Name-Of-The-Database
get the list of all tables.
show tables;
Run any query, e.g select * from
SELECT * FROM table_name;
I have listed down some daily docker useful commands, have a look.
https://rohanjmohite.wordpress.com/2017/08/04/docker-daily-useful-commands/
please let me know in case any further more explanation required?
You can execute any SQL command directly from the host to view your database.
You are looking for this command:
docker exec -it mysql-container mysql -uroot -pmy-secret-pw -D thisdatabase -e "SELECT * FROM table_name;
where mysql-container is the name of the mysql container
where -uroot is the account for the sql container
where -pmy-secret-pw is the password for the sql container
where thisdatabase is the name of the database to inspect
where table_name is obviously the database table name of interest
TIP: if it is a new container, and you don't know the password, just run this command:
sudo docker logs mysql-container 2>&1 | grep GENERATED
One solution is to use MySQL Workbench and create a connection pointing to the docker database container. From there you can check what schema tables have been created.
If the database docker container is started, you can inspect the container and find the IPAddress using the following command:
docker inspect container-name-here
get the IPAddress and use it in the MySQLWorkbench to create the connection
I have a sql script that contains the statement create tbl2 as select * from tbl;
This statement works on a docker mysql with version 5.7 (currently 5.7.19), even though enforce-gtid-consistency is turned on. Relevant lines from the docker compose yml are:
image: mysql:5.7
command: --gtid-mode=ON --enforce-gtid-consistency=true
This is even though the documentation clearly specifies:
Since only transactionally safe statements can be logged when --enforce-gtid-consistency is enabled, it follows that the operations listed here cannot be used with this option:
CREATE TABLE ... SELECT statements
The same statement fails with ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECT. on a google cloud mysql instance.
show variables like '%gtid%' returns the same result on the docker mysql and the google cloud instance (and enforce_gtid_consistency is on in both).
Primary reason for failure you see is log-bin is NULL in container database though gtid_mode is on and enforce_gtid_consistency is on. So you must run container with log-bin set , setting this additionally also require to set server-id. Below is one example to run container where you should be able to reproduce error - "
ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECT.
"
docker run -d --name=my-mysql --env="MYSQL_ROOT_PASSWORD=rootpassword123" --publish 3306:3306 --volume=/u01/mysql:/var/lib/mysql mysql/mysql-server:5.7.20 --gtid_mode=ON --enforce_gtid_consistency=ON --log-bin=mysql-bin --master-info-repository=table --relay-log-info-repository=table --server-id=1
cheers
raj