Home:ALL Converter>Connecting to MySQL Fails Using Bash in Terraform but works via SSH

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

Ask Time:2017-08-02T21:18:48         Author:uprightvinyl

Json Formatter

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

Author:uprightvinyl,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/45462032/connecting-to-mysql-fails-using-bash-in-terraform-but-works-via-ssh
yy