Java Application can not connect MySQL in Amazon Linux EC2 - mysql

My java app can automatically generate table and connect database when I run my java application since I already configure the username and password of mysql into java application codes. But in terms of installing mysql in EC2 using packer, it will have a temporary password, my java application could not connect mysql? In this case, what can I do, should I use docker to avoid this issue? I will highly appreciated any help.
I always have a error, when I run java application in EC2 due to temporary password issue
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
Set the MySQL root password in advance
Setup.sh codes:
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

Related

С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?
Probably env variables are not loaded to load data from /root, use:
mysql --defaults-file=~/.my.cnf -h db_server_address < script.sql

Docker MySQL automatical Installation and configuration

On old MySQL 5.6 I was automatically installing it and creating all needed users and passwords, using the code below.
Is there a way to do the same on a new MySQL 8.0 so that it automatically installs into docker containers allows root without a password and creates all the above?
RUN yum -y install https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
RUN yum-config-manager --disable mysql57-community; yum-config-manager --enable mysql56-community
RUN yum -y install mysql-community-server mysql-community-client
#Configure MySQL
# Needed to create system tables etc.
chown -R mysql:mysql /var/lib/mysql
su -l mysql -c "/usr/bin/mysql-systemd-start pre"
# Start main service
(su -l mysql -c "/usr/bin/mysqld_safe --basedir=/usr") &
# Don't signal startup success before a ping works
su -l mysql -c "/usr/bin/mysql-systemd-start post"
mysql -e "CREATE DATABASE IF NOT EXISTS \`${USERNAME}\`;"
mysql -e "GRANT ALL PRIVILEGES ON \`${USERNAME}\`.* TO \`${USERNAME}\`#'%' IDENTIFIED BY \"${MYSQL_PASS}\";"
mysql -e "GRANT ALL PRIVILEGES ON \`${USERNAME}\`.* TO \`${USERNAME}\`#'localhost' IDENTIFIED BY \"${MYSQL_PASS}\";"
mysqladmin shutdown

MySQL service inside docker container not working in macOS Sierra 10.12.6

I was forced to reinstall macOS Sierra because I was in the beta program for High Sierra and I get a serious crash so I downgraded the system.
This Dockerfile was working in High Sierra before the sudden crash of the system.
FROM ubuntu:16.04
MAINTAINER XXX version 0.0.1
# Prepare Debian environment
ENV DEBIAN_FRONTEND noninteractive
# we don't need an apt cache in a container
RUN echo "Acquire::http {No-Cache=True;};" > /etc/apt/apt.conf.d/no-cache
# ----------------------------
# Configure supervisor
# ----------------------------
RUN apt-get update > /dev/null 2>&1 && apt-get install -y supervisor > /dev/null 2>&1
RUN mkdir -p /var/log/supervisor
COPY files/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
## Mysql
RUN apt-get install -y mysql-client > /dev/null 2>&1
#RUN debconf-set-selections <<< 'mysql-server mysql-server/root_password password 1234'
#RUN debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password 1234'
RUN echo 'mysql-server mysql-server/root_password password 1234' | debconf-set-selections
RUN echo 'mysql-server mysql-server/root_password_again password 1234' | debconf-set-selections
RUN apt-get -y install mysql-server > /dev/null 2>&1
RUN sed -i -e 's/127.0.0.1/0.0.0.0/g' /etc/mysql/mysql.conf.d/mysqld.cnf
RUN echo "sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE" >> /etc/mysql/mysql.conf.d/mysqld.cnf
RUN usermod -d /var/lib/mysql/ mysql
mys
ADD files/xxx.dump /tmp/xxx.dump
ADD files/mysql_xxx.sql /tmp/mysql_xxx.sql
RUN service mysql start && \
mysql -uroot -p1234 < /tmp/mysql_xxx.sql
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN apt-get clean
EXPOSE 3306
CMD ["/usr/bin/supervisord"]
Now, reinstalling all things I does not work, it always outputs the same error:
Starting MySQL database server mysqld
...fail!
To add some information, in the step where the Dockerfile stops I am starting the service to dump a database.
My system version macOS Sierra 10.12.6 (16G29) is and my Docker version is 17.06.0-ce-mac19 (18663). Any workaround for this problem?
You should be using the official MySQL Dockerfile as your starting point. You could either just use the MySQL image itself, FROM mysql:5.7 for your Dockerfile or you can copy the Dockerfile and the docker-entrypoint,sh from their Github to "take control" of the MySQL image and not have any dependency on MySQL releases/changes.
I think it is a good idea to use the same OS base for all your Docker containers, you are currently using Ubuntu distro, if you do not mind or are just getting started you may want to have Debian be your OS base. I say this because the Docker images I have looked so far, Cloudbee's Jenkins, .NET Core, ASP.NET COre and MySQL all have a Debian base so I been thinking Debian is the most popular OS base for Docker images though I only have anecdotal evidence of this obviously.
My company prefers CentOS distro, so I took the official MySQL Dockerfile and converted to work with CentOS:7, it was a pain.

