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

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

Related

Java Application can not connect MySQL in Amazon Linux EC2

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

Why bash script, that must creat and run zabbix server, execute without errors, but result is wrong?

everyone. Ive started to learn bash and DevOps, lost 2 days, but I cant understand the following:
requirments
Centos 7
DB mysql
zabbix server version 4
mariadb
I need to write bash script (script at the end of the question), that creates and runs zabbix server (exactly 4 version). When I run the script manually step by step (inserting each command in pwsh by hand) in two ways for db creating (example1 or example2) - it is executed properly: zabbix db is created, all services started, zabbix frontend is working well.
But when I run the script as root user or as local user by command:
>sh -c /home/zabbix_server
script is finished by:
>Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.
>Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
>Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
there are no error messages during or at the end of the script, but when I try manually execute the command create db (example1 or example2), it's doesn't matter:
#Create DB (example1)
mysql -uroot <<EOF
create database zabbix character set utf8 collate utf8_bin;
create user 'zabbix'#'localhost' identified by 'zabbix';
grant all privileges on zabbix.* to 'zabbix'#'localhost';
EOF
#Create DB (example2)
mysql -u root -e "CREATE DATABASE zabbix; CREATE USER zabbix#localhost identified by 'zabbix'; GRANT ALL ON zabbix.* to zabbix#localhost WITH GRANT OPTION;"
I get the error message: ... db can't be created, zabbix base exist
But the result of the command:
>show databases;
doesn't show db zabbix in db list.
script
#!/usr/bin/env bash
setenforce 0
#Install the repository configuration package
rpm -Uvh https://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm
yum clean all
#Install Zabbix server, frontend, agent, database
yum install zabbix-server-mysql -y
yum install zabbix-web-mysql -y
yum install zabbix-agent -y
yum install mariadb-server -y
#Start DB
systemctl start mariadb
#Create DB (example1)
mysql -uroot <<EOF
create database zabbix character set utf8 collate utf8_bin;
create user 'zabbix'#'localhost' identified by 'zabbix';
grant all privileges on zabbix.* to 'zabbix'#'localhost';
EOF
#Create DB (example2)
mysql -u root -e "CREATE DATABASE zabbix; CREATE USER zabbix#localhost identified by 'zabbix'; GRANT ALL ON zabbix.* to zabbix#localhost WITH GRANT OPTION;"
#Import initial schema and data
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql zabbix
#Configure the database for Zabbix server
echo DBPassword=zabbix >> /etc/zabbix/zabbix_server.conf
#Start zabbix server
systemctl start zabbix-server
#Configure frontend
sed -i 's:# php_value date.timezone.*:php_value date.timezone Europe\/Minsk:g' /etc/httpd/conf.d/zabbix.conf;
#Start httpd
systemctl restart zabbix-server zabbix-agent httpd
#Make Zabbix server and agent processes start at system boot
systemctl enable zabbix-server zabbix-agent httpd
Depending on what you want to achieve, this may be a good suggestion:
CREATE DATABASE IF NOT EXISTS zabbix;
This makes the call idempotent!
The problem was, that the script contains a \r (CR) at the end (0d). Remove it with:
tr -d '\r' < old_name_script > new_name_script
And the script works fine.
If it will be useful, there is fully working example of the script, that creates zabbix server:
#!/usr/bin/env bash
#Disable SELinux
enforceStatus=getenforse
if [ "$enforceStatus" != "Permissive" ]; then
setenforce 0
fi
#Install the repository configuration package
rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
yum clean all
#Install Zabbix server, frontend, agent, database, httpd
yum install zabbix-server-mysql -y
yum install zabbix-web-mysql -y
yum install zabbix-agent -y
yum install mariadb mariadb-server -y
yum install httpd -y
#Start and add to autostart DB mariadb
systemctl start mariadb
systemctl enable mariadb.service
#Create DB (example1)
mysql -uroot <<EOF
create database zabbix character set utf8 collate utf8_bin;
create user 'zabbix'#'localhost' identified by 'zabbix';
grant all privileges on zabbix.* to 'zabbix'#'localhost';
EOF
#Import initial schema and data
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql zabbix
#Configure the database for Zabbix server
echo DBPassword=zabbix >> /etc/zabbix/zabbix_server.conf
#Configure frontend
sed -i 's:# php_value date.timezone.*:php_value date.timezone Europe\/Minsk:g' /etc/httpd/conf.d/zabbix.conf;
#Start zabbix server processes start at system boot
systemctl restart zabbix-server
systemctl enable zabbix-server
#Start httpd processes start at system boot
systemctl restart httpd
systemctl enable httpd
#Start zabbix-agent processes start at system boot
systemctl restart zabbix-agent
systemctl enable zabbix-agent
#Add permissions to irewall
firewall-cmd --permanent --add-port=10050/tcp
firewall-cmd --permanent --add-port=10051/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

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

