mysql—通过ssh使用sql脚本失败,但在主机上直接启动时失败

vvppvyoh  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(540)

我配置了一个jenkins作业,我的管道如下所示:

stage ('Preparing SQL Database'){
        steps{
            //Starting MYSQL Image
            sh '(sudo sshpass -p "PASSWORD" ssh -o StrictHostKeyChecking=no  $user@$host "sudo docker run --name nameofthecontainer -p 13306:3306 -d mysql:mysql")'

            sh '(sudo sshpass -p "PASSWORD" ssh -o StrictHostKeyChecking=no  $user@host "/opt/docker/dockerdeployment/databasescript.sh")'
        }

我的databasescript.sh如下所示(仅导入sql转储):

sudo docker exec -i nameofthecontainer  /usr/bin/mysql -u $MYSQLUSER -pPassword $MYSQLOPTIONS $MYSQLDB -e "DROP DATABASE IF EXISTS ${MYSQLDB};CREATE DATABASE ${MYSQLDB} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; commit;"
sudo docker exec -i nameofthecontainer  /usr/bin/mysql -u $MYSQLUSER -pPassword $MYSQLOPTIONS $MYSQLDB < /srv/Dump.sql

done

当我直接在主机上执行脚本时,它成功了。但是,当我尝试运行jenkins作业(通过ssh执行此databasescript的人)时,出现以下错误消息:

ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
(...)
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

有人能帮忙吗?

0ejtzxu1

0ejtzxu11#

我终于解决了。问题是,在执行第二步时,mysql容器还没有准备好接受连接。我在两个台阶之间用睡眠命令把它修好了。

相关问题