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

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

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

Amazon EC2 user-data, how install and configure MySQL? (third-party database)

I try to provision an ec2 instance and I using an userdata:
#!/bin/bash
yum update -y
yum install httpd php php-mysql -y
amazon-linux-extras install php7.4 -y
yum install php-mbstring php-xml php-intl -y
service httpd start
chkconfig httpd on
yum install mysql -y
Everything is working properly but I don't know how to set DBRootPassword, DBUser and DBName. And run SQL command.
Indeed, this commands don't works:
mysqladmin -u root password 'DBRootPassword'
mysql -u root --password='DBRootPassword'
and sql command like that:
CREATE DATABASE DBName;
CREATE USER 'DBUser'#'localhost' IDENTIFIED BY 'DBPassword';
GRANT ALL ON DBName.* TO 'DBUser'#'localhost';
FLUSH PRIVILEGES;
Can you help me? (In this project I don't want use RDS, but a third-party database)
Best Regards

How to set up root password while installing mysql in docker?

I am trying to install mysql in docker and setting default password of root as root using following Dockerfile.
Dockerfile:
FROM ubuntu:20.04
COPY mysql_setup.sh /software/mysql_setup.sh
RUN sh /software/mysql_setup.sh
CMD ["/bin/sh"]
Where /software/mysql_setup.sh
# Download and Install the Latest Updates for the OS
apt-get update && apt-get upgrade -y
# Set the Server Timezone to CST
echo "America/Chicago" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata
# Enable Ubuntu Firewall and allow SSH & MySQL Ports
ufw enable
ufw allow 22
ufw allow 3306
# Install essential packages
apt-get -y install zsh htop
# Install MySQL Server in a Non-Interactive mode. Default root password will be "root"
echo "mysql-server mysql-server-8.0.26/root_password password root" | sudo debconf-set-selections
echo "mysql-server mysql-server-8.0.26/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mysql-server
# Run the MySQL Secure Installation wizard
mysql_secure_installation
sed -i 's/127\.0\.0\.1/0\.0\.0\.0/g' /etc/mysql/my.cnf
mysql -uroot -p -e 'USE mysql; UPDATE `user` SET `Host`="%" WHERE `User`="root" AND `Host`="localhost"; DELETE FROM `user` WHERE `Host` != "%" AND `User`="root"; FLUSH PRIVILEGES;'
service mysql restart
But above code does not seem to be working as when i enter into docker and use and use root as password it gives an error.
root#839f2946bfdf:/# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
How can i fix this error?
You cannot run a mysql install inside a container in a normal way. Thing like ufw cannot be done. The entire RUN step is at build time only, so service mysql restart won't be running when the container is started, only CMD is.
Recommend just using the Docker Library mysql container. There's a standard MYSQL_ROOT_PASSWORD variable for setting the root password.

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

MariaDB never starts within docker image

I'm creating a docker image with ubuntu trusty and MariaDB 5.5 but I can never get MariaDB to start unless I actually connect to the running VM.
Dockerfile:
FROM ubuntu:trusty
# Upgrade packages
RUN apt-get update && apt-get upgrade -y
# So we can add a repo to apt
RUN apt-get install -y software-properties-common
# Add MariaDB repo to aptitude
RUN apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
RUN add-apt-repository 'deb http://mirror.jmu.edu/pub/mariadb/repo/5.5/ubuntu trusty main'
RUN apt-get update
# Install MariaDB
RUN DEBIAN_FRONTEND=noninteractive debconf-set-selections << 'mariadb-server-5.5 mysql-server/root_password password PASS'
RUN DEBIAN_FRONTEND=noninteractive debconf-set-selections << 'mariadb-server-5.5 mysql-server/root_password_again password PASS'
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mariadb-server
# Start MariaDB
RUN service mysql start
# Configure MariaDB User permissions
RUN echo "CREATE USER 'ubuntu'#'localhost'" | mysql -u root
The command I'm running to create:
docker build -t ebth-com-trusty --file `pwd`/Dockerfile `pwd` --no-cache
The create command will always fail due to:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
But if I comment out the CREATE USER command, and then connect to the VM, I can connect to MariaDB just fine after running a manual service mysql start.
It is as if the RUN service mysql start just doesn't work, and I'm not sure how to debug this any further.
Every command in a Dockerfile is run in it's own container.
You can think of the process like:
new container is spun up using the previous image
the command is executed
the container is spun down
an image of that container is taken for the next command to run on
This means that the command RUN service mysql start will spin up a new container, start MariaDB, then shut down the container including shutting down MariaDB.
Instead try using CMD and ENTRYPOINT. You can think of them as setting the default executable, command and parameters executed once the container is spun up. However, the difference between the two is a bit more neuanced. Check out the docs: CMD and ENTRYPOINT
It would look something like this:
# Create default user
RUN service mysql start && \
echo "CREATE USER 'ubuntu'#'localhost'" | mysql -u root
# Start MariaDB
ENTRYPOINT ["/bin/bash"]
CMD ["service", "mysql", "start"]