1 数据库创建

1 Database creation

If installing from Zabbix Git repository, you need to run:

$ make dbschema

prior to proceeding to the next steps.

概述

Overview

Zabbxi数据库必须在Zabbix server或proxy安装的时候创建。 A Zabbix database must be created during the installation of Zabbix server or proxy.

本节提供有关创建Zabbix数据库的说明。 每个受支持的数据库都有对应的创建命令。 This section provides instructions for creating a Zabbix database. A separate set of instructions is available for each supported database.

<note 注意>schema.sql, images.sql and data.sql 这些文件在Zabbix的子目录 database 下. 如果Zabbix是通过分发包安装的, 参考分发包相关文档. :::

<note 重要>对于Zabbix proxy 数据库, 只需要导入 schema.sql (不需要导入 images.sql 和 data.sql) :::

schema.sql, images.sql and data.sql files are located in the database subdirectory of Zabbix sources. If Zabbix was installed from distribution packages, refer to the distribution documentation.

For a Zabbix proxy database, only schema.sql should be imported (no images.sql nor data.sql)

MySQL

  1. shell> mysql -uroot -p<password>
  2. mysql> create database zabbix character set utf8 collate utf8_bin;
  3. mysql> grant all privileges on zabbix.* to [email protected] identified by '<password>';
  4. mysql> quit;

如果使用Zabbix程序包按照手册 Debian/UbuntuRHEL/CentOS 导入数据到数据库。

If you use Zabbix packages continue with instructions for Debian/Ubuntu or RHEL/CentOS to import the data into the database.

  1. shell> cd database/mysql
  2. shell> mysql -uzabbix -p<password> zabbix < schema.sql
  3. # 下面步骤当创建Zabbix proxy数据库时不需要执行
  4. shell> mysql -uzabbix -p<password> zabbix < images.sql
  5. shell> mysql -uzabbix -p<password> zabbix < data.sql

PostgreSQL

需要使用有权限的用户去创建数据库对象。 以下shell命令将创建zabbix用户。在提示下请输入密码并再次确认密码。(注意,可能首先要求输入sudo命令对应的用户密码 ):

You need to have database user with permissions to create database objects. The following shell command will create user zabbix. Specify password when prompted and repeat password (note, you may first be asked for sudo password):

  1. shell> sudo -u postgres createuser --pwprompt zabbix

现在将以先前创建的用户作为数据库所有者(参数:-O zabbix)设置数据库zabbix(最后一个参数)并导入initial schema和数据(假设当前目录位于Zabbix sources的根目录中):

Now we will set up the database zabbix (last parameter) with the previously created user as the owner (-O zabbix) and import initial schema and data (assuming you are in the root directory of Zabbix sources):

  1. shell> sudo -u postgres createdb -O zabbix zabbix

如果使用Zabbix程序包按照手册 Debian/UbuntuRHEL/CentOS导入数据到数据库。

If you use Zabbix packages continue with instructions for Debian/Ubuntu or RHEL/CentOS to import the data into the database.

  1. shell> cd database/postgresql
  2. shell> cat schema.sql | sudo -u zabbix psql zabbix
  3. # 下面步骤当创建Zabbix proxy数据库时不需要执行
  4. shell> cat images.sql | sudo -u zabbix psql zabbix
  5. shell> cat data.sql | sudo -u zabbix psql zabbix

上面的命令仅作为例子提供参考,它可以在大多数GNU / Linux安装中使用。 可以使用不同的命令,例如:”psql -U <username>”,这取决于系统/数据库的配置方式。如果在设置数据库时遇到麻烦,请咨询数据库管理员。

The above commands are provided as an example that will work in most of GNU/Linux installations. You can use different commands, e. g. “psql -U <username>” depending on how your system/database are configured. If you have troubles setting up the database please consult your Database administrator.

Oracle

假设在Oracle服务器host上存在有权限创建数据库对象的用户(用户名为zabbix,密码为password),并且该用户具有/tmp目录的写入权限。Zabbix数据库需要使用UTF8字符集。检查当前设置:

We assume that a zabbix database user with password password exists and has permissions to create database objects in ORCL service located on the host Oracle database server with a user shell user having write access to /tmp directory. Zabbix requires a Unicode database character set and a UTF8 national character set. Check current settings:

  1. sqlplus> select parameter,value from v$nls_parameters where parameter='NLS_CHARACTERSET' or parameter='NLS_NCHAR_CHARACTERSET';

需要将Zabbix数据库安装介质拷贝到Oracle服务器上的/tmp/zabbix_images目录下:

If you are creating a database for Zabbix server you need to have images from Zabbix sources on the host where Oracle is running. Copy them to a directory /tmp/zabbix_images on the Oracle host:

  1. shell> cd /path/to/zabbix-sources
  2. shell> ssh [email protected]_host "mkdir /tmp/zabbix_images"
  3. shell> scp -r misc/images/png_modern [email protected]_host:/tmp/zabbix_images/

现在开始创建数据库: Now prepare the database:

  1. shell> cd /path/to/zabbix-sources/database/oracle
  2. shell> sqlplus zabbix/[email protected]_host/ORCL
  3. sqlplus> @schema.sql
  4. # 下面步骤当创建Zabbix proxy数据库时不需要执行
  5. sqlplus> @images.sql
  6. sqlplus> @data.sql

然后删掉介质存放的临时目录: Now the temporary directory can be removed:

  1. shell> ssh [email protected]_host "rm -rf /tmp/zabbix_images"

IBM DB2

  1. shell> db2 "create database zabbix using codeset utf-8 territory us pagesize 32768"
  2. shell> cd database/ibm_db2
  3. shell> db2batch -d zabbix -f schema.sql
  4. # 下面步骤当创建Zabbix proxy数据库时不需要执行
  5. shell> db2batch -d zabbix -f images.sql
  6. shell> db2batch -d zabbix -f data.sql

Zabbix server,Zabbix proxy,Zabbix web server必须将字符集设置为UTF-8。否则,来自Zabbix的文本信息将被IBM DB2服务器解析为非UTF-8字符集,并在Zabbix与数据库之间互相转换。数据库将存储有问题的非ASCII字符。

It is important to set UTF-8 locale for Zabbix server, Zabbix proxy and the web server running Zabbix frontend. Otherwise text information from Zabbix will be interpreted by IBM DB2 server as non-UTF-8 and will be additionally converted on the way from Zabbix to the database and back. The database will store corrupted non-ASCII characters.

Zabbix前端使用了SQL中的OFFSETLIMIT语句。为了SQL正常工作,IBM DB2服务器必须设置环境变量DB2_COMPATIBILITY_VECTOR的值为3。在数据库启动之前执行下面的命令:

Zabbix frontend uses OFFSET and LIMIT clauses in SQL queries. For this to work, IBM DB2 server must have DB2_COMPATIBILITY_VECTOR variable be set to 3. Run the following command before starting the database server:

  1. shell> db2set DB2_COMPATIBILITY_VECTOR=3

SQLite

只有为Zabbix proxy 创建数据库的时候才能使用SQLite!

Using SQLite is supported for Zabbix proxy only!

如果使用SQLite作为Zabbix proxy的数据库,创建时如果数据库不存在,将自动创建。

If using SQLite with Zabbix proxy, database will be automatically created if it does not exist.

  1. shell> cd database/sqlite3
  2. shell> sqlite3 /var/lib/sqlite/zabbix.db < schema.sql

返回 安装部分。 Return to the installation section.