How to Install mysql-server via dockerfile

Does anybody knows how to install mysql-server via dockerfile? I have written a Dockerfile, but the build ends with an error: /bin/sh: 1: /usr/bin/mysqld: not found
USER root
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server-5.7
# Remove pre-installed database
RUN rm -rf /var/lib/mysql/*
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/"/etc/mysql/my.cnf
ENV DB_USER example
ENV DB_PASSWORD example
ENV DB_NAME example
ENV VOLUME_HOME "/var/lib/mysql"
EXPOSE 3306
RUN cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf
RUN /usr/bin/mysqld && sleep 5 && \
mysql -uroot -e "CREATE USER '${DB_USER}'#'%' IDENTIFIED BY '${DB_PASSWORD}'" && \
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${DB_USER}'#'%' WITH GRANT OPTION" &&\
mysql -uroot -e "CREATE DATABASE ${DB_NAME}" && \
mysqladmin -uroot shutdown
For an ubuntu:16.04 base image, mysqld is found in /usr/sbin, not /usr/bin
If you can add a step RUN which mysqld before your final RUN command that will show you where the mysqld executable is found. It may vary depending on which base image/distro you're using.
You can also use RUN mysqld ... without a full path, if the file is in your $PATH
You may also need to update your RUN sed line as below, adding spaces around the quoted string:
RUN sed -i -e "s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
Otherwise, you may see the following error:
The command '/bin/sh -c sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/"/etc/mysql/my.cnf' returned a non-zero code: 1

Scripts and Dockerfile

I am trying to run a mysql benchmark (sysbench) inside my docker container. The script works well and performs the benchmark while running in the host terminal but when I try to run it inside a container I get an error as:
FATAL: unable to connect to MySQL server, aborting... FATAL: error
2002: Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2) ALERT: Error: failed to determine
table 'sbtest' type! ALERT: MySQL error: Can't connect to local MySQL
server through socket '/var/run/mysqld/mysqld.sock' (2) FATAL: failed
to get database capabilities!
My docker file is:
FROM ubuntu:14.04
RUN apt-get -y update && apt-get install -y sysbench
ADD ./script.sh /code/script.sh
WORKDIR /code
ENTRYPOINT ./script.sh
And my script is:
export DEBIAN_FRONTEND="noninteractive"
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password root'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root'
sudo apt-get -y install mysql-server
sudo mysql --user="root" --password="root" -e "create database dbtest"
sudo sysbench --test=oltp --oltp-table-size=1000000 --mysql-db=dbtest --mysql-user=root --mysql-password=root prepare
sudo sysbench --test=oltp --oltp-table-size=1000000 --oltp-test-mode=complex --oltp-read-only=off --num-threads=6 --max-time=60 --max-requests=0 --mysql-db=dbtest --mysql-user=root --mysql-password=root run
How can I fix this, because the same script works on a normal Linux machine?