简体   繁体   中英

Connecting to MySQL Fails Using Bash in Terraform but works via SSH

I'm running the following bash script via Terraform during the creation of CentOS server.

"sudo docker run -d --restart=unless-stopped --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=${var.mysql_root_password} mysql",
"sudo yum -y install mysql",
"mysql -u root -p${var.mysql_root_password} -h 127.0.0.1"

But the MySQL command fails with the following output

ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

However, if I connect via SSH to server, I can run the mysql command and successfully connect to MySQL.

I have shortened the mysql command above to simplify my question. I was trying to use this command to connect to the MySQL instance and create a user and database. I have managed to get that to work by passing the username and database I want to create via environment variables as part of the docker run command. However, I'm interested to know why the above does't work via Terraform but does work when I SSH to the server.

Cheers

Chris

You need to wait. Start a database container and waiting the service ready.

"sudo docker run -d --restart=unless-stopped --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=${var.mysql_root_password} mysql",
"sudo yum -y install mysql",
sleep 180
"mysql -u root -p${var.mysql_root_password} -h 127.0.0.1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM