[英]Syntax error for init sql while starting a docker container with MySQL
I've got a problem.我有问题。 I'm trying to run a MySQL server in a docker container.我正在尝试在 docker 容器中运行 MySQL 服务器。 So far so good.到现在为止还挺好。 Now I would like to run a sql-file on startup of the container for initial stuff:现在我想在容器启动时运行一个 sql 文件以获取初始内容:
CREATE USER 'root'@'%' IDENTIFIED BY '1111';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
CREATE DATABASE testdb;
I'm adding the file in my docker-compose.yml with我在我的 docker-compose.yml 中添加文件
volumes: -/mysql/home:/docker-entrypoint-initdb.d
Also, in order to connect to my container remotely, I need to create another root user with the statements above!此外,为了远程连接到我的容器,我需要使用上述语句创建另一个 root 用户! These are necessary for my case.这些对于我的情况是必要的。 But all I get is a 1064 error (MySQL Error 1064: You have an error in your SQL syntax)
and I have no idea why.但我得到的只是 1064 错误(MySQL Error 1064: You have an error in your SQL syntax)
,我不知道为什么。 Syntax seems alright to me, I even deleted some lines to be sure.语法对我来说似乎没问题,我什至删除了一些行来确定。 The error stays the same.错误保持不变。 Why?为什么? Does Docker use a special syntax or something like that? Docker 是否使用特殊语法或类似的语法?
Thanks for helping!感谢您的帮助!
In docker container, root
is the default user for mysql having ALL PRIVILEGES .在 docker 容器中, root
是 mysql 的默认用户,拥有ALL PRIVILEGES 。 So,所以,
root
user & then do not set privileges manually.您不需要创建root
用户,然后不要手动设置权限。 Anyway, your query ( init.sql
under /mysql/home
directory) would like as:无论如何,您的查询( /mysql/home
目录下的init.sql
)将如下所示:
-- CREATE USER 'root'@'%' IDENTIFIED BY '1111';
-- GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
CREATE DATABASE testdb;
In docker-compose.yml, volumes
generally synchronizes contents for directory vs directory .在 docker-compose.yml 中, volumes
通常同步目录与目录的内容。 So, you need to remove file init.sql
from volumes
directive.因此,您需要从volumes
指令中删除文件init.sql
。 So, the docker-compose.yml could be look like this:因此, docker-compose.yml可能如下所示:
version: '3'
services:
mysql:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: "1111"
ports:
- 3306:3306
volumes:
- /mysql/home:/docker-entrypoint-initdb.d
container_name: mysql
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.