'Сan't automatically connect to RDS via the script on EC2?

Im use terraform to create infrastr. in AWS. I'm using script on ec2 userdata, that connects to rds.But this script doesn`t work.

#! /bin/bash 
yum update -y
yum install -y httpd
service httpd start
usermod -a -G apache centos
chown -R centos:apache /var/www
yum install -y mysql php php-mysql
systemctl enable httpd.service
cd /var/www/html/
echo "[mysql]" > ~/.my.cnf
echo "user = myuser" >> ~/.my.cnf
echo "password = passworddata" >> ~/.my.cnf
chmod 600 ~/.my.cnf   
cd database/
mysql -h db_server_address < script.sql
systemctl restart httpd.service

/var/log/cloud-init-output.log ERROR 1045 (28000): Access denied for user 'root'@'10.0.1.91' (using password: NO)

Application cannot connect to the database because there is no schema created. But, when I do it manually into the instance everything works fine.I understand that script is not perfect, but what's the problem,why the script don`t take credentials from ~/.my.cnf?



Solution 1:[1]

Probably env variables are not loaded to load data from /root, use:

mysql --defaults-file=~/.my.cnf -h db_server_address < script.sql

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marek Knappe