I got a MariaDB database dump from a colleague, and he asked me to run it in a docker container.
So i executed the following:
docker pull mariadb:10.4.26
then created the container
docker run --name test_smdb -e MYSQL_ROOT_PASSWORD=<some_password> -p 3306:3306 -d mariadb:10.4.26
then connected to the container:
docker exec -it test_smdb mariadb --user root -p<some_password>
and created a database in it from the mariadb prompt:
MariaDB [(none)]> CREATE DATABASE smdb_dev;
So far, so good. But when i tried to import the dump into it via this command:
docker exec -i test_smdb mariadb -uroot -p<some_password> --force < C:\smdb-dev.sql
i get a lot of lines like
ERROR 1046 (3D000) at line 22: No database selected.
So i am not sure what exactly is the issue?
Should i define a database, in which the dump should be imported? If yes - how exactly, because i look at different pages, like:
https://hub.docker.com/_/mariadb, especially this:
$ docker exec -i some-mariadb sh -c 'exec mariadb -uroot -p"$MARIADB_ROOT_PASSWORD"' < /some/path/on/your/host/all-databases.sql
and i see no database mentioned here.
Or
2) The colleague has not created the dump in the correct way?
I do not use mariadb in a docker environment at my place, but I do use mariadb on a linux machine so it should be really similar.
You said you used this command :
docker exec -i test_smdb mariadb -uroot -p<some_password> --force < C:\smdb-dev.sql
If we breakthrough it :
docker exec -i test_smdb docker stuff where you ask docker to execute the following command on the test_smdb container (or close to it, I'm not a docker daily user).
mariadb -uroot -p<password> --force here is the interesting part. You ask your shell to open mariadb and login as root with then given password with an extra flag --force. But you never specify which database should be overridden.
In my gist, again for mariadb outside docker but I really think it should be the same, I've the following command mariadb -uusername -p<password> <DB_NAME> < /path/to/file.sql
So I would try something like :
docker exec -i test_smdb mariadb -uroot -p<some_password> smdb_dev --force < C:\smdb-dev.sql
Below command should work I believe
docker exec -i test_smdb sh -c "exec mariadb -uroot -pPASSWORD smdb_dev" < /some/path/on/your/host/all-databases.sql
Reference: Import local database to remote host Docker container
Related
I executed docker commands in order to create a docker image, containing an maria DB SQL dump
docker pull mariadb:10.4.26
docker run --name test_smdb -e MYSQL_ROOT_PASSWORD=<some_password> -p 3306:3306 -d mariadb:10.4.26
docker exec -it test_smdb mariadb --user root -p<some_password>
MariaDB [(none)]> CREATE DATABASE smdb_dev;
docker exec -i test_smdb mariadb -uroot -p<some_password> smdb_dev --force < C:\smdb-dev.sql
But as this will have to be part of a Azure pipeline execution, i was advised to put all this in a docker file.
How to combine docker creation and putting it in DEV Azure Container Registry?
Unfortunately i cannot find proper information how combine this all and especially the calling of an execution of a database creation from the docker file?
You can try to use this as a sample and modify to your needs:
# Dockerfile
FROM mariadb:10.4.26
ENV MYSQL_ROOT_PASSWORD dummy-password
ENV MARIADB_DATABASE smdb-dev
COPY C:\smdb-dev.sql ./docker-entrypoint-initdb.d/
ENTRYPOINT [ "docker-entrypoint.sh" ]
EXPOSE 3306
CMD [ "mysqld" ]
When you first time run a container, docker will restore your DB from the docker-entrypoint-initdb.d/ folder (it will restore it only if the persistent volume will be empty, otherwise it will just skip this step), in order to make this data persistent, you need to create a volume and attach it to mysql data folder on your container.
docker build *Dockerfile dir path* -t *image-name*
docker run -dp 3306:3306 *image-name*
I am working through the Docker tutorial from here: https://docs.docker.com/language/python/develop/
I have done the first few steps from the tutorial to create a MYSQL container:
$ docker volume create mysql
$ docker volume create mysql_config
$ docker network create mysqlnet
$ docker run --rm -d -v mysql:/var/lib/mysql \
-v mysql_config:/etc/mysql -p 3307:3306 \
### (note: I used 3307:3306 here instead of the specified 3306:3306 because port 3306 was already being used on my machine) ###
--network mysqlnet \
--name mysqldb \
-e MYSQL_ROOT_PASSWORD=p#ssw0rd1 \
mysql
I then followed the next step to check if the mysql container was running:
$ docker exec -ti mysqldb mysql -u root -p
the terminal then prompts:
Enter password:
and I am unable to enter anything. no commands seem to work. ctrl+C didn't even register. the only thing I could do was kill the terminal itself. I am very confused since I have been following the documentation very closely though I am sure it's something very dumb.
Thanks in advance!
Try:
$ docker exec -ti mysqldb bash
then:
mysql -u root -p
I think you can connect with shell first
i am creating a mysql 5.6 docker using bash script and i would like to change the password.
how can i send sql commands from bash to docker?
build:
sudo docker build -t mysql-5.6 -f ./.Dockerfile .
run.sh:
#!/bin/bash
sudo docker run --name=mysql1 -d mysql-5.6
sudo docker exec -it mysql1 mysql -uroot -p$base_password \
<<< SET PASSWORD FOR 'root'#'localhost' = PASSWORD('new_pass');
You need to bind MySQL-port like descriped here. To keep the port 3306 you can just expose it on your host the following way:
sudo docker run --name=mysql1 -p 3306:3306 -d mysql-5.6
After that you should be able to use mysql -u USER -p PASSWORD on your local host. This will then allow you to send commands to your docker-container.
Just basic and simple steps illustrating what I have tried:
docker pull mysql/mysql-server
sudo docker run -i -t mysql/mysql-server:latest /bin/bash
yum install vi
vi /etc/my.cnf -> bind-address=0.0.0.0
exit
docker ps
docker commit new_image_name
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=secret -d new_image_name
docker ps -a STATUS - Exited (1)
Please let me know what I did wrong.
Instead of trying to modify an existing image, try and use (for testing) MYSQL_ROOT_HOST=%.
That would allow root login from any IP. (As seen in docker-library/mysql issue 241)
sudo docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_ROOT_HOST=% -d mysql/mysql-server:latest
The README mentions:
By default, MySQL creates the 'root'#'localhost' account.
This account can only be connected to from inside the container, requiring the use of the docker exec command as noted under Connect to MySQL from the MySQL Command Line Client.
To allow connections from other hosts, set this environment variable.
As an example, the value "172.17.0.1", which is the default Docker gateway IP, will allow connections from the Docker host machine.
I'm using the official MariaDB container. I need binary logs in mixed format, which I currently do by running:
docker run -it --link mariadb:mysql --rm mariadb sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'
and then
SET GLOBAL binlog_format = 'MIXED';
However if I restart the container I have to do it again. Is there a way to enable it by default?