首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么bash脚本,必须创建并运行zabbix服务器,执行时没有错误,但结果是错误的?

为什么bash脚本,必须创建并运行zabbix服务器,执行时没有错误,但结果是错误的?
EN

Stack Overflow用户
提问于 2021-01-14 21:20:30
回答 2查看 562关注 0票数 0

所有人。我ve started to learn bash and DevOps, lost 2 days, but I can不明白以下几点:requirments

4

  • mariadb

  • Centos 7
  • DB mysql

服务器版本

我需要编写bash脚本(问题末尾的脚本),该脚本创建并运行zabbix服务器(确切地说是4个版本)。当我一步一步地手动运行脚本(手动将每个命令插入pwsh中)时,可以使用两种方法创建db (example1或example2) --它是正确执行的:创建了zabbix,启动了所有服务,zabbix前端运行良好。但是,当我以根用户或本地用户的身份通过命令运行脚本时:

代码语言:javascript
运行
复制
>sh -c /home/zabbix_server

脚本通过以下方式完成:

代码语言:javascript
运行
复制
>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 (example1或example2)时,这并不重要:

代码语言:javascript
运行
复制
#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,则存在zabbix基,但是命令的结果是:

代码语言:javascript
运行
复制
>show databases;

不会在db列表中显示db zabbix。

脚本

代码语言:javascript
运行
复制
#!/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
EN

回答 2

Stack Overflow用户

发布于 2021-01-14 23:59:20

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

代码语言:javascript
运行
复制
CREATE DATABASE IF NOT EXISTS zabbix;

这就是呼叫的幂等性!

票数 0
EN

Stack Overflow用户

发布于 2021-01-16 16:44:47

问题是,脚本在末尾(0d)包含一个\r (CR)。用下列方法移除:

代码语言:javascript
运行
复制
tr -d '\r' < old_name_script > new_name_script

剧本写得很好。如果有用的话,有一个完整的脚本示例,它创建了zabbix服务器:

代码语言:javascript
运行
复制
#!/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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65727084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档