【问题标题】:Why bash script, that must creat and run zabbix server, execute without errors, but result is wrong?为什么 bash 脚本,必须创建并运行 zabbix 服务器,执行没有错误,但结果是错误的?
【发布时间】:2021-01-14 21:20:30
【问题描述】:

每个人。我ve started to learn bash and DevOps, lost 2 days, but I cant了解以下内容: 要求

  • Centos 7
  • 数据库mysql
  • zabbix 服务器版本 4
  • mariadb

我需要编写 bash 脚本(问题末尾的脚本),它创建并运行 zabbix 服务器(正好是 4 版本)。当我以两种方式逐步手动运行脚本(手动在 pwsh 中插入每个命令)以创建数据库(示例 1 或示例 2)时 - 它正确执行:创建 zabbix db,启动所有服务,zabbix 前端运行良好。 但是当我以 root 用户身份或通过命令以本地用户身份运行脚本时:

>sh -c /home/zabbix_server

脚本完成者:

>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.

在脚本期间或脚本结束时没有错误消息,但是当我尝试手动执行命令 create db(example1 或 example2)时,没关系:

#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;"

我收到错误消息:... db can't be created, zabbix base exists 但是命令的结果:

>show databases;

在 db 列表中不显示 db zabbix。

脚本

#!/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

【问题讨论】:

  • 您的脚本正在创建数据库和用户两次。首先低于Create DB (example1),然后再次低于Create DB (example2)。第二个由于重复而出错。
  • example1 和 exaple2 是创建数据库的两种不同方式。我使用了example1或example2,但没有同时使用
  • 您是否以 zabbix 用户身份登录 mysql 来执行“显示数据库”?如果在标头中添加 -x 到 bash 即 #!/usr/bin/env bash -x,您将获得更多调试信息
  • @A.Matrosov : 请注意 sh -c /home/zabbix_server 仅将脚本 zabbix-server 作为 bash 运行 如果 脚本具有执行权限 并且 a # !- bash 的行。如果不是这种情况,您必须使用bash /home/zabbix_server 运行它才能让bash 执行它。

标签: mysql linux bash mariadb


【解决方案1】:

根据您想要实现的目标,这可能是一个不错的建议:

CREATE DATABASE IF NOT EXISTS zabbix;

这使得调用是幂等的!

【讨论】:

    【解决方案2】:

    问题是,该脚本在末尾 (0d) 包含一个 \r (CR)。删除它:

    tr -d '\r' < old_name_script > new_name_script
    

    脚本运行良好。 如果它有用的话,有一个完整的脚本示例,可以创建 zabbix 服务器:

    #!/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
    

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      相关资源
      最近更新 更多