problem with running of mysql image in docker - mysql

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

Related

Where docker stores mysql settings

I am facing one issue with docker, L am using laradock docker env for laravel. Since it has issue with mysql version I had to run those command:
$ docker-compose exec mysql bash
$ mysql -u root -p
ALTER USER 'root'#'localhost' indentified WITH mysql_native_password BY 'root';
Also imported database through http://localhost:8080 and phpmyadmin
So I am trying to reproduce this issue again, so I deleted everything from docker with
$ docker system prune
but when I rebuild the containers
sudo docker-compose up -d nginx mysql phpmyadmin workspace
My previous database is loaded again.
So my question would be how to delete db and MySQL settings, so I can execute the alter command and import database again.
Overall I am trying to determine if this issue with MySQL will occur on another platform again, so I am trying to reproduce it from scratch and that is why I need to reset completely MySQL env and databases.
So not sure where MySQL settings are stored and how to delete them.
MySQL is storing most of the important information of your container in a volume.
Now, the command:
docker system prune
do not remove the volumes, per default.
If you also want to remove them, you can run:
docker system prune --volumes
If you do want to list or act on those specific volumes:
docker volume --help
would give you all the commands on volumes like rm, ls, ...

How to run mysql scripts in docker image?

In my Dockerfile
FROM mysql:5.7
ADD ./mysql_scripts /mysql_scripts
WORKDIR /mysql_scripts
RUN mysql -u root -p < create_user.sql &&\
mysql -u root -p < create_database.sql &&\
mysql -u root -p < create_tables.sql
EXPOSE 3306
but when I build that image I have problem like:Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
You cannot run those mysql commands, because the container is not running yet, so neither is MySQL.
What you can do is copy your .sql files to /docker-entrypoint-initdb.d. MySQL will run all .sql, .sql.gz and .sh located in this directory after initialization, which would result in the following Dockerfile:
FROM mysql:5.7
COPY ./mysql_scripts /docker-entrypoint-initdb.d
EXPOSE 3306
More information can be found on the dockerhub page of mysql
You can not import database at build time, as every RUN command run in separate shell plus MySQL process also not ready to accept the connection.
You can take benefits from docker entrypoint that does these out of the box.
COPY create_user.sql /docker-entrypoint-initdb.d/
COPY create_database.sql /docker-entrypoint-initdb.d/
COPY create_tables.sql /docker-entrypoint-initdb.d/
So when the container started it will run the above script in alphabetical order
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.
mysql-docker-init-db
Also you use MYSQL_USER to create user.

Can't connect to local MySQL server using Docker

Please help me to solve this problem:
I am using docker to create a mysql container and initialize a database in it automatically based on mysql:8.0.19. But when I get into the container and enter the pass word after 'mysql -u root -p', it is said that:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
There are four files: Dockerfile, schema.sql, privileges.sql, setup.sh
Dockerfile:
FROM mysql:8.0.19
# allow no password
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
# copy file into container
COPY setup.sh /mysql/setup.sh
COPY schema.sql /mysql/schema.sql
COPY privileges.sql /mysql/privileges.sql
# exec these command when container start up
CMD ["sh", "/mysql/setup.sh"]
setup.sh:
#!/bin/bash
echo 'checking mysql status.'
service mysql status
echo '1.start mysql....'
service mysql start
sleep 3
service mysql status
echo '2.start importing data....'
mysql < /mysql/schema.sql
echo '3.end importing data....'
sleep 3
service mysql status
echo '4.start changing password....'
mysql < /mysql/privileges.sql
echo '5.end changing password....'
sleep 3
service mysql status
echo 'mysql is ready'
tail -f /dev/null
privileges.sql:
use mysql;
select host, user, plugin from user;
ALTER USER 'root'#'localhost' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
schema.sql:
create database `collector`;
use collector;
DROP TABLE IF EXISTS `EVENT`;
CREATE TABLE `EVENT` (
`ID` VARCHAR(50) NOT NULL,
`EventType`VARCHAR(50) DEFAULT NULL,
`EventID` VARCHAR(50) DEFAULT NULL,
`EpcID` VARCHAR(50) DEFAULT NULL,
`TimeStamp` FLOAT DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `EVENT` (`ID`, `EventType`, `EventID`, `EpcID`, `TimeStamp`)
VALUES('0','0','0','0',0.0);
I think privileges.sql might be the one that causing the problem, but i have no idea how to solve it.
By the way I use:
docker build . -t mysql_service:1.0
docker run -p 3306:3306 --name mysql_service -e MYSQL_ROOT_PASSWORD=123456 -d mysql_service:1.0
to build and run.
To set a root password use MYSQL_ROOT_PASSWORD=123456.
MYSQL_DATABASE=collector is also available to create the database and use it as the default.
FLUSH PRIVILEGES aren't needed for ALTER USER.
The docker container doesn't have a init so service ... commands won't work. Also the mysqld isn't running in the build stage.
https://hub.docker.com/_/mysql/ under Initializing a fresh instance you can put SQL files in /docker-entrypoint-initdb.d to initialize the database.

How to import a .sql file while starting mysql service on ubuntu OS

After installing mysql on ubuntu I'd like to start the mysql service with following command which will start the service and also it will create the schema and tables.
service mysql start && mysql -u root -e "CREATE DATABASE organzation; use organzation; CREATE TABLE employee(Email varchar(30) NOT NULL)"
Now suppose I have a file.sql file, how to import this file.sql while starting the mysql service.
PS: mysql -u username -p database_name < file.sql this doesn't work while starting the mysql service.
From mysql Docker:
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.
You just need to put your .sql file(s) in /docker-entrypoint-initdb.d directory.

Docker MySQL does not popoulate database with data

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.