Install MySQL Community Server 5.7 via bash shell script in CentOS 7 (x64)

I am trying to create a bash shell script to automate the installation of MySQL Community Server version 5.7 on CentOS 7 (x64).
I came across this lovely script https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/docker-entrypoint.sh and put together the following:
#!/bin/sh
DATADIR="/var/lib/mysql"
MYSQL_ROOT_PASSWORD="$(pwmake 128)"
echo ' -> Removing previous mysql installation';
systemctl stop mysqld.service && yum remove -y mysql-community-server && rm -rf /var/lib/mysql && rm -rf /var/log/mysqld.log
echo ' -> Installing mysql database server';
yum localinstall -y https://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum install -y mysql-community-server
echo ' -> Creating mysql data directory'
mkdir -p "$DATADIR"
chown -R mysql:mysql "$DATADIR"
echo ' -> Initializing mysql database'
mysqld --initialize-insecure=on --user=mysql --datadir="$DATADIR"
mysqld --user=mysql --datadir="$DATADIR" --skip-networking & pid="$!"
mysql=( mysql --protocol=socket -uroot )
for i in {30..0}; do
if echo 'SELECT 1' | "${mysql[#]}" &> /dev/null; then
break
fi
echo 'MySQL init process in progress ...'
sleep 1
done
if [ "$i" = 0 ]; then
echo >&2 'MySQL init process failed'
exit 1
fi
echo ' -> Setting mysql server root password';
mysql_tzinfo_to_sql /usr/share/zoneinfo | "${mysql[#]}" mysql
"${mysql[#]}" <<-EOSQL
SET ##SESSION.SQL_LOG_BIN=0;
DELETE FROM mysql.user where user != 'mysql.sys';
CREATE USER 'root'#'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
GRANT ALL ON *.* TO 'root'#'%' WITH GRANT OPTION ;
DROP DATABASE IF EXISTS test ;
FLUSH PRIVILEGES ;
EOSQL
if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
fi
if ! kill -s TERM "$pid" || ! wait "$pid"; then
echo >&2 'MySQL init process failed.'
exit 1
fi
chown -R mysql:mysql "$DATADIR"
echo " -> Mysql server setup completed, your root password: $MYSQL_ROOT_PASSWORD"
The script is supposed to do the following:
Removing previous mysql installation
Installing mysql database server
Creating mysql data directory & set ownership
Initializing mysql database
Setting mysql server root password
When I ran the script, this is the output I've got:
Total download size: 142 M
Installed size: 652 M
Downloading packages:
mysql-community-server-5.7.10-1.el7.x86_64.rpm | 142 MB 00:00:04
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql-community-server-5.7.10-1.el7.x86_64 1/1
Verifying : mysql-community-server-5.7.10-1.el7.x86_64 1/1
Installed:
mysql-community-server.x86_64 0:5.7.10-1.el7
Complete!
-> Creating mysql data directory
-> Initializing mysql database
MySQL init process in progress ...
MySQL init process in progress ...
...snipped...
MySQL init process in progress ...
MySQL init process in progress ...
MySQL init process failed
I thought the steps in the script were pretty straight forward, yet it failed. Any ideas why it might be?
I have managed to achieve this by re-writing the shell script. This now works flawlessly for me :)
#!/bin/bash
mysqlRootPass="$(pwmake 128)"
echo ' -> Removing previous mysql server installation'
systemctl stop mysqld.service && yum remove -y mysql-community-server && rm -rf /var/lib/mysql && rm -rf /var/log/mysqld.log && rm -rf /etc/my.cnf
echo ' -> Installing mysql server (community edition)'
yum localinstall -y https://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum install -y mysql-community-server
echo ' -> Starting mysql server (first run)'
systemctl enable mysqld.service
systemctl start mysqld.service
tempRootDBPass="`grep 'temporary.*root#localhost' /var/log/mysqld.log | tail -n 1 | sed 's/.*root#localhost: //'`"
echo ' -> Setting up new mysql server root password'
systemctl stop mysqld.service
rm -rf /var/lib/mysql/*logfile*
wget -O /etc/my.cnf "https://my-site.com/downloads/mysql/512MB.cnf"
systemctl start mysqld.service
mysqladmin -u root --password="$tempRootDBPass" password "$mysqlRootPass"
mysql -u root --password="$mysqlRootPass" -e <<-EOSQL
DELETE FROM mysql.user WHERE User='';
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
DELETE FROM mysql.user where user != 'mysql.sys';
CREATE USER 'root'#'%' IDENTIFIED BY '${mysqlRootPass}';
GRANT ALL ON *.* TO 'root'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOSQL
systemctl status mysqld.service
echo " -> MySQL server installation completed, root password: $mysqlRootPass";
echo 'First step : Disabling and removing partially MariaDB'
yum remove MariaDB-common MariaDB-compat MariaDB-server
yum repolist enabled | grep "mariadb.*"
echo 'Removing Percona'
yum remove Percona-Server-client-55 Percona-Server-server-55
Percona- Server-shared-55.i686 percona-release
echo 'Last step : Removing completely mariadb'
yum remove mariadb mariadb-server
echo 'Removing mysql'
rm /etc/my.cnf
yum remove mysql-server mysql-libs mysql-devel mysql
echo 'Installing mysql assuming the rpm file is already download'
yum localinstall mysql57-community-release-el7-7.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"
yum install mysql-community-server
echo 'Starting mysql'
systemctl enable mysqld
systemctl start mysqld
echo 'Finally the root password needed for mysql to start'
echo 'oldpass will contain the temporary password value'
echo 'newpass must be written here and must meet Mysql Password Policies'
oldpass=$( grep 'temporary.*root#localhost' /var/log/mysqld.log |
tail -n 1 | sed 's/.*root#localhost: //' )
newpass="Password!"
mysqladmin -u root --password=${oldpass} password $newpass

Install MySQL 5.5 in Ubuntu 11.10

I have been downloaded the DEB package of mysql 5.5.23.
Install MySQL by the command as follows:
sudo dpkg -i mysql-5.5.23-debian6.0-x86_64.deb
The result said that installed successfully.
But when I type the command:
sudo service mysql start
Or
mysql
It shows me that "mysql: unrecognized service" or "mysql: command not found".
Besides, I can't find MySQL files in "/usr/include" or "/usr/bin"
Please help me. Thx.
Because MySQL 5.5.x is not packaged, in repositories only exist 5.1.x
This worked for me:
http://www.rebojo.com/debian-installing-mysql/
Why not a simple command :
sudo apt-get install mysql-server
MySQL Download URL
https://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.56-linux-glibc2.5-x86_64.tar.gz
Open the terminal and follow along:
Uninstall any existing version of MySQL
sudo rm /var/lib/mysql/ -R
Delete the MySQL profile
sudo rm /etc/mysql/ -R
Automatically uninstall mysql
sudo apt-get autoremove mysql* --purge
sudo apt-get remove apparmor
Download version 5.5.51 from MySQL site
wget https://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.56-linux-glibc2.5-x86_64.tar.gz
Add mysql user group
sudo groupadd mysql
Add mysql (not the current user) to mysql user group
sudo useradd -g mysql mysql
Extract it
sudo tar -xvf mysql-5.5.56-linux-glibc2.5-x86_64.tar.gz
Move it to /usr/local
sudo mv mysql-5.5.56-linux-glibc2.5-x86_64 /usr/local/
Create mysql folder in /usr/local by moving the untarred folder
cd /usr/local
sudo mv mysql-5.5.49-linux2.6-x86_64 mysql
set MySql directory owner and user group
cd mysql
sudo chown -R mysql:mysql *
Install the required lib package (works with 5.6 as well)
sudo apt-get install libaio1
Execute mysql installation script
sudo scripts/mysql_install_db --user=mysql
Set mysql directory owner from outside the mysql directory
sudo chown -R root .
Set data directory owner from inside mysql directory
sudo chown -R mysql data
Copy the mysql configuration file
sudo cp support-files/my-medium.cnf /etc/my.cnf
Start mysql
sudo bin/mysqld_safe --user=mysql &
sudo cp support-files/mysql.server /etc/init.d/mysql.server
Set root user password
sudo bin/mysqladmin -u root password '[your new password]'
Add mysql path to the system
sudo ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
Reboot!
Start mysql server
sudo /etc/init.d/mysql.server start
Stop mysql server
sudo /etc/init.d/mysql.server stop
Check status of mysql
sudo /etc/init.d/mysql.server status
Enable myql on startup
sudo update-rc.d -f mysql.server defaults
*Disable mysql on startup (Optional)
`sudo update-rc.d -f mysql.server remove`
REBOOT!
Now login using below command, start mysql server if it's not running already
mysql -u root -p