没有这样的文件或目录使用脚本文件

时间:2017-04-11 08:03:44

标签: linux bash docker

当我启动mysql时,即使文件存在于正确的目录中,我也收到'没有这样的文件或目录'。

手创乐团:

#!/bin/bash

/usr/bin/docker exec -i mysql /bin/bash <<EOF
mysql -uroot -ppasswd 
CREATE DATABASE theDB CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL PRIVILEGES on theDB.* to user@'%' IDENTIFIED BY 'passwd';
FLUSH PRIVILEGES;
EOF

docker.yml:

version: "2"
services:
  mysql:
    image: mysql:latest
    restart: always
    container_name: mysql
    environment:
      -  MYSQL_ROOT_PASSWORD=passwd
    volumes:
      -  /temp/mysql_cnf:/etc/mysql/mysql.conf.d
    ports:
      -  3306:3306
    command: /bin/sh -c "chmod +x /home/user/mysql-container/thescript.sh && /bin/sh /home/user/mysql-container/thescript.sh"

2 个答案:

答案 0 :(得分:0)

乍一看,我怀疑是因为你的内联段被解释为多个命令。尝试添加&#34; \&#34;抑制EOL:

/usr/bin/docker exec -i mysql /bin/bash <<EOF
mysql -uroot -ppasswd  \
CREATE DATABASE theDB CHARACTER SET utf8 COLLATE utf8_general_ci; \
GRANT ALL PRIVILEGES on theDB.* to user@'%' IDENTIFIED BY 'passwd'; \
FLUSH PRIVILEGES; 
EOF

答案 1 :(得分:0)

CREATE不是有效的Bash命令。无论如何,Bash包装外壳在这里似乎是多余的。

#!/bin/bash    
/usr/bin/docker exec -i mysql mysql -uroot -ppasswd <<'EOF'
CREATE DATABASE theDB CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL PRIVILEGES on theDB.* to user@'%' IDENTIFIED BY 'passwd';
FLUSH PRIVILEGES;
EOF