You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MySQL docker 5.7.6 and later fails to initialize database #69
When using the docker image to start without an existing database, the container's entrypoint.sh script tries to call the mysqld binary to create the database. This fails in versions later than 5.7.5 because the script starts by calling mysqld --verbose --help to get the configured datadir and when that runs and there is no database, it initializes it automatically.
When the script then calls --initialize, that fails with the error:
[ERROR] --initialize specified but the data directory has files in it. Aborting.
The workaround is to run mysql:5.7.5 to create the database, then upgrade to a later image, which will work because the database already exists.
Reacted by Amit Kumar, Tasos Maroudas, Suren, Andrés Felipe Piñeros, Alex Ivasyuv, Doodad/Doodead, Max Rumpf, kojima, semaz, Piotr Sobieszczański and 10 more
The problem is one of permissions: the container has the data dir /var/lib/mysql owned by root, so when entrypoint.sh is calling mysqld --help to get the data dir, the directory isn't writable by mysqld and mysqld can't write the innodb log files it will normally create automatically when there is no database already set up. entrypoint.sh will later chown the directory.
The problem is that the container relies on this behavior for successful initialization, but it can break when using a volume to mount the data dir from another long-running file system (whether on the host or another container): when a the container is started on an empty volume, the mysql user may have write permissions there (because the volume is writable to anyone or because the volume was previously used to host another mysql database and the admin just rm -rf $vol/* it). If that is the case, then the first call to mysqld --help will create the innodb log files and then initialization will fail.
Reacted by Rafael Turk, James▪Pei, Ed Sweeney, Maarten, Christopher Simon, Jonathan Stucklen, Sergio, Rishav and Robert Heim
There's a better workaround for this; running mysqld --verbose --help with the --innodb-read-only option prevents it from creating the files. Could maybe make a PR for this, as it's not as ugly :)
The reason it's not necessary is that mysqld doesn't have write access to /var/lib/mysql on the Debian image (permissions on $DATADIR are set after --verbose --help is run), so it'll simply fail to create the files. On the OL image ownership on the folder is set when the server is installed, so this workaround is needed even with the default setup.
The problem for the official image is that if the user provides a --datadir that is writable from the start, it'll create the files and initialization will fail.
Reacted by Jay Dorsey, Victor Adossi ("vados"), Shukri Adams, Matej Žilák, Nathan Walston, ., Ralf Vitasek, oclausen, Vittorio Morganti, Florian Ajir and 37 more
Reacted by Jay Vercellone, Wout and Methrat0n
Reacted by Jose, Vittorio Morganti, Copriwolf Teng, Valentino Pistis, Maarten, Maxime Louet, A-J Roos, Veysel Şahin, Dang Nguyen and Jay Vercellone
Reacted by Vittorio Morganti, Reyn Tancio, karthik101, Valentino Pistis, Maarten, Fumiya Funatsu, Mohamed Hafez, A-J Roos, Veysel Şahin, Dang Nguyen and 3 more
The MySQL and MariaDB images are for the most part maintained by the same people and process, one working as well as the other (or as badly). This bug is for MySQL 5.7, which doesn't have a comparable MariaDB version.
mine isn't fixed yet. i am still getting 'initialized specified but the data directory has files in it' i tried to clear the directories and made clean start.
Reacted by Petro S, Sylvain Leroy, Jacky Wang, TuNA, Naveen D N, Jose and losymear
I have this problem too. Currently I delete the volume and let kubernetes recreate one if one pod fails to start, but that is not really a nice solution.
Reacted by Michael Cwienczek, Petro S, Sylvain Leroy, Jacky Wang, Naveen D N, Maycon Brito and Tom Raganowicz
I resolved this issue by removing --default-time-zone=UTC from my startup. I assume this is because the tables are not setup quite yet and it puts log files for the error into my /var/lib/mysql/ folder. Maybe this will help someone out.
I'm trying to set up docker for the first time and I appreciate everyone's help in getting it fixed.
I get the "mysqld: Can't create/write to file '/var/mysql/data/init.sql' (Errcode: 2 "No such file or directory")" and here is my docker-compose.yaml:
`version: '3'
services:
php:
build:
context: ./symfony
container_name: php
depends_on:
- mysql
env_file:
- ./symfony/.env
# Comment out these volumes in production
volumes:
- ./symfony:/srv/symfony:rw,cached
# If you develop on Linux, comment out the following volumes to just use bind-mounted project directory from host
environment:
# If you develop on Windows change this to remote_host=docker.for.win.localhost
# If you develop on Linux change this to remote_host=172.17.0.1
XDEBUG_CONFIG: "remote_host=docker.for.mac.localhost idekey=IDE_XDEBUG"
BLACKFIRE_CLIENT_ID: ${BLACKFIRE_CLIENT_ID}
BLACKFIRE_CLIENT_TOKEN: ${BLACKFIRE_CLIENT_TOKEN}
symfony:
build:
context: ./symfony
dockerfile: Dockerfile.nginx
container_name: symfony
depends_on:
- php
ports:
- "8080:80"
volumes:
- ./symfony/public:/srv/symfony/public:ro
# Optional mysql database - uncomment and replace "depends_on: [ postgres ] with mysql
mysql:
image: mysql
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: symfony
MYSQL_PASSWORD: symfony
command: --log-bin=/var/lib/mysql/mysql-bin.log --binlog-format=ROW --server-id=1 --init-file /var/mysql/data/init.sql
# You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
volumes:
- ./symfony/docker/mysql:/var/mysql/data:ro
- db-data:/var/lib/mysql:rw
adminer:
image: adminer
container_name: adminer
depends_on:
- mysql
restart: always
ports:
- 2000:8080
blackfire:
image: blackfire/blackfire
container_name: blackfire
depends_on:
- php
environment:
BLACKFIRE_SERVER_ID: ${BLACKFIRE_SERVER_ID}
BLACKFIRE_SERVER_TOKEN: ${BLACKFIRE_SERVER_TOKEN}
BLACKFIRE_LOG_LEVEL: 4
ports:
- "8707:8707"
h2-proxy:
# Don't use this proxy in prod
build:
context: ./h2-proxy
dockerfile: ./Dockerfile
container_name: h2-proxy
depends_on:
- symfony
ports:
- "80:80"
- "443:443"
volumes:
db-data: {}
@maykino, you probably want to drop --init-file /var/mysql/data/init.sql and instead just mount your folder containing the sql file to /docker-entrypoint-initdb.d/ and it will automatically be run on first initialization of the data directory. Use docker volume rm db-data to clear out the old data directory after stopping and deleting the container.
mysql:
image: mysqlcontainer_name: mysqlenvironment:
MYSQL_ROOT_PASSWORD: rootMYSQL_USER: symfonyMYSQL_PASSWORD: symfonycommand: --log-bin=/var/lib/mysql/mysql-bin.log --binlog-format=ROW --server-id=1# You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!volumes:
# this will run all .sh, .sql and .sql.gz files found
- ./symfony/docker/mysql:/docker-entrypoint-initdb.d/:ro
- db-data:/var/lib/mysql:rw
Actually this is the path and you should mention a valid path for this to work. If your data directory is in current directory then instead of my-data you should mention ./my-data, otherwise it will give you that error in mysql and mariadb also.
Hi there,
I would like to build an image prefilled with some data to run tests. So, idea is to build the image once and just restart it on every test iteration to use the same snapshot every time.
mysqltest | 2019-05-09T09:42:47.557600Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
mysqltest | 2019-05-09T09:42:47.558842Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
mysqltest | 2019-05-09T09:42:47.559241Z 0 [ERROR] Aborting
@yosifkit , I've grab an idea of mounting entry dir from your comment, but it doesn't work for me. Could you advise why my installation doesn't work?
Anybody else knows? Just curious if nobody run tests in such manner?
I'm facing a problem, I have tried everything with no luck.
[ERROR] [Server] --initialize specified but the data directory has files in it. Aborting
[System] [Server] /usr/sbin/mysqld (mysqld 8.0.19) initializing of server in progress as process 231
[ERROR] [Server] --initialize specified but the data directory has files in it. Aborting.
[ERROR] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
[ERROR] [Server] Aborting
[System] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.19) MySQL Community Server - GPL.
It's closed because the original issue was resolved, and there have since been many unrelated issues posted here, mostly requests for support (and this isn't a proper support forum).
Activity
md5 commentedon May 3, 2015
The
mysql:5.7.6image was tested with this test before release: https://github.com/docker-library/official-images/blob/master/test/tests/mysql-basics/run.shYou may need to provide some more information about your failure.
guss77 commentedon May 4, 2015
The problem is one of permissions: the container has the data dir
/var/lib/mysqlowned by root, so whenentrypoint.shis callingmysqld --helpto get the data dir, the directory isn't writable by mysqld and mysqld can't write the innodb log files it will normally create automatically when there is no database already set up.entrypoint.shwill later chown the directory.The problem is that the container relies on this behavior for successful initialization, but it can break when using a volume to mount the data dir from another long-running file system (whether on the host or another container): when a the container is started on an empty volume, the mysql user may have write permissions there (because the volume is writable to anyone or because the volume was previously used to host another mysql database and the admin just
rm -rf $vol/*it). If that is the case, then the first call tomysqld --helpwill create the innodb log files and then initialization will fail.ltangvald commentedon May 11, 2015
This is a known issue with MySQL 5.7.6 and later, and is being looked into:
http://bugs.mysql.com/bug.php?id=75995.
We have a temporary workaround in the mysql/mysql-server:5.7 image:
https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/docker-entrypoint.sh
Note that the whole if-file-exists check can probably be replaced with a simple rm -f
ltangvald commentedon Jun 11, 2015
There's a better workaround for this; running mysqld --verbose --help with the --innodb-read-only option prevents it from creating the files. Could maybe make a PR for this, as it's not as ugly :)
tianon commentedon Jun 11, 2015
👍 🤘
It's not necessary in our latest version though, is it? Are we still creating those files prematurely?
ltangvald commentedon Jun 12, 2015
The reason it's not necessary is that mysqld doesn't have write access to /var/lib/mysql on the Debian image (permissions on $DATADIR are set after --verbose --help is run), so it'll simply fail to create the files. On the OL image ownership on the folder is set when the server is installed, so this workaround is needed even with the default setup.
The problem for the official image is that if the user provides a --datadir that is writable from the start, it'll create the files and initialization will fail.
yosifkit commentedon Jul 28, 2015
@guss77 did #78 fix this for you?
Change mysql image version and volume id
bittner commentedon Dec 28, 2016
Disclaimer
Not sure if I will be threatened to death for saying this, but ... well, see below.
ℹ️ This is meant as a help for people looking for a solution for their project, not for this project.
Use MariaDB
Instead of using the mysql image people may use the mariadb image, which works flawlessly with Docker volumes. Example
docker-compose.ymlfile:ltangvald commentedon Jan 2, 2017
The MySQL and MariaDB images are for the most part maintained by the same people and process, one working as well as the other (or as badly). This bug is for MySQL 5.7, which doesn't have a comparable MariaDB version.
jonathan-kosgei commentedon Mar 24, 2017
This trickles down to other providers, I hit this running a percona cluster backed by glusterfs
ramizrshaik commentedon Apr 25, 2017
mine isn't fixed yet. i am still getting 'initialized specified but the data directory has files in it' i tried to clear the directories and made clean start.
kersten commentedon Apr 28, 2017
I have this problem too. Currently I delete the volume and let kubernetes recreate one if one pod fails to start, but that is not really a nice solution.
jcollum commentedon Nov 3, 2017
@ltangvald re: https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/docker-entrypoint.sh -- how do I use this in a docker-compose?
21 remaining items
kellyjandrews commentedon Sep 13, 2018
I resolved this issue by removing
--default-time-zone=UTCfrom my startup. I assume this is because the tables are not setup quite yet and it puts log files for the error into my/var/lib/mysql/folder. Maybe this will help someone out.maykino commentedon Oct 31, 2018
I'm trying to set up docker for the first time and I appreciate everyone's help in getting it fixed.
I get the "mysqld: Can't create/write to file '/var/mysql/data/init.sql' (Errcode: 2 "No such file or directory")" and here is my docker-compose.yaml:
`
Here is the full output I'm getting:
yosifkit commentedon Oct 31, 2018
@maykino, you probably want to drop
--init-file /var/mysql/data/init.sqland instead just mount your folder containing the sql file to/docker-entrypoint-initdb.d/and it will automatically be run on first initialization of the data directory. Usedocker volume rm db-datato clear out the old data directory after stopping and deleting the container.maykino commentedon Nov 1, 2018
@yosifkit wow! It worked! Thanks a lot! ###
*** SQUASH 488 commits ***
*** SQUASH 488 commits ***
ktamas77 commentedon Apr 3, 2019
this is the solution.
SCIF commentedon May 9, 2019
Hi there,
I would like to build an image prefilled with some data to run tests. So, idea is to build the image once and just restart it on every test iteration to use the same snapshot every time.
and got this:
@yosifkit , I've grab an idea of mounting entry dir from your comment, but it doesn't work for me. Could you advise why my installation doesn't work?
Anybody else knows? Just curious if nobody run tests in such manner?
jonesmith518 commentedon Oct 28, 2019
Got the same error, but .index file was found inside MySQL data dir which prevent initializing.
thearyanahmed commentedon Feb 3, 2020
Still have the bug on 2020, should have been fixed a long time ago. Using mariadb as a bypass for the time being as @bittner said
albanafmeti commentedon Feb 4, 2020
I had a similar issue with the same error message.
I fixed with
Then restart again and it's done.
But this resolved my kind of issue, it doesn't mean it can resolve yours :)
boay24 commentedon Feb 25, 2020
I'm facing a problem, I have tried everything with no luck.

[ERROR] [Server] --initialize specified but the data directory has files in it. Aborting
[System] [Server] /usr/sbin/mysqld (mysqld 8.0.19) initializing of server in progress as process 231
[ERROR] [Server] --initialize specified but the data directory has files in it. Aborting.
[ERROR] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
[ERROR] [Server] Aborting
[System] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.19) MySQL Community Server - GPL.
WSL2 INSIDER
MYSQL 8.0
RAM 16G
alexdrupal commentedon Aug 4, 2020
Same here. Why does it closed? I had to downgrade to 5.7.5 in order to make my docker-compose.yml file working.
tianon commentedon Aug 4, 2020
It's closed because the original issue was resolved, and there have since been many unrelated issues posted here, mostly requests for support (and this isn't a proper support forum).
I would recommend trying the Docker Community Forums, the Docker Community Slack, or Stack Overflow for further help diagnosing your deployment.