MySQL container crash after /etc/mysql/my.cnf change, how to edit back? - mysql

I changed some mysql config settings and set something wrong, now Docker container keeps restarting and I cannot find the my.cnf file to edit in host filesystem. I have tried aufs/diff folders but so far unable to find it. Also tried:
find / -name my.cnf -exec nano {} \;
But it does not bring up the file I changed. And I tried to change config.v2.json to start /bin/bash instead of mysqld and restarted docker, but yet it started mysqld (due supervisor or something?) using official mysql container image.

I am seeing two possible solutions for your problem:
Bypass the ENTRYPOINT for the MySQL image
Find your image name by running docker images then run:
docker run -it --entrypoint="/bin/sh" OPTIONS image
That should take you to the bash inside the container and from there you can execute all the commands you want to find your my.cnf file. Although I don't know if editing the file from there, save it and try to run it again will works. I didn't tried.
Delete the old image and use the proper way to edit the my.cnf file
Find your image name by running: docker images and then delete it by running docker rmi <image_name>
Check the docs for the default MySQL images at MySQL Dockerhub is pretty straight on this and I quote:
Using a custom MySQL configuration file The MySQL startup
configuration is specified in the file /etc/mysql/my.cnf, and that
file in turn includes any files found in the /etc/mysql/conf.d
directory that end with .cnf. Settings in files in this directory will
augment and/or override settings in /etc/mysql/my.cnf. If you want to
use a customized MySQL configuration, you can create your alternative
configuration file in a directory on the host machine and then mount
that directory location as /etc/mysql/conf.d inside the mysql
container.
If /my/custom/config-file.cnf is the path and name of your custom
configuration file, you can start your mysql container like this (note
that only the directory path of the custom config file is used in this
command):
$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e
MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
This will start a new
container some-mysql where the MySQL instance uses the combined
startup settings from /etc/mysql/my.cnf and
/etc/mysql/conf.d/config-file.cnf, with settings from the latter
taking precedence.
From that point and if you create the my.cnf file on your host then you'll never run into this problem again since you can edit the file as many times as you want.

Related

Load MySQL Employees Sample Database into a already running docker container on Windows

I have a MySQL Docker container running in my local Windows machine. I want to load Employees database into that docker container.
Employees Database Reference: https://dev.mysql.com/doc/employee/en/
I tried using MySQL Workbench and "Run SQL Script", but it's throwing below error:
[WinError 32] The process cannot access the file because it is being used by another process:
'C:\\Users\\roul\\AppData\\Local\\Temp\\tmp4fbw2bb4.cnf'
After reading some article I think we may have one option of attaching the script file location volume into the container and run the script from docker command prompt, but that I'm unable to do it.
Anyone here have already done that?
Find the datadir of you MySQL Server:
SHOW VARIABLES WHERE variable_Name LIKE "datadir"
Copy the content of the folder to your datadir (\. copy the content of the folder, not the folder; maybe you want improve this to not mess the datadir):
docker cp test_db-master/. CONTAINER:/var/lib/mysql/
Run the script inside the container:
docker exec -i CONTAINER /bin/bash -c "cd /var/lib/mysql/ && /usr/bin/mysql -u root --password=123456 < /var/lib/mysql/employees.sql"

How to install sudo and nano command in MySql docker image

