skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. Depending on what you want to achieve, this may be a good suggestion:

    CREATE DATABASE IF NOT EXISTS zabbix;
    

    This makes the call idempotent!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search