我的java应用程序可以在运行我的java应用程序时自动生成表并连接数据库,因为我已经将mysql的用户名和密码配置到java应用程序代码中。但是对于使用packer在EC2中安装mysql来说,它会有一个临时密码,我的java应用程序无法连接mysql?在这种情况下,我该怎么办,我应该使用docker来避免这个问题吗?我将非常感谢任何帮助。
由于临时密码问题,当我在 EC2 中运行 java 应用程序时,我总是出错
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Caused by: org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction
Setup.sh代码:
echo '[mysqld]' | sudo tee -a /etc/my.cnf
echo 'skip-networking=0' | sudo tee -a /etc/my.cnf
echo 'bind-address=0.0.0.0' | sudo tee -a /etc/my.cnf
echo 'max_connections=100' | sudo tee -a /etc/my.cnf
echo 'wait_timeout=1200' | sudo tee -a /etc/my.cnf
echo 'interactive_timeout=1200' | sudo tee -a /etc/my.cnf
echo 'lower_case_table_names=1' | sudo tee -a /etc/my.cnf
echo '[client]' | sudo tee -a /etc/my.cnf
echo 'host=localhost' | sudo tee -a /etc/my.cnf
echo 'port=3306' | sudo tee -a /etc/my.cnf
echo 'user=root' | sudo tee -a /etc/my.cnf
echo 'password=Amazaon#2023' | sudo tee -a /etc/my.cnf
# Update the system
sudo yum update -y
sleep 10
# Install MySQL
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
sudo yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm
sudo yum install -y mysql-community-server
sudo yum install -y mysql-connector-java
# Start the MySQL service and enable it to start on boot
sudo systemctl start mysqld
sudo systemctl enable mysqld
# Install the MySQL Connector/J driver for Java
sudo yum install -y mysql-connector-java
# Restart the MySQL service
#sudo systemctl restart mysqld