I am trying to connect Django application with MySql docker container. I am using the latest version of MySql i.e MySql 8.0 to build a container. I was able to build the MySql container successfully but I am not able to connect it using Django's default MySql Connector. When I run the docker-compose up command I get the error mentioned below.
django.db.utils.OperationalError: (1045, 'Plugin caching_sha2_password could not be loaded: /usr/lib/x86_64-linux-gnu/mariadb19/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory')
I started looking for the solution and got to know that MySql has released a major change in its default authentication plugin which is not support by most of the MySql Connectors.
To fix this issue I will have to set the default-authentication-plugin to mysql_native_password in my.cnf file of MySql container.
I logged into container using command docker exec -it <conatiner id> /bin/bash and was also able to locate the my.cnf file inside the container.
To edit the my.cnf file I will have to use the nano command as stated below.
nano my.cnf
But unfortunately nano command is not installed in MySql Container. To install nano I will need sudo installed in container.
I tried installing sudo using below mentioned command but it did not work.
apt-get install sudo
error -
Reading package lists... Done
Building dependency tree
Reading state information... Done
What are the possible solutions to fix this issue.
In general you shouldn't try to directly edit files in containers. Those changes will get lost as soon as the container is stopped and deleted; this happens extremely routinely since many Docker options can only be set at startup time, and the standard way to update the software in a container is to recreate it with a newer image. In your case, you also can't live-edit a configuration file the main container process needs at startup time, because it will have already read the configuration file by the time you're able to edit it.
The right way to do this is to inject the modified configuration file at container startup time. If you haven't already, get the default configuration file out of the image
docker run --rm mysql:8 cat /etc/mysql/my.cnf > my.cnf
and edit it, directly, on your host, using your choice of text editor. When you launch the container, inject the modified file
docker run -v $PWD/my.cnf:/etc/mysql/my.cnf ... mysql:8
or, in Docker Compose,
volumes:
- ./my.cnf:/etc/mysql/my.cnf
The Docker Hub mysql image documentation has some more suggestions on ways to set this; see "Using a custom MySQL configuration file" there.
While docker exec is an extremely useful debugging tool, it shouldn't be part of your core workflow, and I'd recommend trying to avoid it in cases like this. (With the bind-mount approach, you can add the modified config file to your source control system and blindly docker-compose up like normal without knowing this detail; a docker exec approach you'd have to remember and repeat by hand every time you started the container stack.)
Also note that you don't need sudo in Docker at all. Every context where you can run something (Dockerfiles, docker run, docker exec) has some way to explicitly specify the user ID, so you can docker exec -u root .... sudo generally depends on things like users having passwords and interactive prompting, which works well for administering a real Linux host but doesn't match a typical Docker environment.
The issue is not with sudo because you've already permissions to install pacakegs.
You should instead update package manager before to install new packages in order to update package repositories:
RUN apt-get update
RUN apt-get install nano
Mysql build the image with oracle linux, run the commands to install nano:
microdnf update
microdnf install nano sudo -y
And edit the my.cnf with nano

Docker and MariaDB/MySQL — Permanently Editing my.cnf to enable remote access

I am running Docker on a Macintosh, and have installed the MariaDB image. I would like to access it from another machine on the LAN.
I understand that the solution is to enable bind-address=0.0.0.0 (or something similar) in /etc/mysql/my.cnf. I executed docker exec -it mariadb bash, installed Joe text editor (because I am much more familiar with it than Vi or Nano), and edited the file.
The problem is that when I restart the Docker image,it has forgotten all the changes, and it doesn’t work.
Am I missing a step, or is this not the way to go about it?
Containers are throw-away by design and, as you noticed, any modifications are lost when you run fresh one.
You have two options:
First one is described here: Docker: editing my.cnf in when building the image (just mount your custom config and be done).
Second option is to make your custom container image based on official image + your modification, something like this:
Dockerfile:
# Lets say mariadb v10.3.28... Change for what you want.
FROM mariadb:10.3.28
# there is already `#bind-address=0.0.0.0` in /etc/mysql/my.cnf
# we use sed and replace it with `bind-address=0.0.0.0`)
RUN sed -i "s/#bind-address=0.0.0.0/bind-address=0.0.0.0/g" /etc/mysql/my.cnf && \
# and, for example, lets change `max_allowed_packet` too.
sed -i "s/max_allowed_packet.*/max_allowed_packet=512M/g" /etc/mysql/my.cnf;
(rule of thumbs is "make as many steps in single RUN as possible" to save image layers)
then build it:
$ cd /where/my/dockerfile/is
$ docker build . -t mymysql
and run it:
# In newer mariadb it should be `-e MARIADB_ROOT_PASSWORD=`
# And maybe you should mount datadir somewhere `-v /my/own/datadir:/var/lib/mysql`
$ docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw mymysql

Docker toolbox with mysql container problem on Windows 10 Home (and Pro)

I unable to run MySQL containers made from MySQL images with database volumes mapped to my host machine's folder.
It doesn't matter if the host folder is empty or with existing database files. I do know that Docker Toolbox could mount volumes on Windows only from c:\Users\ so my test folder is under that one.
I was trying different (official and not) MySQL images from 5.5 to latest with no result. Anytime when location /var/lib/mysql in container is pointing to a folder on my host machine (c:\Users\someuser\testfolder) I've got an error on container`s running with InnoDB error ("InnoDB: Operating system error number 22 in a file operation" or "InnoDB: File ./ib_logfile0: 'aio write' returned OS error 122").
I was trying to modify mysql container's /etc/my.cnf (under [mysqld] section, using "docker cp" command) adding "innodb_use_native_aio=OFF" or (sometimes even and) "innodb_use_native_aio=0" keys and even was trying to run "docker run" with "--user 1000:50" with no result either.
Just after I delete mount point from container's /var/lib/mysql to my host folder, the container runs normally.
There are many alike questions but no one has complete step by step solution how to run a MySQL container with a Docker Toolbox under Windows 10 (Home & Pro) to bring container work with an existing database on the host's volumes.
It took me a while to get an answer but finally everything worked! For those who are new to Docker and have problems mounting MySQL folder to the host here is a short guide. Please note I chose bitnami/mysql image for my experiments (for another images folders can be differ).
Create a folder c:\Users\[YourAccount]\MySQLData for MySQL data.
Create a folder c:\Users\[YourAccount]\MySQLConf for a custom MySQL config file.
Create a custom MySQL config file c:\Users\[YourAccount]\MySQLConf\my_custom.cnf and add two lines in it:
[mysqld]
innodb_use_native_aio=0
4. Now create and run the container mounting your custom config and data folder to it:
docker run -d --name mysql -e ALLOW_EMPTY_PASSWORD="yes" \
-v //c/Users/[YourAccount]/MySQLData:/bitnami/mysql/data \
-v //c/Users/[YourAccount]/MySQLConf/my_custom.cnf:/opt/bitnami/mysql/conf/my_custom.cnf:ro \ bitnami/mysql:latest
Hooray!

Docker db container running. Another process with pid <id> is using unix socket file

I'm trying to run a docker mysql container with initialized db according instruction provided in this message https://stackoverflow.com/a/29150538/6086816. After first run it works ok, but on second run, after trying of executing /usr/sbin/mysqld from script, I get this error:
db_1 | 2016-03-19T14:50:14.819377Z 0 [ERROR] Another process with pid 10 is using unix socket file.
db_1 | 2016-03-19T14:50:14.819498Z 0 [ERROR] Unable to setup unix socket lock file.
...
mdir_db_1 exited with code 1
what can be the reason of it?
I was facing the same issue. Following are the steps that I tried to resolve this issue -
Firsly, stop your docker service by using following command - "sudo service docker stop"
Now,get into the docker folder in my Linux system using the following path -
/var/lib/docker.
Then within the docker folder you need to get into the volumes folder. This folder contains the volumes of all your containers (memory of each container) -
cd /volumes
After getting into volumes do 'sudo ls' you will find multiple folders with hash names. These folders are volumes of your containers. Each folder is named after its hash
(You need to inspect your docker container and get the hash of your container volume. For this, you need to do the following steps -
Run command "docker inspect 'your container ID' ".
Now you will get a JSON file. It is the config file of your docker container.
Seach for Mounts key within this JSON file. In Mounts, you will get the Name(hash) of your volume. (You will also get the path of your volume within the Mounts. Within Mounts "Name" key is your volume name and "Source" is the path where your volume is located.)).
Once you get the name of your volume you can go within your volume folder and within this folder you will find "_data" folder. Get into this folder.
Finally within "_data" folder use sudo ls command and you will find a folder with the name mysql.sock.lock. Remove this folder By "rm -f mysql.sock.lock".
Now restart your docker service and then start your docker container. It will start working.
Note- Use sudo in each command while you are in the docker container folder.
You should make sure the socket file have been deleted before you start mysql.Check my.cnf(/etc/mysql/my.cnf) file to get the path of socket file.
find sth like this socket = /var/run/mysqld/mysqld.sock.And delete the .sock.lock file as well.
This is a glitch with docker.
Execute following commands:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
This will stop all containers and remove them.
After this it should work just fine.
Just faced same problem.
After many research, summary of my solution:
Find host location of docker files
$ docker inspect <container_name> --> Mounts.Source section
In my case, it was /var/snap/docker/common/.../_data
As root, you can ls -l that directory and see the files that are preventing your container from starting, the socket mysql.sock and the file mysql.sock.lock
Simply delete them as root ($ sudo rm /var/snap/.../_data/mysql.sock*) and start your docker container.
NOTE: be sure you don't have any other mysql.sock... files than those two. In that case don't use wildcar (*), delete each of them individually.
Hope this helps.
I had the same problem and got rid of it in an easy and mysterious way.
First I have noticed that I am unable to start mysql_container container. Running docker logs mysql_container indicated exactly the same problem as described repeating for few times.
I wanted to get a look around by running the container in an interactive mode by docker start -i mysql_container from one bash window while running things like
docker exec -it mysql_container cat /etc/mysql/my.cnf in another.
I have done that and was very surprised to see that this time the container started successfully. I cannot understand why. I can only guess that starting an interactive mode together with running subsequent docker exec commands slowed down init process and some another process had a bit more time to remove its locks.
Hope that helps anybody.